使用java api写入 dolphinDB

使用java api run方法插入数据

java api 写入dolphinDB

1 . 案例代码

package com.dolphindb;
import com.xxdb.*;
import com.xxdb.data.BasicStringVector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public void connect(){
try{
this.conn.connect("192.168.1.156", 8961);
logger.info("connect success");
}catch (IOException e){
logger.error("connect failed,{}",e.getCause());
}
}

public void prepare(){
List<String> scripts = new ArrayList<>();
scripts.add("t_many_columns=table(200:0\n" +
",`col1`col2`col3`col4`col5`col6`col7`col8`col9`col10`col11`col12`col13`col14`col15`col16`col17`col18`col19\n" +
",[BOOL,CHAR,SHORT,INT,LONG,DATE,MONTH,TIME,MINUTE,SECOND,DATETIME,TIMESTAMP,NANOTIME,NANOTIMESTAMP,FLOAT,DOUBLE,SYMBOL,STRING,UUID]\n" +
")");
scripts.add("share t_many_columns as t_many_columns_g;");
int total = scripts.size();
int failed = 0;
for(String script : scripts){
try {
this.conn.run(script);
}catch (IOException e){
logger.error("command failed {} caused by {}", script, e.getCause());
}
}
logger.info("run {} command(s), {} failed.", total, failed);

}


public static void main(String[] args) throws IOException {
InsertData test = new InsertData();
test.connect();
test.prepare();
test.testInsert();

}

public void testStringVector() throws IOException {
BasicStringVector vector = (BasicStringVector)conn.run("rand(`IBM`MSFT`GOOG`BIDU, 10)");
int size = vector.rows();
System.out.println("size: "+size);
for(int i=0; i<size; ++i)
System.out.println(vector.getString(i));
}

public void testInsert() throws IOException {
String[] record = {"true", "'a'", "122h", "21", "21h", "2013.06.13", "2012.06M",
"13:30:10.008", "03:30m", "13:30:10", "2012.06.13T13:30:10", "2012.06.13T13:30:10.008",
"13:30:10.008007006", "2012.06.13T13:30:10.008007006", "2.1f", "2.1", "`ABAB", "`hello",
"uuid(\"5d212a78-cc48-e3b1-4235-b4d91473ee87\")"
};
StringBuilder sb = new StringBuilder();
sb.append("insert into t_many_columns values(");
for (int i = 0; i < record.length-1; i++) {
sb.append(record[i]);
sb.append(",");
}
sb.append(record[record.length-1]);
sb.append(");");
logger.info("run command: {}", sb);
this.conn.run(sb.toString());
}
}

attachments-2022-04-A1oylP1s6254ef1e91d70.png

2. 说明

以上代码枚举了常见dolphinDB数据类型,核心思路是构建对应字符串,通过run来插入数据。特别需要注意字符串需符合相应类型规范,不得少any character, 否则会导致非预期的结果。数据类型格式参见:

用户手册-数据类型

  • 发表于 2022-04-12 11:06
  • 阅读 ( 2931 )
  • 分类:编程语言

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 文章