2

I'm trying to create a BQ table schema, as seen on this page

But I get a compilation error for all the mode="REQUIRED"

I didn't see anything special to import but the bq module.

Expected type '_SpecialForm[str]', got 'str' instead 

The code:

    bqServiceWrapper.create_table(_ADS_TO_REMOVE_TABLE_NAME,
                                  [
                                      bigquery.SchemaField("add_id", "STRING", mode="REQUIRED"),
                                      bigquery.SchemaField("timestamp_str", "TIMESTAMP", mode="REQUIRED"),
                                      bigquery.SchemaField("timestamp", "TIMESTAMP", mode="REQUIRED")
                                  ])

BTW does the python BQ library allows creating a table without a schema (like Java does?). If so - how can the type to implied as "TIMESTAMP" and not "STRING"?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471

1 Answers1

1

There appears to be an extra mode=”REQUIRED” in your code. Also, your code is not creating a table as mentioned in the doc table = bigquery.Table(table_id, schema=schema) . Rewriting your code as follows :

from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()


client.create_table(bigquery.Table("ProjectID.Dataset.Table", schema= [
                                   bigquery.SchemaField("add_id", "STRING", mode="REQUIRED"),
                                   bigquery.SchemaField("timestamp_str", "TIMESTAMP", mode="REQUIRED"),
                                    bigquery.SchemaField("timestamp", "TIMESTAMP", mode="REQUIRED")
                                ]))

This creates the table in BigQuery with the required schema :enter image description here

For creating a schemaless table using the Python client library you can simply run the above code without the schema : client.create_table(bigquery.Table("ProjectID.Dataset.Table")) or directly client.create_table("ProjectID.Dataset.Table"). But if we are creating a schemaless table we need to define the schema either by auto-detect or manually and only then we can add data to it. Assuming you are trying to load data from a CSV file into an empty table with auto-detect schema, you need to have the Timestamp data in the supported format as specified in this doc.

Krish
  • 752
  • 3
  • 10
  • The extra `required` was a copy paste issue. Regarding the signature, it has actually worked for me. – Elad Benda Sep 13 '21 at 11:39
  • Hi @EladBenda, Could you please clarify what is the issue you are facing? Because the code I provided, is creating the BigQuery table with the schema that you mentioned. – Krish Sep 15 '21 at 07:38
  • how do I enable "auto-detect"? – Elad Benda Sep 15 '21 at 07:44
  • Please refer to this [documentation](https://cloud.google.com/bigquery/docs/schema-detect#python) for enabling schema auto-detection. The documentation shows how to enable "auto-detect" using the Client Libraries. – Krish Sep 15 '21 at 09:54