2

我想将数据从Google Cloud Storage上传到BigQuery,但我找不到任何描述如何执行此操作的Java示例代码。有人请给我一些暗示,如何做到这一点?使用Java将数据从Google Cloud Storage加载到BigQuery

我实际上想做的是将数据从Google App Engine表格传输到BigQuery(并且每天同步),以便我可以进行一些分析。我在Google App Engine中使用Google Cloud Storage Service将新记录写入Google Cloud Storage中的文件,唯一缺失的部分是将数据追加到BigQuery中的表格(或者为第一次写入创建一个新表格)。无可否认,我可以使用BigQuery浏览器工具手动上传/附加数据,但我希望它是自动的,否则我需要每天手动进行。

回答

4

我不知道任何用于将表格从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

非常感谢您的代码。我试了一下,效果很好。为了让这个片段对更多人有用,这里有一些小小的错别字,如果你可以稍微修改一下,那将会很棒。 1.第3行“JobConfigurationQLoad”应该是“JobConfigurationLoad”; 2. Jobs.insert()函数需要两个参数,第一个参数是一个String(尽管放入的内容无关紧要); 3.在最后一行代码中,我认为你的意思是“JobReference jobRef”而不是“jobId”。再次感谢! –

+0

代码已根据您建议的更改进行更新。感谢您的反馈。 –

相关问题