有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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


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

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

任何帮助对我来说都是一次飞跃。请让我知道,如果我的要求是明确的在这里

谢谢


共 (2) 个答案

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