有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中更新BigQuery架构/添加新列

我需要通过Java更新BigQuery表模式。更改将是附加的(仅添加新列)

我正在努力找到实现这一目标的方法。在Python中,可以这样做:

table_ref = client.dataset(dataset_id).table(table_id)
table = client.get_table(table_ref)  # API request

original_schema = table.schema
new_schema = original_schema[:]  # creates a copy of the schema
new_schema.append(bigquery.SchemaField('phone', 'STRING'))

table.schema = new_schema
table = client.update_table(table, ['schema'])  # API request

https://cloud.google.com/bigquery/docs/managing-table-schemas页上,声明将使用修补程序端点执行此任务

提出了一个改进补丁API的问题,但我不知道结果https://github.com/googleapis/google-cloud-java/issues/1564

这是到修补程序类文档的链接:https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/java/latest/com/google/api/services/bigquery/Bigquery.Tables.Patch.html#set-java.lang.String-java.lang.Object-

任何帮助都将不胜感激。谢谢


共 (2) 个答案

  1. # 1 楼答案

    Java中的想法与您共享的Python示例中的想法相同,即获取当前模式并向其添加一个新列。您可以通过我准备的代码片段来实现这一点,您可以在下面看到:

    // Instantiate the BQ client
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    
    // Get the table, schema and fields from the already-existing table
    Table table = bigquery.getTable(TableId.of("PROJECT_ID", "DATASET", "TABLE"));
    Schema schema = table.getDefinition().getSchema();
    FieldList fields = schema.getFields();
    
    // Create the new field
    Field newField = Field.of("column2", LegacySQLTypeName.STRING);
    
    // Create a new schema adding the current fields, plus the new one
    List<Field> field_list = new ArrayList<Field>();
    for (Field f : fields) {
        field_list.add(f);
    }
    field_list.add(newField);
    Schema newSchema = Schema.of(field_list);
    
    // Update the table with the new schema
    Table updatedTable = table.toBuilder().setDefinition(StandardTableDefinition.of(newSchema)).build().update();
    

    这段代码使用的是com.google.cloud.bigquery包(参见其documentation here)。然后,它按照tables documentation中的示例指定模式定义,并最终更新它

  2. # 2 楼答案

    我还通过另一个Google BigQuery软件包实现了这一点:

        HttpTransport transport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();
        GoogleCredential credential;
        try {
            credential = GoogleCredential.getApplicationDefault(transport,
                    jsonFactory);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (credential.createScopedRequired()) {
            credential = credential.createScoped(BigqueryScopes.all());
        }
    
        Bigquery.Tables bqTables = new Bigquery.Builder(transport, jsonFactory, credential).build().tables();
        Bigquery.Tables.Get bqTableGet = bqTables.get(this.project, this.dataset, this.tablePrefix + strDate);
    
        Table bqTable = bqTableGet.execute();
        bqTable.setSchema(this.schema);
    
        Bigquery.Tables.Patch bqTablePatch = bqTables.patch(this.project, this.dataset, this.tablePrefix + strDate, bqTable);
        bqTablePatch.execute();