Search code examples
pythongoogle-cloud-platformgoogle-bigquerydata-partitioningtable-partitioning

How to find out the type of partitioning in a table in google bigquery using python apis


def partition(dataset1, dataset2):
    try:
        client.get_dataset(dataset2)
        print("Dataset {} already exists".format(dataset2))
    except NotFound:
         print("Dataset {} not found".format(dataset2))
         createDataset(dataset2)
    table = client.get_table(dataset1) # get the source dataset
    partition_column = table.time_partitioning.field #get the column name

I tried using get_table description but it dint work out it was returning just none. And I got the docs link from googleapi link here How to get what type of partitioning is used by the table


Solution

  • You can use the following script to get the partition type :

    from google.cloud import bigquery
    
    if __name__ == '__main__':
        # Construct a BigQuery client object.
        client = bigquery.Client()
    
        # table_id = 'your-project.your_dataset.your_table'
    
        table = client.get_table('your-project.your_dataset.your_table')  # Make an API request.
    
        # View table properties
        print(
            "Got table '{}.{}.{}'.".format(table.project, table.dataset_id, table.table_id)
        )
        print("Table schema: {}".format(table.schema))
        print("Table description: {}".format(table.description))
        print("Table has {} rows".format(table.num_rows))
    
        # Partition type and field.
        partition_field: str = table.time_partitioning.field
        partition_type: str = table.time_partitioning.type_
    
        print("Table partition field {}".format(partition_field))
        print("Table partition type {}".format(partition_type))
    
    • table.time_partitioning.field gives the partition column name
    • table.time_partitioning.type_ gives the partition type