Butter cup
Butter cup

Reputation: 66

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

Upvotes: 2

Views: 607

Answers (1)

Tank
Tank

Reputation: 31

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

Upvotes: 3

Related Questions