C++ 2.0 MultithreadedTableWriter Upsert 失败

表结构

months=2010.01M..2040.12M
db_xhTradeDailyDb=database('dfs://xhTradeDailyDb', VALUE, months)

db=database("dfs://xhTradeDailyDb")
table_name="Test"
if(existsTable("dfs://xhTradeDailyDb",table_name))
    dropTable(db,table_name);
col_names=["int_value","string_value","double_value"]
col_types=[INT,STRING,DOUBLE]
partition_col_names = []
new_table = db.createTable(table(5000:0, col_names, col_types), table_name)
colComment = {string_value:"string",double_value:"double"}
setColumnComment(new_table,colComment)
schema(new_table).colDefs;

C++ writer 创建

 vector<string> colNames;

 colNames.push_back("int_value");

writer = new MultithreadedTableWriter("127.0.0.1", 8848, "admin", "123456",

                                              "dfs://xhTradeDailyDb", "Test",

                                              false, false, NULL,

                                              1, 1, 1,

                                              "", nullptr,dolphindb::MultithreadedTableWriter::M_Upsert,&colNames);

写入

writer->insert(errorInfo, rand() % 10000, "AAAAAAAB", rand() % 10000 + 0.1) 

报错

2023-07-24 21:13:21.038509: [139790579783424] Error: threadid 2279200512 Failed to save the inserted data:  127.0.0.1:8848 Server response: 'Syntax Error: [line #1] Cannot recognize the token int_value' script: 'upsert!{loadTable("dfs://xhTradeDailyDb","Test"),,int_value}'

这个语句  script: 'upsert!{loadTable("dfs://xhTradeDailyDb","Test"),,`int_value 拼写的就不对,中间参数空了,ignoreNull没有
我在终端上执行
pt = loadTable('dfs://xhTradeDailyDb', 'Test')
t2 = table( 2 as int_value, "2" as string_value, 2.1 as double_value)
upsert!(pt, t2,false,`int_value)
是可以upsert成功的
writer 拼出来的语句不对
请先 登录 后评论

1 个回答

wale

在new一个MultithreadedTableWriter类的时候,最后一个参数的含义是:modeOption 字符串数组,表示不同模式下的扩展选项,目前,仅当 mode 指定为 M_Upsert 时有效,表示由 upsert! 可选参数组成的字符串数组

upser!函数的用法为upsert!(obj, newData, [ignoreNull=false], [keyColNames], [sortColumns])

这里报错是因为在传入参数的时候colNames.push_back("int_value");只传入了一个参数,改为vector<string> colNames{"false", "`int_value"};就可以正常插入数据了。

int main()
{
    static DBConnection conn(false, false);
    conn.connect("192.168.0.126", 8848, "admin", "123456");
    vector<string> colNames{"false", "`int_value"};
    SmartPointer<MultithreadedTableWriter> writer;
    writer = new MultithreadedTableWriter("192.168.0.126", 8848, "admin", "123456",
                                                "dfs://xhTradeDailyDb", "Test",
                                                false, false, NULL,
                                                1, 1, 1,
                                                "", nullptr,dolphindb::MultithreadedTableWriter::M_Upsert,&colNames);
    ErrorCodeInfo errorInfo;
    writer->insert(errorInfo, rand() % 10000, "AAAAAAAB", rand() % 10000 + 0.1);
    if(errorInfo.hasError()){
        cout << errorInfo.errorInfo <<endl;
    }
    return 0;
}
请先 登录 后评论
  • 1 关注
  • 0 收藏,529 浏览
  • worm 提出于 2023-07-24 21:13

相似问题