使用Java从本地驱动器上载JSON文件到BigQuery

时间:2014-09-22 09:07:26

标签: java json google-bigquery

您好
我是Google BigQuery的新手 搜索示例JAVA代码,该代码将从我的本地驱动器中获取JSON文件并将上传到BigQuery。在此过程中,代码应该:

  1. 从包含JSON数据换行符的本地驱动器中的文件读取
  2. 在BigQuery中生成一个新表
  3. 生成从文件实时读取JSON的表模式
  4. 将其上传到新生成的BigQuery表中。
  5. 任何帮助对我来说都是一个飞跃。如果我的要求在这里明确,请告诉我!

    感谢。

2 个答案:

答案 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();    
  }