在 WSO2 流处理器中将字符串转换为双精度或浮点数问题
Converting string to double or float issue in WSO2 stream processor
我有一个 CSV 文件要加载到 WS02 流处理器中以模拟事件。我有一个名为 "Result" 的列。这是 CSV 文件中的字符串列。
在 WSO2 流处理器中,我尝试将传入的结果字符串转换为双精度或浮点数。但这不起作用。我还尝试以某种方式更改 CSV 文件。例如,我曾尝试使用 excel 中的 "format cells" 选项将其设置为双精度或小数,但这没有用。
下面是我在 WSO2 中尝试的代码。这是结果值的示例:159,321
它最多有 6 位数字。逗号前后最多 3 位数字。
define stream ResultStream(Nr String, DateTime String, Result String);
@sink(type='log', prefix='LOGGER')
define stream OutputStream(Nr int, DateTime String, Result double);
@info(name='Query')
from ResultStream
select convert(Nr, 'int') as Nr, DateTime, convert(Result, 'double') as Result
insert into OutputStream;
我收到以下错误:
java.lang.String cannot be cast to java.lang.Double. Hence, dropping event 'Event{timestamp=1570697706466, data=[18, 1-9-2010 08:06, 54,103], isExpired=false}' java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at org.wso2.siddhi.core.query.selector.attribute.aggregator.AvgAttributeAggregator$AvgAttributeAggregatorDouble.processAdd(AvgAttributeAggregator.java:162)
at org.wso2.siddhi.core.query.selector.attribute.aggregator.AvgAttributeAggregator.processAdd(AvgAttributeAggregator.java:102)
at org.wso2.siddhi.core.query.selector.attribute.aggregator.AttributeAggregator.process(AttributeAggregator.java:87)
at org.wso2.siddhi.core.query.selector.attribute.processor.executor.AggregationAttributeExecutor.execute(AggregationAttributeExecutor.java:40)
at org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor.process(AttributeProcessor.java:41)
at org.wso2.siddhi.core.query.selector.QuerySelector.processNoGroupBy(QuerySelector.java:136)
at org.wso2.siddhi.core.query.selector.QuerySelector.process(QuerySelector.java:94)
at org.wso2.siddhi.core.query.processor.stream.window.LengthWindowProcessor.process(LengthWindowProcessor.java:137)
at org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor.processEventChunk(WindowProcessor.java:65)
at org.wso2.siddhi.core.query.processor.stream.AbstractStreamProcessor.process(AbstractStreamProcessor.java:123)
at org.wso2.siddhi.core.query.input.ProcessStreamReceiver.processAndClear(ProcessStreamReceiver.java:187)
at org.wso2.siddhi.core.query.input.ProcessStreamReceiver.process(ProcessStreamReceiver.java:97)
at org.wso2.siddhi.core.query.input.ProcessStreamReceiver.receive(ProcessStreamReceiver.java:133)
at org.wso2.siddhi.core.stream.StreamJunction.sendEvent(StreamJunction.java:204)
at org.wso2.siddhi.core.stream.StreamJunction$Publisher.send(StreamJunction.java:414)
at org.wso2.siddhi.core.stream.input.InputDistributor.send(InputDistributor.java:34)
at org.wso2.siddhi.core.stream.input.InputEntryValve.send(InputEntryValve.java:44)
at org.wso2.siddhi.core.stream.input.InputHandler.send(InputHandler.java:73)
at org.wso2.carbon.siddhi.editor.core.internal.DebuggerEventStreamService.pushEvent(DebuggerEventStreamService.java:70)
at org.wso2.carbon.event.simulator.core.service.EventSimulator.eventSimulation(EventSimulator.java:320)
at org.wso2.carbon.event.simulator.core.service.EventSimulator.run(EventSimulator.java:451)
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)
您遇到的问题是由结果值中的逗号引起的。
您需要删除逗号才能使其成为有效的转换数字。
检查下面更新的 Siddhi 应用程序。我使用 str:replaceAll 方法删除了带有空字符串的 ','。
define stream ResultStream(Nr String, DateTime String, Result String);
@sink(type='log', prefix='LOGGER')
define stream OutputStream(Nr int, DateTime String, Result double);
@info(name='Query')
from ResultStream
select convert(Nr, 'int') as Nr, DateTime, convert(str:replaceAll(Result,',',''), 'double') as Result
insert into OutputStream;
我有一个 CSV 文件要加载到 WS02 流处理器中以模拟事件。我有一个名为 "Result" 的列。这是 CSV 文件中的字符串列。
在 WSO2 流处理器中,我尝试将传入的结果字符串转换为双精度或浮点数。但这不起作用。我还尝试以某种方式更改 CSV 文件。例如,我曾尝试使用 excel 中的 "format cells" 选项将其设置为双精度或小数,但这没有用。
下面是我在 WSO2 中尝试的代码。这是结果值的示例:159,321
它最多有 6 位数字。逗号前后最多 3 位数字。
define stream ResultStream(Nr String, DateTime String, Result String);
@sink(type='log', prefix='LOGGER')
define stream OutputStream(Nr int, DateTime String, Result double);
@info(name='Query')
from ResultStream
select convert(Nr, 'int') as Nr, DateTime, convert(Result, 'double') as Result
insert into OutputStream;
我收到以下错误:
java.lang.String cannot be cast to java.lang.Double. Hence, dropping event 'Event{timestamp=1570697706466, data=[18, 1-9-2010 08:06, 54,103], isExpired=false}' java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at org.wso2.siddhi.core.query.selector.attribute.aggregator.AvgAttributeAggregator$AvgAttributeAggregatorDouble.processAdd(AvgAttributeAggregator.java:162)
at org.wso2.siddhi.core.query.selector.attribute.aggregator.AvgAttributeAggregator.processAdd(AvgAttributeAggregator.java:102)
at org.wso2.siddhi.core.query.selector.attribute.aggregator.AttributeAggregator.process(AttributeAggregator.java:87)
at org.wso2.siddhi.core.query.selector.attribute.processor.executor.AggregationAttributeExecutor.execute(AggregationAttributeExecutor.java:40)
at org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor.process(AttributeProcessor.java:41)
at org.wso2.siddhi.core.query.selector.QuerySelector.processNoGroupBy(QuerySelector.java:136)
at org.wso2.siddhi.core.query.selector.QuerySelector.process(QuerySelector.java:94)
at org.wso2.siddhi.core.query.processor.stream.window.LengthWindowProcessor.process(LengthWindowProcessor.java:137)
at org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor.processEventChunk(WindowProcessor.java:65)
at org.wso2.siddhi.core.query.processor.stream.AbstractStreamProcessor.process(AbstractStreamProcessor.java:123)
at org.wso2.siddhi.core.query.input.ProcessStreamReceiver.processAndClear(ProcessStreamReceiver.java:187)
at org.wso2.siddhi.core.query.input.ProcessStreamReceiver.process(ProcessStreamReceiver.java:97)
at org.wso2.siddhi.core.query.input.ProcessStreamReceiver.receive(ProcessStreamReceiver.java:133)
at org.wso2.siddhi.core.stream.StreamJunction.sendEvent(StreamJunction.java:204)
at org.wso2.siddhi.core.stream.StreamJunction$Publisher.send(StreamJunction.java:414)
at org.wso2.siddhi.core.stream.input.InputDistributor.send(InputDistributor.java:34)
at org.wso2.siddhi.core.stream.input.InputEntryValve.send(InputEntryValve.java:44)
at org.wso2.siddhi.core.stream.input.InputHandler.send(InputHandler.java:73)
at org.wso2.carbon.siddhi.editor.core.internal.DebuggerEventStreamService.pushEvent(DebuggerEventStreamService.java:70)
at org.wso2.carbon.event.simulator.core.service.EventSimulator.eventSimulation(EventSimulator.java:320)
at org.wso2.carbon.event.simulator.core.service.EventSimulator.run(EventSimulator.java:451)
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)
您遇到的问题是由结果值中的逗号引起的。 您需要删除逗号才能使其成为有效的转换数字。
检查下面更新的 Siddhi 应用程序。我使用 str:replaceAll 方法删除了带有空字符串的 ','。
define stream ResultStream(Nr String, DateTime String, Result String);
@sink(type='log', prefix='LOGGER')
define stream OutputStream(Nr int, DateTime String, Result double);
@info(name='Query')
from ResultStream
select convert(Nr, 'int') as Nr, DateTime, convert(str:replaceAll(Result,',',''), 'double') as Result
insert into OutputStream;