我不知道任何用于将表格从Google Cloud Storage加载到BigQuery的Java示例。这就是说,如果你遵循运行的查询工作here的说明,您可以用如下因素,而不是运行负载工作:
Job job = new Job();
JobConfiguration config = new JobConfiguration();
JobConfigurationLoad loadConfig = new JobConfigurationLoad();
config.setLoad(loadConfig);
job.setConfiguration(config);
// Set where you are importing from (i.e. the Google Cloud Storage paths).
List<String> sources = new ArrayList<String>();
sources.add("gs://bucket/csv_to_load.csv");
loadConfig.setSourceUris(sources);
// Describe the resulting table you are importing to:
TableReference tableRef = new TableReference();
tableRef.setDatasetId("myDataset");
tableRef.setTableId("myTable");
tableRef.setProjectId(projectId);
loadConfig.setDestinationTable(tableRef);
List<TableFieldSchema> fields = new ArrayList<TableFieldSchema>();
TableFieldSchema fieldFoo = new TableFieldSchema();
fieldFoo.setName("foo");
fieldFoo.setType("string");
TableFieldSchema fieldBar = new TableFieldSchema();
fieldBar.setName("bar");
fieldBar.setType("integer");
fields.add(fieldFoo);
fields.add(fieldBar);
TableSchema schema = new TableSchema();
schema.setFields(fields);
loadConfig.setSchema(schema);
// Also set custom delimiter or header rows to skip here....
// [not shown].
Insert insert = bigquery.jobs().insert(projectId, job);
insert.setProjectId(projectId);
JobReference jobRef = insert.execute().getJobReference();
// ... see rest of codelab for waiting for job to complete.
对于负载配置对象的更多信息,请参阅的javadoc here。
非常感谢您的代码。我试了一下,效果很好。为了让这个片段对更多人有用,这里有一些小小的错别字,如果你可以稍微修改一下,那将会很棒。 1.第3行“JobConfigurationQLoad”应该是“JobConfigurationLoad”; 2. Jobs.insert()函数需要两个参数,第一个参数是一个String(尽管放入的内容无关紧要); 3.在最后一行代码中,我认为你的意思是“JobReference jobRef”而不是“jobId”。再次感谢! –
代码已根据您建议的更改进行更新。感谢您的反馈。 –