简体   繁体   English

如何转换 PCollection<row> 在数据流 Apache 中使用 Java 束</row>

[英]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.

相关问题 如何转换 PCollection<row> 使用 Java 到数据流 Apache 中的 Integer</row> - How to convert PCollection<Row> to Integer in Dataflow Apache beam using Java 如何转换 PCollection<tablerow> 到个人收藏<row>在 Apache 梁?</row></tablerow> - How to convert PCollection<TableRow> to PCollection<Row> in Apache Beam? 如何从 PCollection 中提取信息<row>加入 apache 光束后?</row> - How to extract information from PCollection<Row> after a join in apache beam? 如何使用 Apache Beam 中的流输入 PCollection 请求 Redis 服务器? - How to request Redis server using a streaming input PCollection in Apache Beam? 如何区分两个 PCollection Apache Beam - How to diff two PCollection Apache Beam 如何在 PCollection 中组合数据 - Apache Beam - How to combine Data in PCollection - Apache beam 如何使用 Java 在 Apache Beam 中将 JSON 转换为 Parquet - How to convert JSON to Parquet in Apache Beam using Java 如何为 PCollection 设置编码器<List<String> &gt; 在 Apache Beam 中? - How do I set the coder for a PCollection<List<String>> in Apache Beam? 如何将 JSON Array 反序列化为 Apache beam PCollection<javaobject></javaobject> - How to deserialize JSON Array to Apache beam PCollection<javaObject> 如何在 Apache Beam / Google Dataflow 中使用 ParseJsons? - How to use ParseJsons in Apache Beam / Google Dataflow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM