![](/img/trans.png)
[英]Google BigQuery: loading data from a local CSV file using the Java API with custom field delimiter
[英]Unableto to upload local csv file to BigQuery using Java API
我嘗試使用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)
謝謝。
@ 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();
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.