将日期时间插入卡桑德拉
Insert datetime into cassandra
我正在尝试将数据从 storm 插入到 cassandra。初始为字符串类型
我的 java class 有以下代码:
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());
这里我得到错误,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 is of type timestamp"。我的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
中需要 int
参数的 setXyz
方法将该值解释为从 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 驱动程序中使用 QueryBuilder
,你可以这样调用它:
session.execute(insertInto("table_name")
.value("userId", literal("123"))
.value("created_at", function("toTimestamp", function("now")))
).toCql());
(假设 Cassandra 2.2 或更高版本,支持使用 toTimestamp
;请参阅 2.2 上的文档)
我正在尝试将数据从 storm 插入到 cassandra。初始为字符串类型
我的 java class 有以下代码:
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());
这里我得到错误,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 is of type timestamp"。我的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
中需要 int
参数的 setXyz
方法将该值解释为从 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 驱动程序中使用 QueryBuilder
,你可以这样调用它:
session.execute(insertInto("table_name")
.value("userId", literal("123"))
.value("created_at", function("toTimestamp", function("now")))
).toCql());
(假设 Cassandra 2.2 或更高版本,支持使用 toTimestamp
;请参阅 2.2 上的文档)