5 Star 6 Fork 0

浙江智臾科技有限公司/api-java

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
MultithreadedTableWriterDemo.java 2.79 KB
一键复制 编辑 原始数据 按行查看 历史
笨笨 提交于 2023-12-12 15:32 +08:00 . fix bugs of examples code.
package examples;
import com.xxdb.DBConnection;
import com.xxdb.comm.ErrorCodeInfo;
import com.xxdb.data.*;
import com.xxdb.data.Vector;
import com.xxdb.multithreadedtablewriter.MultithreadedTableWriter;
import java.time.LocalDate;
import java.util.*;
/**
* MTW (MultihreadedTableWriter) is used to write streaming data into dfs table or streamTable.
* when write dfs table, we recommend that the thread number equals to the partition number of the dfs table.
*/
public class MultithreadedTableWriterDemo {
private static String host = "localhost";
private static int port = 8848;
private static String username = "admin";
private static String password = "123456";
public static void main(String[] args) throws Exception {
DBConnection conn= new DBConnection();
conn.connect(host, port, "admin", "123456");
Random random = new Random();
String script =
"dbName = 'dfs://valuedb3'" +
"if (exists(dbName))" +
"{" +
"dropDatabase(dbName);" +
"}" +
"datetest = table(1000:0,`date`symbol`id,[DATE, SYMBOL, LONG]);" +
"db = database(directory= dbName, partitionType= HASH, partitionScheme=[INT, 10]);" +
"pt = db.createPartitionedTable(datetest,'pdatetest','id');";
conn.run(script);
MultithreadedTableWriter multithreadedTableWriter_ = new MultithreadedTableWriter(host, port, "admin", "123456", "dfs://valuedb3", "pdatetest",
false, false, null, 10000, 1,
5, "id", new int[]{Vector.COMPRESS_LZ4, Vector.COMPRESS_LZ4, Vector.COMPRESS_DELTA});
ErrorCodeInfo ret;
try
{
for (int i = 0; i < 10000; ++i)
{
ret = multithreadedTableWriter_.insert(LocalDate.of(2022, 3, 23), "AAAAAAAB", random.nextInt() % 10000);
if(ret.hasError()){
System.out.println("insert failed: " + ret.getErrorInfo());
}
}
}
catch (Exception e) // mtw may throw exception
{
System.out.println("MTW exit with exception {0}" + e.getMessage());
}
//wait for the Completion by join the thread.
multithreadedTableWriter_.waitForThreadCompletion();
MultithreadedTableWriter.Status writeStatus = new MultithreadedTableWriter.Status();
writeStatus = multithreadedTableWriter_.getStatus();
// check if mtw has errors.
if (writeStatus.hasError())
{
System.out.println("error in writing !");
}
System.out.println("writeStatus: {0}\n" + writeStatus.toString());
System.out.println(((BasicLong)conn.run("exec count(*) from pt")).getLong());
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dolphindb/api-java.git
git@gitee.com:dolphindb/api-java.git
dolphindb
api-java
api-java
master

搜索帮助