java api demo

一个简单的java api连接dolphinDB,进行DML,DDL操作。
package com.dolphindb;
import com.xxdb.data.Entity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.xxdb.DBConnection;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;


public class TestConn {
public static final Logger logger = LogManager.getLogger(TestConn.class);
private DBConnection conn = new DBConnection();

public TestConn(String host, int port, String user, String password) throws IOException {
conn.connect(host, port, user, password);
}

public TestConn(String host, int port) throws IOException {
this(host, port, null, null);
}

public static void main(String[] args) {
String host = "192.168.1.156";
int port = 8961;
String user = "admin";
String password = "123456";


ArrayList<String> scripts = new ArrayList<>();
scripts.add("timestamp = [2022.04.07T13:30:10.000,2022.04.07T13:30:05.008,2022.04.07T13:31:10.000,2022.04.07T13:32:10.000]");
scripts.add("nodeId = 1123598818738675223 1123598821738675201 1123598818738675223 1123598818738675223");
scripts.add("noiseValue=40 50 60 70");
scripts.add("t=table(timestamp, nodeId, noiseValue)");
scripts.add("share t as tglobal"); //share this table so you can use gui to test data
scripts.add("if(existsDatabase(\"dfs://valuedb\")) dropDatabase(\"dfs://valuedb\")\n" +
"db=database(\"dfs://valuedb\", VALUE, 2022.01M..2050.12M)");
scripts.add("pt = db.createPartitionedTable(t, `pt, `timestamp)");
scripts.add("pt.append!(t)");
scripts.add("pt=loadTable(db,`pt)");
scripts.add("select * from pt");

try{
TestConn test = new TestConn(host, port, user, password);
logger.info("successfully connect to {}.",host);
for (String s : scripts) {
test.run(s);
}
}
catch (IOException e){
logger.error("connect failed : {}",e.getCause());
}
}

private Entity run(String script) throws IOException {
logger.info("run script: {}", script);
return conn.run(script);

}

}

0 条评论

请先 登录 后评论
wfHuang
wfHuang

3 篇文章

作家榜 »

  1. Junxi 73 文章
  2. liang.lin 5 文章
  3. mhxiang 4 文章
  4. wfHuang 3 文章
  5. admin 3 文章
  6. chris 2 文章
  7. 丘坤威 1 文章
  8. 陈枢之 1 文章