'How to convert PCollection<Row> to Integer in Dataflow Apache beam using Java

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 RowToInteger method

public class RowToLong extends DoFn<Row, Integer> {
    public static final Logger LOG = LoggerFactory.getLogger(RowToLong.class.getName());    

    private PCollectionView<Integer> outputCount;      

    @ProcessElement
    public void processElement(ProcessContext context) {    
        

        Integer total_count= Long.valueOf(context.element().getInt64("total_count"));          
          context.output(total_count);    
    }

}

error

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



Solution 1:[1]

String total_count  = select sum(cast(col1 as bigint)) as total_count from <table>
Integer total_count = context.element().getInt64("total_count");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elikill58