【问题标题】:Unableto to upload local csv file to BigQuery using Java API无法使用 Java API 将本地 csv 文件上传到 BigQuery
【发布时间】:2012-12-27 10:13:50
【问题描述】:

我尝试使用 java api 将本地 csv 文件导入 BigQuery。

但我没能做到。

如果您理解我在下面的代码中的错误,请告诉我...

        TableSchema schema = new TableSchema();
        ArrayList<TableFieldSchema> fields = new ArrayList<TableFieldSchema>();
        fields.add(new TableFieldSchema().setName("nn").setType("String"));
        fields.add(new TableFieldSchema().setName("gg").setType("String"));
        fields.add(new TableFieldSchema().setName("uu").setType("String"));
        schema.setFields(fields);

        TableReference destTable = new TableReference();
        destTable.setProjectId(projectId);
        destTable.setDatasetId(datasetId);
        destTable.setTableId("testUploads_fromJava");

        FileContent content = new FileContent("application/octet-stream", new File(csv));

        Job job = new Job();
        JobConfiguration config = new JobConfiguration();
        JobConfigurationLoad configLoad = new JobConfigurationLoad();

        configLoad.setSchema(schema);
        configLoad.setDestinationTable(destTable);

        config.setLoad(configLoad);
        job.setConfiguration(config);

        Insert insert = bigquery.jobs().insert(projectId, job, content);
        insert.setProjectId(projectId);
        JobReference jobRef = insert.execute().getJobReference();

“JobReference jobRef = insert.execute().getJobReference();”发生错误。

这是错误代码。

java.lang.NullPointerException
at java.net.URI$Parser.parse(URI.java:3004)
at java.net.URI.<init>(URI.java:577)
at com.google.api.client.http.GenericUrl.<init>(GenericUrl.java:100)
at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:269)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:408)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:328)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:449)
at bigquery.GettingBigQueryResult.loadLocalCSVtoBQ(GettingBigQueryResult.java:117)
at main.GetBQData.main(GetBQData.java:70)

谢谢。

【问题讨论】:

  • 确保本地文件的 URI 正确。看起来 NPE 来自 URI。

标签: java csv upload google-bigquery


【解决方案1】:

@greeness谢谢你的建议。

我搞错了设置方案。 我修改代码,因为方案定义是从 json 加载的。

正确的代码如下。

TableSchema schema = new TableSchema();
        schema.setFields(new ArrayList<TableFieldSchema>());
        JacksonFactory JACKSON = new JacksonFactory();
        JACKSON.createJsonParser(new FileInputStream("schema.json"))
        .parseArrayAndClose(schema.getFields(), TableFieldSchema.class, null);
        schema.setFactory(JACKSON);

        TableReference destTable = new TableReference();
        destTable.setProjectId(projectId);
        destTable.setDatasetId(datasetId);
        destTable.setTableId(tableId);

        FileContent content = new FileContent("application/octet-stream", new File(csv));

        Job job = new Job();
        JobConfiguration config = new JobConfiguration();
        JobConfigurationLoad configLoad = new JobConfigurationLoad();

        configLoad.setSchema(schema);
        configLoad.setDestinationTable(destTable);

        configLoad.setEncoding("UTF-8");
        configLoad.setCreateDisposition("CREATE_IF_NEEDED");

        config.setLoad(configLoad);
        job.setConfiguration(config);

        Insert insert = bigquery.jobs().insert(projectId, job, content);
        insert.setProjectId(projectId);
        JobReference jobRef = insert.execute().getJobReference();
        String jobId = jobRef.getJobId();

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-16
    • 2014-07-04
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 2021-07-20
    • 2019-10-25
    • 1970-01-01
    相关资源
    最近更新 更多