BigQuery Create Table with Structs using Java API

336 Views Asked by At

I am trying to create a table in Big Query using Java API for the below format col1 STRUCT<col2 ARRAY<STRUCT<array_element STRING>>> . I am able to create like col2 ARRAY<STRUCT<array_element STRING>> , but unable to wrap another STRUCT outside of col2 ARRAY<STRUCT<array_element STRING>>. below is the sample code

            TableId tableId = TableId.of(datasetName, tableName);

            Schema schema =
                    Schema.of(
                            Field.of("id", StandardSQLTypeName.STRING),
                            Field.of("first_name", StandardSQLTypeName.STRING),
                            Field.of("last_name", StandardSQLTypeName.STRING),
                            Field.of("dob", StandardSQLTypeName.DATE),
                            // create the nested and repeated field
                            Field.newBuilder(
                                            "addresses",
                                            StandardSQLTypeName.STRUCT
                                            ,Field.of("status", StandardSQLTypeName.STRING),Field.of("value", StandardSQLTypeName.STRING)
                                    )
                                    .setMode(Mode.REPEATED)
                                    .build(),
                            Field.of("test", StandardSQLTypeName.DATE)

                    );

the above code is creating a table as below

(                                                                  |
|   id STRING,                                                       |
|   first_name STRING,                                               |
|   last_name STRING,                                                |
|   dob DATE,                                                        |
|   addresses ARRAY<STRUCT<status STRING, value STRING>>,            |
|   test DATE                                                        |
| );

but for address column I need a data type like col1 STRUCT<addresses ARRAY<STRUCT<status STRING, value STRING>>>.

any suggestion ?

0

There are 0 best solutions below