使用Java将数据从Google云端存储加载到BigQuery

Jac*_*Guo 3 google-app-engine google-cloud-storage google-bigquery

我想将数据从Google云端存储上传到BigQuery,但是找不到任何描述该操作方法的Java示例代码。有人可以给我一些如何做的提示吗?

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

Jor*_*ani 5

我不知道任何用于将表格从Google云端存储加载到BigQuery的Java示例。就是说,如果您按照此处的运行查询作业的说明进行操作,则可以按照以下方式运行加载作业:

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.
Run Code Online (Sandbox Code Playgroud)

有关负载配置对象的更多信息,请参见此处的javadoc 。