gpt4 book ai didi

用于列出表数据的 Java BigQuery API

转载 作者:行者123 更新时间:2023-11-30 06:07:02 25 4
gpt4 key购买 nike

我正在尝试使用 JAVA 列出来自 BigQuery 的表数据。但是我无法找到如何配置 API 以获得每次调用的最大行数?

public class QuickstartSample {
public static void main(String... args) throws Exception {
GoogleCredentials credentials;
File credentialsPath = new File("/Users/gaurang.shah/Downloads/fb3735b731b9.json"); // TODO: update to your key path.
FileInputStream serviceAccountStream = new FileInputStream(credentialsPath);
credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);

BigQuery bigquery = BigQueryOptions.newBuilder().
setCredentials(credentials).
setProjectId("bigquery-public-data").
build().
getService();

Dataset hacker_news = bigquery.getDataset("hacker_news");
Table comments = hacker_news.get("comments");
TableResult result = comments.list().;
for (FieldValueList row : result.iterateAll()) {
// do something with the row
System.out.println(row);
}
}
}

最佳答案

要限制行数,您可以使用带有 TableDataListOption.pageSize(n) 参数的 listTableData 方法。

以下示例返回 100 行作为结果:

String datasetName = "my_dataset_name";
String tableName = "my_table_name";
TableId tableIdObject = TableId.of(datasetName, tableName);

TableResult tableData =
bigquery.listTableData(tableIdObject, TableDataListOption.pageSize(100));
for (FieldValueList row : tableData.iterateAll()) {
// do something with the row
}

关于用于列出表数据的 Java BigQuery API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51090193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com