Google bigquery Java-在google大查询中一次插入一行?
我正在创建一个应用程序,每当用户单击一篇文章时,我都需要捕获文章数据和用户数据,以计算每篇文章的覆盖范围,并能够对覆盖的数据进行分析 我的应用程序在应用程序引擎上 当我检查BQ中插入的文档时,大多数都指向以作业或流形式的批量插入 问题:Google bigquery Java-在google大查询中一次插入一行?,google-bigquery,Google Bigquery,我正在创建一个应用程序,每当用户单击一篇文章时,我都需要捕获文章数据和用户数据,以计算每篇文章的覆盖范围,并能够对覆盖的数据进行分析 我的应用程序在应用程序引擎上 当我检查BQ中插入的文档时,大多数都指向以作业或流形式的批量插入 问题: 每次启动用户操作时,每次在大查询中插入一行是否是一种好的做法?如果是这样,您能给我指出一些Java代码来有效地实现这一点吗 加载作业和DML查询的数量有限制(每天1000个),因此您需要将其用于此类应用程序。请注意,流式插入不同于从Java流加载数据 Table
每次启动用户操作时,每次在大查询中插入一行是否是一种好的做法?如果是这样,您能给我指出一些Java代码来有效地实现这一点吗 加载作业和DML查询的数量有限制(每天1000个),因此您需要将其用于此类应用程序。请注意,流式插入不同于从Java流加载数据
TableId tableId = TableId.of(datasetName, tableName);
// Values of the row to insert
Map<String, Object> rowContent = new HashMap<>();
rowContent.put("booleanField", true);
// Bytes are passed in base64
rowContent.put("bytesField", "Cg0NDg0="); // 0xA, 0xD, 0xD, 0xE, 0xD in base64
// Records are passed as a map
Map<String, Object> recordsContent = new HashMap<>();
recordsContent.put("stringField", "Hello, World!");
rowContent.put("recordField", recordsContent);
InsertAllResponse response =
bigquery.insertAll(
InsertAllRequest.newBuilder(tableId)
.addRow("rowId", rowContent)
// More rows can be added in the same RPC by invoking .addRow() on the builder
.build());
if (response.hasErrors()) {
// If any of the insertions failed, this lets you inspect the errors
for (Entry<Long, List<BigQueryError>> entry : response.getInsertErrors().entrySet()) {
// inspect row error
}
}
TableId TableId=TableId.of(datasetName,tableName);
//要插入的行的值
Map rowContent=newhashmap();
rowContent.put(“booleanField”,true);
//字节在base64中传递
rowContent.put(“bytesField”,“Cg0NDg0=”);//base64中的0xA、0xD、0xD、0xE、0xD
//记录作为映射传递
Map recordsContent=newhashmap();
recordsContent.put(“stringField”,“你好,世界!”);
rowContent.put(“记录字段”,记录内容);
插入应答=
bigquery.insertAll(
InsertAllRequest.newBuilder(表ID)
.addRow(“rowId”,rowContent)
//通过调用生成器上的.addRow()可以在同一RPC中添加更多行
.build());
if(response.hasErrors()){
//如果任何插入失败,这将允许您检查错误
for(条目:response.getInsertErrors().entrySet()){
//检查行错误
}
}
(摘自上的示例)
请特别注意,失败的插入并不总是引发异常。您还必须检查响应对象是否存在错误
每次启动用户操作时,每次在大查询中插入一行是否是一种好的做法
是的,将事件流传输到BigQuery进行分析是非常典型的。如果将多个事件缓冲到同一个向BigQuery发送的流式插入请求中,则可以获得更好的性能,但每次只支持一行。谷歌示例的简化版本
Map<String, Object> row1Data = new HashMap<>();
row1Data.put("booleanField", true);
row1Data.put("stringField", "myString");
Map<String, Object> row2Data = new HashMap<>();
row2Data.put("booleanField", false);
row2Data.put("stringField", "myOtherString");
TableId tableId = TableId.of("myDatasetName", "myTableName");
InsertAllResponse response =
bigQuery.insertAll(
InsertAllRequest.newBuilder(tableId)
.addRow("row1Id", row1Data)
.addRow("row2Id", row2Data)
.build());
if (response.hasErrors()) {
// If any of the insertions failed, this lets you inspect the errors
for (Map.Entry<Long, List<BigQueryError>> entry : response.getInsertErrors().entrySet()) {
// inspect row error
}
}
Map row1Data=newhashmap();
row1Data.put(“booleanField”,true);
row1Data.put(“stringField”、“myString”);
Map row2Data=newhashmap();
row2Data.put(“booleanField”,false);
row2Data.put(“stringField”、“myOtherString”);
TableId TableId=TableId.of(“myDatasetName”、“myTableName”);
插入应答=
bigQuery.insertAll(
InsertAllRequest.newBuilder(表ID)
.addRow(“row1Id”,row1Data)
.addRow(“row2Id”,row2Data)
.build());
if(response.hasErrors()){
//如果任何插入失败,这将允许您检查错误
for(Map.Entry:response.getInsertErrors().entrySet()){
//检查行错误
}
}