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());
}
}
以上代码枚举了常见dolphinDB数据类型,核心思路是构建对应字符串,通过run来插入数据。特别需要注意字符串需符合相应类型规范,不得少any character, 否则会导致非预期的结果。数据类型格式参见:
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!