[英]How to convert PCollection<Row> to Long in Dataflow Apache beam using Java
When we try to convert the Pcollection to Long we are getting Type casting exception.当我们尝试将 Pcollection 转换为 Long 时,我们会遇到类型转换异常。 kindly have a look into below code.请查看以下代码。
error错误
java.lang.Integer cannot be cast to java.lang.Long
Creating the pcollection创建集合
PCollection<Row> count = pt.apply(SqlTransform.query(Constants.total_count));
PCollectionView<Long> outputCount = detail_count
.apply("Row to long",
ParDo.of(new RowToLong())).apply(View.asSingleton());
Query询问
String total_count = select sum(cast(col1 as INT)) as total_count from <table>
Converting RowTo method转换 RowTo 方法
public class RowToLong extends DoFn<Row, Long> {
public static final Logger LOG = LoggerFactory.getLogger(RowToLong.class.getName());
private PCollectionView<Long> outputCount;
@ProcessElement
public void processElement(ProcessContext context) {
// Long total_count=((Number)c.element().getInt64("total_count")).longValue();
Long total_count= Long.valueOf(context.element().getInt64("total_count"));
context.output(total_count);
}
}
Try this using below code.使用下面的代码试试这个。
Query询问
String total_count = select sum(cast(col1 as bigint)) as total_count from <table>
Converting row to method将行转换为方法
Long total_count = context.element().getInt64("total_count").longValue();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.