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

时间:2014-01-17 10:44:51

标签: java google-bigquery

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

我尝试了以下内容:

    TableSchema schema = new TableSchema();
    List<TableFieldSchema> tableFieldSchema = new ArrayList<TableFieldSchema>();
    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

2 个答案:

答案 0 :(得分:16)

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

TableSchema schema = new TableSchema();
List<TableFieldSchema> tableFieldSchema = new ArrayList<TableFieldSchema>();
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) {
    }

答案 1 :(得分:5)

尝试在表上设置项目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"}]}}