How to load data in nested array using dataflow

自作多情 提交于 2020-08-06 05:18:08

问题


I am trying to load the data into below table. I am able to load the data in "array_data". But how to load the data in nested array "inside_array".I have tried the commented part to load the data in inside_array array but it did not work. enter image description here

Here is my code.- Pipeline p = Pipeline.create(options);

    org.apache.beam.sdk.values.PCollection<TableRow> output = p.apply(org.apache.beam.sdk.transforms.Create.of("temp"))
            .apply("O/P",ParDo.of(new DoFn<String, TableRow>() {
                /**
                 * 
                 */
                private static final long serialVersionUID = 307542945272055650L;

                @ProcessElement
                public void processElemet(ProcessContext c) {
                    TableRow row = new TableRow();
                    row.set("name","Jack");
                    row.set("phone","9874563210");
                    
                    TableRow ip = new TableRow().set("address", "M G Road").set("email","abc@gmail.com");
                    TableRow ip1 = new TableRow().set("address","F C Road").set("email","xyz@gmail.com");
                                
                    java.util.List<TableRow> metadata = new ArrayList<TableRow>();
                    metadata.add(ip);
                    metadata.add(ip1);
            
                    row.set("array_data",metadata);
                    
                    LOG.info("O/P:"+row);       
                    c.output(row);
                }}));
    
    output.apply("Write to table",BigQueryIO.writeTableRows().withoutValidation().to("AA.nested_array")
            .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_NEVER)
            .withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_TRUNCATE));
    
    p.run();
    

Anyone has any clue or suggestion.Thanks in advance.


回答1:


To Handle the nested array using dataflow create a seprate List and add it into your main array of tablerow.

Here I tried this way and I got the expected output.

Pipeline p = Pipeline.create(options); org.apache.beam.sdk.values.PCollection output = p.apply(org.apache.beam.sdk.transforms.Create.of("temp")) .apply("O/P",ParDo.of(new DoFn<String, TableRow>() {

                @ProcessElement
                public void processElemet(ProcessContext c) {
                    TableRow row = new TableRow();
                    row.set("name","Jack");
                    row.set("phone","9874563210");
                    
                    List<TableRow> listDest   = new ArrayList<>();
                    TableRow t=new TableRow().set("detail1","one" ).set("detail2", "two");
                    TableRow t1=new TableRow().set("detail1","three" ).set("detail2", "four");
                    listDest.add(t);
                    listDest.add(t1);
                            
                    
                    TableRow ip = new TableRow().set("address", "M G Road").set("email","abc@gmail.com").set("inside_array", listDest);
                    TableRow ip1 = new TableRow().set("address","F C Road").set("email","xyz@gmail.com").set("inside_array", listDest);
    
                    java.util.List<TableRow> metadata = new ArrayList<TableRow>();
                    metadata.add(ip);
                    metadata.add(ip1);
                
                    row.set("array_data",metadata);
                    LOG.info("O/P:"+row);       
                    c.output(row);
                }}));

Adding the image of table with data as well.

hope It will helpful if anyone is working on the same kind of table.



来源:https://stackoverflow.com/questions/62625774/how-to-load-data-in-nested-array-using-dataflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!