When I run a query in BigQuery Web UI, the results are displayed in a table where both name and type of each field are known (even when a field is a result of COUNT(), AVG(), ... operation, type of field is known, of course).
The results can be then directly exported as a table/json/csv.
My question is, when I retrieve query results in my java project, e.g. with a query:
String query = "SELECT nationality, COUNT(DISTINCT personID) AS population
FROM Dataset.Table
GROUP BY nationality";
PCollection<TableRow> result = p.apply(BigQueryIO.Read.fromQuery(query));
... is it possible to obtain the schema of TableRow in result
PCollection, without explicitly defining it?
I think it must be possible, since it's possible with the same query when using BigQuery Web UI.
But I can't figure out how to do it ...
TableSchema schema = // function of PCollection<TableRow> result ?
result.apply(BigQueryIO.Write
.named("Write Results Table")
.to(getTableReference(tableName))
.withSchema(schema));
That way query results could be always automatically exported/saved into a new table (only the table name then needs to be explicitly provided).
Any ideas? Any help would be appreciated :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…