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

如何从Java客户端(没有CSV文件)创建BigQuery数据集和表/模式,java,google-bigquery,Java,Google Bigquery,我认为从第200行开始的方法是相关的(编辑:我需要向该行添加一个参数) Insert insertReq=bigquery.jobs().Insert(项目ID,insertJob); )但它不起作用。我得到“加载配置必须至少指定一个源URI” 我尝试了以下方法: TableSchema schema = new TableSchema(); List<TableFieldSchema> tableFieldSchema = new ArrayList<Tabl

我认为从第200行开始的方法是相关的(编辑:我需要向该行添加一个参数) Insert insertReq=bigquery.jobs().Insert(项目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) {
    }
TableSchema schema=new TableSchema();
List tableFieldSchema=new ArrayList();
TableFieldSchema schemaEntry=新的TableFieldSchema();
schemaEntry.setName(myFirstFieldName);
schemaEntry.setType(“字符串”);
tableFieldSchema.add(schemaEntry);
schema.setFields(tableFieldSchema);
Table Table=新表();
表1.setSchema(模式);
table.setId(tableName);
table.setCreationTime(System.currentTimeMillis());
table.setKind(“bigquery#table”);
试一试{
bigquery.tables().insert(项目ID、数据集ID、表格).execute();
}捕获(IOE异常){
}

但是我得到一个错误
必需的参数丢失

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

根据Jordan Tigani的想法,下面是使用Google Java API客户端在BigQuery中创建空白表的Java代码:

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) {
}

您看到了哪些编译错误?编辑了问题-谢谢目标是创建一个包含0行的表?用例是什么?是的,我已经有了插入行的代码,我只是在寻找创建空表的代码。感谢Hanks为我提供了在Java中执行此操作的线索,请参阅我的答案
    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) {
    }