您好
我是Google BigQuery的新手
搜索示例JAVA代码,该代码将从我的本地驱动器中获取JSON文件并将上传到BigQuery。在此过程中,代码应该:
任何帮助对我来说都是一个飞跃。如果我的要求在这里明确,请告诉我!
感谢。
答案 0 :(得分:2)
请遵循以下:
答案 1 :(得分:0)
public long writeJSONFileToTable(String datasetName, String tableName, Path JSONPath, String location)
throws IOException, InterruptedException, TimeoutException {
LoadStatistics stats = null;
try {
//[START bigquery_load_from_file]
TableId tableId = TableId.of(datasetName, tableName);
//WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build();
WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.json()).build();
// The location must be specified; other fields can be auto-detected.
JobId jobId = JobId.newBuilder().setLocation(location).build();
TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration);
// Write data to writer
try (OutputStream stream = Channels.newOutputStream(writer)) {
Files.copy(JSONPath, stream);
}
// Get load job
Job job = writer.getJob();
job = job.waitFor();
stats = job.getStatistics();
System.out.println("State: " + job.getStatus().getState());
System.out.println(stats.getOutputRows());
// [END bigquery_load_from_file]
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return stats.getOutputRows();
}