BigQuery,如何以编程方式定义数组字段?

时间:2018-06-06 09:45:18

标签: java kotlin google-bigquery

我正在尝试启动一个数据仓库项目,这就是我希望我的架构看起来像:

table:event_log 架构:

 -> info
    -> user_id: "xyz"
    -> user_properties // <- I want this to be array like
       -> 0
          -> key: "name
          -> value
             -> int_value: null
             -> string_value: "osp"
             ...
       -> 1 // and it goes on

问题是我不知道如何以编程方式定义这个数组结构。

我从这里接受了这个想法:

https://www.youtube.com/watch?v=pxNrkjBeHpw

这是我的代码(kotlin使用java google云库)到目前为止:

    val tableId = TableId.of(datasetName, tableName)

    // First part, general field
    val generalInfoFields = ArrayList<Field>()
    generalInfoFields.add(Field.of("user_id", LegacySQLTypeName.STRING))


    generalInfoFields.add(Field.of("user_properties", {ARRAY LIKE TYPE??}))

    val general_info = Field.of("general_info", LegacySQLTypeName.RECORD, FieldList.of(generalInfoFields))


    // Combine fields and create table
    val tableSchema = Schema.of(general_info)
    val tableDefinition = StandardTableDefinition.of(tableSchema)
    val tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build()
    val table = bigquery.create(tableInfo)



    log.info("dataset created " + dataset.datasetId.dataset)

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

要在BigQuery架构中定义数组,您需要使用Field.Mode.REPEATED修饰符。检查official docs

您的代码将如下所示:

val arrayField = Field.newBuilder("user_properties", LegacySQLTypeName.RECORD, FieldList.of(<record nested fields here>))
        .setMode(Field.Mode.REPEATED).build()