Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add "array of strings" as a schema value for BigQuery

I want to store an array of string values in a single column in BigQuery. In order to do that I need to first create a table with the proper schema. The schema identifier for the array is ARRAY<T> where T is the data type.

For example: T replaced by INT64 as ARRAY<INT64> enables the storage of 64-bit integer array in that column. How should I get this same effect but for storing string values? I have already tried STRING, VARCHAR and CHAR.

Just mention: I am using latest google-cloud python package

Documentation Reference for BigQuery Schema

like image 499
Vikram Tiwari Avatar asked Jul 25 '17 23:07

Vikram Tiwari


People also ask

How do you create an array in BigQuery?

To declare a specific data type for an array, use angle brackets ( < and > ). For example: SELECT ARRAY<FLOAT64>[1, 2, 3] as floats; Arrays of most data types, such as INT64 or STRING , don't require that you declare them first.

How do I add multiple columns in BigQuery?

Alter statement allows us to modify the structure of the existing table. Using Alter Table Add Column statement, we can add one or more columns to the existing table in BigQuery.


2 Answers

In order to add a column with array data, you need to define that column's mode as REPEATED. Hence a sample schema ends up being:

{
  'name': 'array_of_strings',
  'type': 'STRING',
  'mode': 'REPEATED'
},{
  'name': 'array_of_floats',
  'type': 'FLOAT',
  'mode': 'REPEATED'
}

This makes the field hold array values.

NOTE: You should be aware that if this is the schema of the table then you can not use CSV import functionality of big query since this is a limitation of CSV file format. You will need to use either json or avro formats.

Reference to the GitHub issue

like image 95
Vikram Tiwari Avatar answered Nov 11 '22 18:11

Vikram Tiwari


In order to do that I need to first create a table with the proper schema.

just run below in Web UI with new destination table - to create needed schema

#standardSQL
SELECT ARRAY<STRING>[] AS array_of_strings
like image 41
Mikhail Berlyant Avatar answered Nov 11 '22 19:11

Mikhail Berlyant