我正在尝试从Scala应用程序将数据流式传输到BQ。查看Streaming Data Into BigQuery中列出的示例,我发现数据需要使用Map<String, Object>
作为TableDataInsertAllRequest.Rows().setJson()
传入。
Map<String, Object>
传递数据?如果不是,有什么理由吗?答案 0 :(得分:1)
不幸的是,我们(或任何Google Cloud Platform)API上生成的库不支持直接写出请求正文。这有助于确保请求的有效性。也就是说,客户端库前面有积极的工作,辅助方法似乎是一个合理的请求。对于上述验证目的,开销可能仍然存在(解析为客户端表示),但客户端接口对于您的方案来说会稍微简单一些。
我会传递你的请求。与此同时,这个问题的答案提到了一个图书馆似乎可以简化你的翻译工作:
答案 1 :(得分:1)
我认为您应该可以使用gcloud-java通过TableDataWriteChannel中的BigQuery api流式传输json内容。
这意味着它应该在没有gcloud-java(并且直接使用api-client)的情况下也可行,尽管你可能需要重复一些代码 图书馆正在为你做。
我强烈建议您查看gcloud-java并随意添加feature request以支持instertAll操作中的json内容。
答案 2 :(得分:1)
我还建议您查看gcloud-java中的BigQuery api。在gcloud-java中,您可以使用TableDataWriteChannel将数据流式传输到BigQuery表。
请参阅以下示例(其中JSON_CONTENT
是一个JSON字符串):
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
TableId tableId = TableId.of("dataset", "table");
LoadConfiguration configuration = LoadConfiguration.builder(tableId)
.formatOptions(FormatOptions.json())
.build();
try (TableDataWriteChannel channel = bigquery.writer(configuration)) {
channel.write(
ByteBuffer.wrap(JSON_CONTENT.getBytes(StandardCharsets.UTF_8)));
} catch (IOException e) {
// handle exception
}
TableDataWriteChannel
使用resumable upload将数据流式传输到BigQuery表,这使得它更适合大数据大文件。
TableDataWriteChannel
也可用于流式传输本地文件:
int chunkSize = 8 * 256 * 1024;
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
LoadConfiguration configuration = LoadConfiguration.builder(tableId)
.formatOptions(FormatOptions.json())
.build();
try (FileChannel fileChannel = FileChannel.open(Paths.get("file.json"))) {
WriteChannel writeChannel = bigquery.writer(configuration);
long position = 0;
long written = fileChannel.transferTo(position, chunkSize, writeChannel);
while (written > 0) {
position += written;
written = fileChannel.transferTo(position, chunkSize, writeChannel);
}
writeChannel.close();
}
有关gcloud-java-bigquery的其他示例,您可以查看BigQueryExample。
答案 3 :(得分:0)
这是流式传输数据的唯一方法。大型文件documented here有批量加载,但为此你需要将文件移动到GCS并从那里发出导入作业。
那么,答案是通常BQ连接器库处理转换,至少它是如何处理Java和PHP的,所以不需要字符串就需要传递对象。