Apache NiFi/Cassandra-PutCassandraRecord无法转换为记录对象

Apache NiFi/Cassandra-PutCassandraRecord无法转换为记录对象,cassandra,apache-nifi,Cassandra,Apache Nifi,我试图使用NiFi的PutCassandraRecord处理器将一些JSON记录插入Cassandra数据库。 我试图在Cassandra中插入时间戳类型,但NiFi抱怨输入字符串“2019-02-02T08:00:00.000”存在NumberFormatException 所述时间戳字段的cassandra数据类型为(ts timestamp) 我使用的是Avro模式: {“名称”:“ts”,“类型”:{“类型”:“长”,“逻辑类型”:“时间戳毫秒”} NiFi日志显示它能够解析JSON对象

我试图使用NiFi的PutCassandraRecord处理器将一些JSON记录插入Cassandra数据库。 我试图在Cassandra中插入时间戳类型,但NiFi抱怨输入字符串“2019-02-02T08:00:00.000”存在NumberFormatException

所述时间戳字段的cassandra数据类型为(ts timestamp) 我使用的是Avro模式: {“名称”:“ts”,“类型”:{“类型”:“长”,“逻辑类型”:“时间戳毫秒”}

NiFi日志显示它能够解析JSON对象,但无法将其转换为记录

2019-05-13 21:13:04,036 ERROR [Timer-Driven Process Thread-2] o.a.n.p.cassandra.PutCassandraRecord PutCassandraRecord[id=ecb33d77-cc4a-17f5-23a8-e002e1777a1c] Unable to write the records into Cassandra table due to org.apache.nifi.serialization.MalformedRecordException: Successfully parsed a JSON object from input but failed to convert into a Record object with the given schema: org.apache.nifi.serialization.MalformedRecordException: Successfully parsed a JSON object from input but failed to convert into a Record object with the given schema
org.apache.nifi.serialization.MalformedRecordException: Successfully parsed a JSON object from input but failed to convert into a Record object with the given schema
        at org.apache.nifi.json.AbstractJsonRowRecordReader.nextRecord(AbstractJsonRowRecordReader.java:98)
        at org.apache.nifi.serialization.RecordReader.nextRecord(RecordReader.java:50)
        at org.apache.nifi.processors.cassandra.PutCassandraRecord.onTrigger(PutCassandraRecord.java:151)
        at org.apache.nifi.processor.AbstractProcessor.onTrigger(AbstractProcessor.java:27)
        at org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1162)
        at org.apache.nifi.controller.tasks.ConnectableTask.invoke(ConnectableTask.java:209)
        at org.apache.nifi.controller.scheduling.TimerDrivenSchedulingAgent$1.run(TimerDrivenSchedulingAgent.java:117)
        at org.apache.nifi.engine.FlowEngine$2.run(FlowEngine.java:110)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NumberFormatException: For input string: "2019-02-02T08:00:35.473"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Long.parseLong(Long.java:589)
        at java.lang.Long.parseLong(Long.java:631)
        at org.apache.nifi.serialization.record.util.DataTypeUtils.toTimestamp(DataTypeUtils.java:1057)
        at org.apache.nifi.serialization.record.util.DataTypeUtils.convertType(DataTypeUtils.java:156)
        at org.apache.nifi.serialization.record.util.DataTypeUtils.convertType(DataTypeUtils.java:120)
        at org.apache.nifi.json.JsonTreeRowRecordReader.convertField(JsonTreeRowRecordReader.java:170)
        at org.apache.nifi.json.JsonTreeRowRecordReader.convertJsonNodeToRecord(JsonTreeRowRecordReader.java:137)
        at org.apache.nifi.json.JsonTreeRowRecordReader.convertJsonNodeToRecord(JsonTreeRowRecordReader.java:83)
        at org.apache.nifi.json.JsonTreeRowRecordReader.convertJsonNodeToRecord(JsonTreeRowRecordReader.java:74)
        at org.apache.nifi.json.AbstractJsonRowRecordReader.nextRecord(AbstractJsonRowRecordReader.java:93)
        ... 14 common frames omitted
这些类型似乎都是正确的。
如果有任何帮助,我们将不胜感激。

问题是您试图插入时间戳字段,但没有指定日期的格式。具体情况如下:

如果输入数据是字符串,则尝试获取其格式字符串,如果格式字符串是有效的格式化程序,则使用它获取日期。如果未指定格式字符串或格式字符串无效,则NiFi将尝试使用
Long.parseLong
对其进行转换

您需要使用如下方式执行相应字段的显式强制转换:

toDate("yyyy-MM-dd'T'hh:mm:ss")

问题是您试图插入时间戳字段,但没有指定日期的格式。具体情况如下:

如果输入数据是字符串,则尝试获取其格式字符串,如果格式字符串是有效的格式化程序,则使用它获取日期。如果未指定格式字符串或格式字符串无效,则NiFi将尝试使用
Long.parseLong
对其进行转换

您需要使用如下方式执行相应字段的显式强制转换:

toDate("yyyy-MM-dd'T'hh:mm:ss")

最后,我将datetime转换为一个历元时间戳,并将其转换为毫秒,然后将其转换为long,以便它与我的Avro模式一起工作

ts = datetime.datetime.strptime(strippedTime, '%Y-%m-%d %H:%M:%S.%f')
epoch = datetime.datetime(1970,1,1)
timestamp = long((ts-epoch).total_seconds()*1000)
fields['ts'] = timestamp

最后,我将datetime转换为一个历元时间戳,并将其转换为毫秒,然后将其转换为long,以便它与我的Avro模式一起工作

ts = datetime.datetime.strptime(strippedTime, '%Y-%m-%d %H:%M:%S.%f')
epoch = datetime.datetime(1970,1,1)
timestamp = long((ts-epoch).total_seconds()*1000)
fields['ts'] = timestamp

您使用的是哪一版本的NiFi?我使用的是1.9.2您使用的是哪一版本的NiFi?我使用的是1.9.2Hi@Alex,我尝试使用NiFi ExecuteScript处理器强制转换到日期时间,但当我尝试使用以下命令将流文件写入输出流:def process(self,inputStream,outputstream):text=IOUtils.toString(inputStream,StandardCharsets.UTF_8)obj=json.loads(text)fields=obj['fields']simpleTime=fields[''u time']ts=datetime.strptime(strippedTime,%Y-%m-%d%H:%m:%S.%f')fields['ts']=ts out=json.dumps(fields)我得到的类型错误:datetime.datetime(2019、2、2、8、0、35、473000)我也尝试过“out=JSON.dumps(fields,indent=4,sort_keys=True,default=str)”所以它可以被正确地序列化…但随后我遇到了无法转换为RecordHi@Alex的原始问题,我尝试使用NiFi ExecuteScript处理器转换为datetime,但当我尝试使用:def process(self,inputStream,outputstream):text=IOUtils.toString将流文件写入输出流时(inputStream,StandardCharsets.UTF_8)obj=json.loads(text)fields=obj['fields']simpleTime=fields[''u time']ts=datetime.strptime(strippedTime,%Y-%m-%d%H:%m:%S.%f')fields['ts']=ts out=json.dumps(fields)我得到的类型错误:datetime.datetime(2019、2、2、8、0、35、473000)我也尝试过“out=JSON.dumps(fields,indent=4,sort_keys=True,default=str)”,这样它就可以被正确地序列化了……但随后我遇到了无法转换为记录的原始问题