gpt4 book ai didi

java - 将日期时间插入 Cassandra

转载 作者:行者123 更新时间:2023-11-30 08:42:12 25 4
gpt4 key购买 nike

我正在尝试将数据从 storm 插入到 cassandra。初始为字符串类型。

我的 java 类有以下代码:

String insertQuery1
= "insert into fault.as_fo_ag_uc ("
+ "host,"
+ "trigger,"
+ "eventtime,uuiddefault) "
+ "values(?,?,?,?)";

BoundStatement boundStatement = new BoundStatement(statement);
boundStatement.setString(1, dto.getHost());
boundStatement.setString(2, dto.getTrigger());
Timestamp ts = Timestamp.valueOf(dto.getEventTime());
boundStatement.setDate(3, ts);
boundStatement.setString(4, dto.getUIDDefault());

这里 eI 得到错误,eventtime 是时间戳类型。因为我已转换为时间戳,但它不起作用。我也尝试过使用 Simple Date Formater 将其格式化为日期类型

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,z");
boundStatement.setDate(3, new java.sql.Date(df.parse(dto.getEventTime)).getTime()));

错误是“EventTime 是时间戳类型”。我的 cassandra 驱动版本是 2.1.7。

com.datastax.driver.core.exceptions.InvalidTypeException: Value eventime is of type timestamp at com.datastax.driver.core.AbstractGettableByIndexData.checkType(AbstractGettableByIndexData.java:89) ~[cassandra-driver-core-2.1.7.1.jar:?] at com.datastax.driver.core.AbstractData.setString(AbstractData.java:157) ~[cassandra-driver-core-2.1.7.1.jar:?] at com.datastax.driver.core.BoundStatement.setString(BoundStatement.java:499) ~[cassandra-driver-core-2.1.7.1.jar:?] at storm.starter.bolt.CassandraWriterBolt.execute(CassandraWriterBolt.java:219) [classes/:?] at backtype.storm.topology.BasicBoltExecutor.execute(BasicBoltExecutor.java:50) [storm-core-0.10.0.jar:0.10.0] at backtype.storm.daemon.executor$fn__5694$tuple_action_fn__5696.invoke(executor.clj:690) [storm-core-0.10.0.jar:0.10.0] at backtype.storm.daemon.executor$mk_task_receiver$fn__5615.invoke(executor.clj:436) [storm-core-0.10.0.jar:0.10.0] at backtype.storm.disruptor$clojure_handler$reify__5189.onEvent(disruptor.clj:58) [storm-core-0.10.0.jar:0.10.0] at backtype.storm.utils.DisruptorQueue.consumeBatchToCursor(DisruptorQueue.java:127) [storm-core-0.10.0.jar:0.10.0] at backtype.storm.utils.DisruptorQueue.consumeBatchWhenAvailable(DisruptorQueue.java:106) [storm-core-0.10.0.jar:0.10.0] at backtype.storm.disruptor$consume_batch_when_available.invoke(disruptor.clj:80) [storm-core-0.10.0.jar:0.10.0] at backtype.storm.daemon.executor$fn__5694$fn__5707$fn__5758.invoke(executor.clj:819) [storm-core-0.10.0.jar:0.10.0] at backtype.storm.util$async_loop$fn__545.invoke(util.clj:479) [storm-core-0.10.0.jar:0.10.0] at clojure.lang.AFn.run(AFn.java:22) [clojure-1.6.0.jar:?] at java.lang.Thread.run(Thread.java:745) [?:1.7.0_79]

最佳答案

BoundStatement 中的setXyz 方法期望 int 参数将该值解释为从 0 开始的索引。

所以你的

boundStatement.setString(2, dto.getTrigger());

实际上是在尝试为 CQL 查询中的 eventtime 列设置占位符。

将语句更改为从 0 开始到 3。

BoundStatement boundStatement = new BoundStatement(statement);
boundStatement.setString(0, dto.getHost());
boundStatement.setString(1, dto.getTrigger());
Timestamp ts = Timestamp.valueOf(dto.getEventTime());
boundStatement.setDate(2, ts);
boundStatement.setString(3, dto.getUIDDefault());

关于java - 将日期时间插入 Cassandra ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34646319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com