【发布时间】:2022-01-27 06:22:06
【问题描述】:
我正在使用以下代码行从 Google Bigquery 获取数据。
public class BQTEST {
public static void main(String... args) throws Exception {
String datasetName = "mydataset";
String tableName = "mytable";
String projectId = "id-gcs";
String query =
"SELECT id, " +
"qtr, " +
"sales, " +
"year " +
"FROM `id-gcs.mydataset.mytable` " +
"where MOD(id,2) = 0";
BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId(projectId)
.setCredentials(
ServiceAccountCredentials.fromStream(new
FileInputStream("gcs.json"))
)
.build().getService();
TableId tableId = TableId.of(projectId, datasetName, tableName);
QueryJobConfiguration queryConfig = QueryJobConfiguration
.newBuilder(query)
.setPriority(QueryJobConfiguration.Priority.BATCH)
.build();
try {
bigquery.query(queryConfig);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
TableResult results = bigquery.listTableData(
tableId,
BigQuery.TableDataListOption.pageSize(1)
);
for (FieldValueList row : results.iterateAll()) {
System.out.printf(
"ID: %s qtr: %s sales: %s year: %s\n", row.get(0).getValue(), row.get(1).getValue(), row.get(2).getValue(), row.get(3).getValue());
}
}
}
我在源表中有 12 条记录,起始 id 值从 1、2、3...12 开始。由于我在 ID 上应用了 Mod,结果集的 id 值应为 2,4,6,8,10,12。
相反,它将整个数据作为结果集返回。
因此,where 子句中的条件不适用。
寻求帮助。
【问题讨论】:
-
过滤器是什么意思?是页面大小吗?还是其他过滤器?你能举一个失败的例子吗?以及 IDE 打印的错误?
-
由于我无法添加更多代码行,因此编辑了我的帖子。请检查。
-
它可以在 BigQuery 控制台上运行吗?
-
是的。该查询在 BigQuery 控制台中运行。 “查询完成(经过 0.2 秒,已处理 312 B)”
-
在执行变量之前,能否打印出变量“query”的值并在 BigQuery 控制台中执行?
标签: java google-cloud-platform google-bigquery gcs