我想创建一个表,然后使用云函数写入BigQuery,但我不想复制表中的数据,所以我首先删除了表,然后在每次调用该函数时创建表.
public static void runCreateTable() {
Schema schema =
Schema.of(
Field.of("id", StandardSQLTypeName.INT64),
Field.of("name", StandardSQLTypeName.STRING));
createTable(DATASET, TABLE_NAME, schema);
}
public static void createTable(String datasetName, String tableName, Schema schema) {
try {
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
TableId tableId = TableId.of(datasetName, tableName);
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
bigquery.create(tableInfo);
System.out.println("Table created successfully");
} catch (BigQueryException e) {
System.out.println("Table was not created. \n" + e.toString());
}
}
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
if (bigquery.getTable(TableId.of(DATASET, TABLE_NAME)).delete()) {
runCreateTable();
TableRow row = new TableRow();
for (Map.Entry<String, Object> entry : campaign.entrySet()) {
row.set("id", entry.getKey()).set("name", entry.getValue());
bigquery.insertAll(InsertAllRequest.newBuilder(bigquery.getTable(TableId.of(DATASET, TABLE_NAME))).addRow(row).build());
}
}
100