如何从Java客户端创建BigQuery数据集和表/模式(没有CSV文件)

我认为这里第200行的方法是相关的(编辑:我需要在行插入insertReq = bigquery.jobs()。insert(PROJECT_ID,insertJob);)中添加一个参数,但它不起作用。 我得到“加载配置必须至少指定一个源URI”

我尝试过以下方法:

TableSchema schema = new TableSchema(); List tableFieldSchema = new ArrayList(); TableFieldSchema schemaEntry = new TableFieldSchema(); schemaEntry.setName(myFirstFieldName); schemaEntry.setType("STRING"); tableFieldSchema.add(schemaEntry); schema.setFields(tableFieldSchema); Table table = new Table(); table.setSchema(schema); table.setId(tableName); table.setCreationTime(System.currentTimeMillis()); table.setKind("bigquery#table"); try { bigquery.tables().insert(PROJECT_ID, DATASET_ID, table).execute(); } catch (IOException e) { } 

但我收到错误Required parameter is missing

好的,基于Jordan Tigani的想法,这里是Java代码,可以使用Google Java API Client在BigQuery中创建一个空白表:

 TableSchema schema = new TableSchema(); List tableFieldSchema = new ArrayList(); TableFieldSchema schemaEntry = new TableFieldSchema(); schemaEntry.setName(myFirstFieldName); schemaEntry.setType("STRING"); tableFieldSchema.add(schemaEntry); schema.setFields(tableFieldSchema); Table table = new Table(); table.setSchema(schema); TableReference tableRef = new TableReference(); tableRef.setDatasetId(DATASET_ID); tableRef.setProjectId(PROJECT_ID); tableRef.setTableId(tableId); table.setTableReference(tableRef); try { bigquery.tables().insert(PROJECT_ID, DATASET_ID, table).execute(); } catch (IOException e) { } 

创建数据集(在创建表之前)

  Dataset dataset = new Dataset(); DatasetReference datasetRef = new DatasetReference(); datasetRef.setProjectId(PROJECT_ID); datasetRef.setDatasetId(DATASET_ID); dataset.setDatasetReference(datasetRef); try { bigquery.datasets().insert(PROJECT_ID, dataset).execute(); } catch (IOException e) { } 

尝试在表上设置项目ID和数据集id(我意识到它似乎是多余的,因为你在insert()操作中指定它们,但这是REST的一个怪癖…项目和数据集是URL的一部分,但它们也是资源的一部分。

从原始HTTP api级别,以下工作:

 https://www.googleapis.com/bigquery/v2/projects/myproject/datasets/mydataset/tables?alt=json {"tableReference": {"tableId": "dfdlkfjx", "projectId": "myproject", "datasetId": "mydataset"}, "schema": {"fields": [{"name": "a", "type": "STRING"}]}}