BigQuery Select 数据基于开始索引和最后索引
BigQuery Select data based on Start Index and Last Index
我正在使用以下代码从 BigQuery 获取数据。
public class BQTEST {
public static void main(String... args) throws Exception {
String datasetName = "mydataset";
String tableName = "mytable";
String projectId = "gcs";
String query =
"SELECT id, " +
"qtr, " +
"sales, " +
"year " +
"FROM `gcs.mydataset.mytable` " +
;
BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId(projectId)
.setCredentials(
ServiceAccountCredentials.fromStream(new
FileInputStream("20a3c78f8388.json"))
)
.build().getService();
TableId tableId = TableId.of(projectId, datasetName, tableName);
QueryJobConfiguration queryConfig = QueryJobConfiguration
.newBuilder(query)
.build();
try {
bigquery.query(queryConfig);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
TableResult results = bigquery.listTableData(
tableId,
BigQuery.TableDataListOption.pageSize(1),
BigQuery.TableDataListOption.startIndex(5)
);
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());
}
}
}
BigQuery.TableDataListOption.startIndex(5)
-- 这将有助于从第 5 个索引读取数据,索引从 0 开始。
但我想分块读取数据,例如一个进程中的前 10 条记录,然后是另一个进程中的下 20 条记录等。
尝试在并行进程中从非常大的 table 中读取数据。这是一个 Truncate/Load table。所以没有分区和日期范围来读取数据。
我找不到在任何 BigQuery 方法中提供 LAST_INDEX
值的方法。
有人可以帮忙吗?
没有 LAST_INDEX
分页方法,您可以查看 documentation。
关于您的要求:
I want to read data in chunks like the first 10 records in one process, then the next 20 records in another process and etc.,
使用 python 你可以使用一些参数作为 max_results
和 start_index
来执行它,但在 java 中唯一的方法是对你的查询进行分页,并且为每个过程更改它。因此,对于并行的每个进程,您将有不同的查询。
因此,每个进程都必须:
- 按某些字段(或所有字段)排序以保证每次查询都会return数据以相同的顺序
- 使用
limit
和 offset
分页:
即:
String query = "SELECT id, qtr, sales, year FROM `gcs.mydataset.mytable` " +
"ORDER BY id, qtr, sales, year " +
"LIMIT 10 " +
"OFFSET " + String.valueOf(process_number * 10)
;
进程0会有0-9行(limit 10 offset 0);
进程 1 将有第 10-19 行(限制 10 偏移量 10)
我正在使用以下代码从 BigQuery 获取数据。
public class BQTEST {
public static void main(String... args) throws Exception {
String datasetName = "mydataset";
String tableName = "mytable";
String projectId = "gcs";
String query =
"SELECT id, " +
"qtr, " +
"sales, " +
"year " +
"FROM `gcs.mydataset.mytable` " +
;
BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId(projectId)
.setCredentials(
ServiceAccountCredentials.fromStream(new
FileInputStream("20a3c78f8388.json"))
)
.build().getService();
TableId tableId = TableId.of(projectId, datasetName, tableName);
QueryJobConfiguration queryConfig = QueryJobConfiguration
.newBuilder(query)
.build();
try {
bigquery.query(queryConfig);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
TableResult results = bigquery.listTableData(
tableId,
BigQuery.TableDataListOption.pageSize(1),
BigQuery.TableDataListOption.startIndex(5)
);
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());
}
}
}
BigQuery.TableDataListOption.startIndex(5)
-- 这将有助于从第 5 个索引读取数据,索引从 0 开始。
但我想分块读取数据,例如一个进程中的前 10 条记录,然后是另一个进程中的下 20 条记录等。
尝试在并行进程中从非常大的 table 中读取数据。这是一个 Truncate/Load table。所以没有分区和日期范围来读取数据。
我找不到在任何 BigQuery 方法中提供 LAST_INDEX
值的方法。
有人可以帮忙吗?
没有 LAST_INDEX
分页方法,您可以查看 documentation。
关于您的要求:
I want to read data in chunks like the first 10 records in one process, then the next 20 records in another process and etc.,
使用 python 你可以使用一些参数作为 max_results
和 start_index
来执行它,但在 java 中唯一的方法是对你的查询进行分页,并且为每个过程更改它。因此,对于并行的每个进程,您将有不同的查询。
因此,每个进程都必须:
- 按某些字段(或所有字段)排序以保证每次查询都会return数据以相同的顺序
- 使用
limit
和offset
分页:
即:
String query = "SELECT id, qtr, sales, year FROM `gcs.mydataset.mytable` " +
"ORDER BY id, qtr, sales, year " +
"LIMIT 10 " +
"OFFSET " + String.valueOf(process_number * 10)
;
进程0会有0-9行(limit 10 offset 0);
进程 1 将有第 10-19 行(限制 10 偏移量 10)