4
votes

I'm accessing BigQuery data through the java API and the new service Dataflow. I would have expect that the TableRow class had methods that:

  • return a list of TableCell instances (in case of a repeated field)
  • return a TableRow instance (in case of a nested field)
  • return a list of TableRow instances (in case of repeated nested fields)

But I've only find sample code using the get method to access a TableRow instance's fields which is not convenient as the output is an instance of the class Object.

The only solution I see for the moment is parsing the strings I get from the get method and build a TableRow object on my own but I was wondering if someone had a better solution to share?

Many Thanks.

3

3 Answers

3
votes

Finally I came to these conclusions:

  • If according to your schema you are expecting a repeated field, a get method applied to your TableRow instance will return an ArrayList instance.
  • If according to your schema you are expecting a record, a get method applied to your TableRow instance will return an instance of LinkedHashMap.
  • As you have guessed if according to your schema you are expecting a repeatead field of records, a get method applied to your TableRow instance will return an instance of ArrayList.

So you just have to downcast the result from the get method applied to your TableRow instance with one of the classes above and then you can make the most of their features (iterating & calling by name for instance).

2
votes

I had Repeated Record being returned, I accessed using following way:

TableRow atrs = (TableRow) row.get("sts");

                List<String> atrValue = (List<String>) atrs.get("value");
                if(atrValue.size()>0){                      
                    TableRow atrRow = new TableRow();
                    atrRow.set("id", "sts");
                    atrRow.set("value", atrValue.toArray());

                    if(atrs.get("sv") !=null){
                        List<Integer> atrSV = ((List<String>) atrs.get("sv"))
                                .stream()
                                .map(x -> Integer.parseInt(x))
                                .collect(Collectors.toList());
                        atrRow.set("sv", atrSV.toArray());
                    }
                    else
                        atrRow.set("sv", null);


                    atrList.add(atrRow);
                }
0
votes

TableRow has a getClassInfo() method. Does it help in any way? (haven't tried working with nested fields myself)