Skip to content

Commit e540195

Browse files
dl239denglong4paradigm
authored
feat: add light java sdk (#3642)
* feat: add light java sdk * test: add test * refact: type * fix: fix light * fix: fix compile * test: fix online mode * fix: fix comment --------- Co-authored-by: denglong <[email protected]> Co-authored-by: 4paradigm <[email protected]>
1 parent a8c0226 commit e540195

File tree

8 files changed

+43
-17
lines changed

8 files changed

+43
-17
lines changed

java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/SdkOption.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class SdkOption {
4545
private int glogLevel = 0;
4646
private String glogDir = "";
4747
private int maxSqlCacheSize = 50;
48+
private boolean isLight = false;
4849

4950
private void buildBaseOptions(BasicRouterOptions opt) {
5051
opt.setEnable_debug(getEnableDebug());

java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/SqlException.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ public class SqlException extends Exception {
2020
public SqlException(String message) {
2121
super(message);
2222
}
23+
24+
public SqlException(String message, Throwable cause) {
25+
super(message, cause);
26+
}
27+
28+
public SqlException(Throwable cause) {
29+
super(cause);
30+
}
2331
}

java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/impl/SqlClusterExecutor.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,28 @@ public class SqlClusterExecutor implements SqlExecutor {
6262
private static final AtomicBoolean initialized = new AtomicBoolean(false);
6363
private SQLRouter sqlRouter;
6464
private DeploymentManager deploymentManager;
65-
private ZKClient zkClient;
6665
private InsertPreparedStatementCache insertCache;
6766

6867
public SqlClusterExecutor(SdkOption option, String libraryPath) throws SqlException {
6968
initJavaSdkLibrary(libraryPath);
70-
69+
ZKClient zkClient = null;
7170
if (option.isClusterMode()) {
7271
SQLRouterOptions sqlOpt = option.buildSQLRouterOptions();
7372
this.sqlRouter = sql_router_sdk.NewClusterSQLRouter(sqlOpt);
7473
sqlOpt.delete();
75-
zkClient = new ZKClient(ZKConfig.builder()
76-
.cluster(option.getZkCluster())
77-
.namespace(option.getZkPath())
78-
.sessionTimeout((int)option.getSessionTimeout())
79-
.build());
80-
try {
81-
if (!zkClient.connect()) {
82-
throw new SqlException("zk client connect failed.");
74+
if (!option.isLight()) {
75+
zkClient = new ZKClient(ZKConfig.builder()
76+
.cluster(option.getZkCluster())
77+
.namespace(option.getZkPath())
78+
.sessionTimeout((int)option.getSessionTimeout())
79+
.build());
80+
try {
81+
if (!zkClient.connect()) {
82+
throw new SqlException("zk client connect failed.");
83+
}
84+
} catch (Exception e) {
85+
throw new SqlException("init zk client failed", e);
8386
}
84-
} catch (Exception e) {
85-
throw new SqlException("init zk client failed. " + e.getMessage());
8687
}
8788
} else {
8889
StandaloneOptions sqlOpt = option.buildStandaloneOptions();

java/openmldb-jdbc/src/test/java/com/_4paradigm/openmldb/jdbc/SQLRouterSmokeTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
public class SQLRouterSmokeTest {
4747
public static SqlExecutor clusterExecutor;
48+
public static SqlExecutor lightClusterExecutor;
4849
public static SqlExecutor standaloneExecutor;
4950

5051
static {
@@ -54,9 +55,10 @@ public class SQLRouterSmokeTest {
5455
option.setZkCluster(TestConfig.ZK_CLUSTER);
5556
option.setSessionTimeout(200000);
5657
clusterExecutor = new SqlClusterExecutor(option);
57-
java.sql.Statement state = clusterExecutor.getStatement();
58-
state.execute("SET @@execute_mode='online';");
59-
state.close();
58+
setOnlineMode(clusterExecutor);
59+
option.setLight(true);
60+
lightClusterExecutor = new SqlClusterExecutor(option);
61+
setOnlineMode(lightClusterExecutor);
6062
// create standalone router
6163
SdkOption standaloneOption = new SdkOption();
6264
standaloneOption.setHost(TestConfig.HOST);
@@ -69,6 +71,16 @@ public class SQLRouterSmokeTest {
6971
}
7072
}
7173

74+
static void setOnlineMode(SqlExecutor executor) {
75+
java.sql.Statement state = executor.getStatement();
76+
try {
77+
state.execute("SET @@execute_mode='online';");
78+
state.close();
79+
} catch (Exception e) {
80+
e.printStackTrace();
81+
}
82+
}
83+
7284
@Test
7385
void testMoreOptions() throws Exception {
7486
SdkOption option = new SdkOption();
@@ -82,7 +94,7 @@ void testMoreOptions() throws Exception {
8294

8395
@DataProvider(name = "executor")
8496
public Object[] executor() {
85-
return new Object[] { clusterExecutor, standaloneExecutor };
97+
return new Object[] { clusterExecutor, lightClusterExecutor, standaloneExecutor };
8698
}
8799

88100
@Test(dataProvider = "executor")
@@ -128,7 +140,7 @@ public void testSmoke(SqlExecutor router) {
128140

129141
// select
130142
String select1 = "select * from tsql1010;";
131-
SQLResultSet rs1 = (SQLResultSet) router .executeSQL(dbname, select1);
143+
SQLResultSet rs1 = (SQLResultSet) router.executeSQL(dbname, select1);
132144

133145
Assert.assertEquals(2, rs1.GetInternalSchema().getColumnList().size());
134146
Assert.assertEquals(Types.BIGINT, rs1.GetInternalSchema().getColumnType(0));

java/openmldb-spark-connector/src/main/java/com/_4paradigm/openmldb/spark/OpenmldbSource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public StructType inferSchema(CaseInsensitiveStringMap options) {
5353
option = new SdkOption();
5454
option.setZkCluster(zkCluster);
5555
option.setZkPath(zkPath);
56+
option.setLight(true);
5657
String timeout = options.get("sessionTimeout");
5758
if (timeout != null) {
5859
option.setSessionTimeout(Integer.parseInt(timeout));

java/openmldb-spark-connector/src/main/java/com/_4paradigm/openmldb/spark/write/OpenmldbDataSingleWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public OpenmldbDataSingleWriter(OpenmldbWriteConfig config, int partitionId, lon
4444
SdkOption option = new SdkOption();
4545
option.setZkCluster(config.zkCluster);
4646
option.setZkPath(config.zkPath);
47+
option.setLight(true);
4748
SqlClusterExecutor executor = new SqlClusterExecutor(option);
4849
String dbName = config.dbName;
4950
String tableName = config.tableName;

java/openmldb-spark-connector/src/main/java/com/_4paradigm/openmldb/spark/write/OpenmldbDataWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public OpenmldbDataWriter(OpenmldbWriteConfig config, int partitionId, long task
4444
SdkOption option = new SdkOption();
4545
option.setZkCluster(config.zkCluster);
4646
option.setZkPath(config.zkPath);
47+
option.setLight(true);
4748
SqlClusterExecutor executor = new SqlClusterExecutor(option);
4849
String dbName = config.dbName;
4950
String tableName = config.tableName;

java/openmldb-spark-connector/src/main/scala/com/_4paradigm/openmldb/spark/read/OpenmldbPartitionReader.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class OpenmldbPartitionReader(config: OpenmldbReadConfig) extends PartitionReade
1313
val option = new SdkOption
1414
option.setZkCluster(config.zkCluster)
1515
option.setZkPath(config.zkPath)
16+
option.setLight(true)
1617
val executor = new SqlClusterExecutor(option)
1718
val dbName: String = config.dbName
1819
val tableName: String = config.tableName

0 commit comments

Comments
 (0)