簡體   English   中英

如何轉換 PCollection<row> 在數據流 Apache 中使用 Java 束</row>

[英]How to convert PCollection<Row> to Long in Dataflow Apache beam using Java

當我們嘗試將 Pcollection 轉換為 Long 時,我們會遇到類型轉換異常。 請查看以下代碼。

錯誤

java.lang.Integer cannot be cast to java.lang.Long

創建集合

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());

詢問

String total_count  = select sum(cast(col1 as INT)) as total_count from <table>

轉換 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);    
    }

}

使用下面的代碼試試這個。

詢問

String total_count  = select sum(cast(col1 as bigint)) as total_count from <table>

將行轉換為方法

Long total_count = context.element().getInt64("total_count").longValue();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM