【问题标题】:Uploading JSON files into BigQuery from local drive using Java使用 Java 将 JSON 文件从本地驱动器上传到 BigQuery
【发布时间】:2014-11-16 04:41:32
【问题描述】:

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

  1. 从本地驱动器中读取包含 JSON 数据换行符分隔的文件
  2. 在 BigQuery 中生成新表
  3. 实时从文件中读取 JSON 生成表架构
  4. 将其上传到新生成的 BigQuery 表中。

任何帮助对我来说都是一个飞跃。如果我的要求在这里明确,请告诉我!

谢谢。

【问题讨论】:

    标签: java json google-bigquery


    【解决方案1】:
    【解决方案2】:
    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();    
      }
    

    【讨论】:

    • 读取 JSON 文件 => Path JSONpath = Paths.get("--------------------/data.json"); JSON 文件包含一个对象应该在一行中。不要用数组大括号括起来。例如 {"id": 1,"name": "bigquery"}。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2013-04-20
    • 2014-06-25
    • 2016-12-28
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多