Set hive partitioning options

Set hive partitioning options

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Java

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries . For more information, see the BigQuery Java API reference documentation .

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  import 
  
 com.google.cloud.bigquery. BigQuery 
 
 ; 
 import 
  
 com.google.cloud.bigquery. BigQueryException 
 
 ; 
 import 
  
 com.google.cloud.bigquery. BigQueryOptions 
 
 ; 
 import 
  
 com.google.cloud.bigquery. ExternalTableDefinition 
 
 ; 
 import 
  
 com.google.cloud.bigquery. FormatOptions 
 
 ; 
 import 
  
 com.google.cloud.bigquery. HivePartitioningOptions 
 
 ; 
 import 
  
 com.google.cloud.bigquery. TableId 
 
 ; 
 import 
  
 com.google.cloud.bigquery. TableInfo 
 
 ; 
 // Sample to create external table using hive partitioning 
 public 
  
 class 
 CreateTableExternalHivePartitioned 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 datasetName 
  
 = 
  
 "MY_DATASET_NAME" 
 ; 
  
 String 
  
 tableName 
  
 = 
  
 "MY_TABLE_NAME" 
 ; 
  
 String 
  
 sourceUri 
  
 = 
  
 "gs://cloud-samples-data/bigquery/hive-partitioning-samples/customlayout/*" 
 ; 
  
 String 
  
 sourceUriPrefix 
  
 = 
  
 "gs://cloud-samples-data/bigquery/hive-partitioning-samples/customlayout/{pkey:STRING}/" 
 ; 
  
 createTableExternalHivePartitioned 
 ( 
 datasetName 
 , 
  
 tableName 
 , 
  
 sourceUriPrefix 
 , 
  
 sourceUri 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 createTableExternalHivePartitioned 
 ( 
  
 String 
  
 datasetName 
 , 
  
 String 
  
 tableName 
 , 
  
 String 
  
 sourceUriPrefix 
 , 
  
 String 
  
 sourceUri 
 ) 
  
 { 
  
 try 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
  BigQuery 
 
  
 bigquery 
  
 = 
  
  BigQueryOptions 
 
 . 
 getDefaultInstance 
 (). 
 getService 
 (); 
  
 // Configuring partitioning options 
  
  HivePartitioningOptions 
 
  
 hivePartitioningOptions 
  
 = 
  
  HivePartitioningOptions 
 
 . 
 newBuilder 
 () 
  
 . 
 setMode 
 ( 
 "CUSTOM" 
 ) 
  
 . 
 setRequirePartitionFilter 
 ( 
 true 
 ) 
  
 . 
  setSourceUriPrefix 
 
 ( 
 sourceUriPrefix 
 ) 
  
 . 
 build 
 (); 
  
  TableId 
 
  
 tableId 
  
 = 
  
  TableId 
 
 . 
 of 
 ( 
 datasetName 
 , 
  
 tableName 
 ); 
  
  ExternalTableDefinition 
 
  
 customTable 
  
 = 
  
  ExternalTableDefinition 
 
 . 
 newBuilder 
 ( 
 sourceUri 
 , 
  
  FormatOptions 
 
 . 
  parquet 
 
 ()) 
  
 . 
 setAutodetect 
 ( 
 true 
 ) 
  
 . 
 setHivePartitioningOptions 
 ( 
 hivePartitioningOptions 
 ) 
  
 . 
 build 
 (); 
  
 bigquery 
 . 
  create 
 
 ( 
 TableInfo 
 . 
 of 
 ( 
 tableId 
 , 
  
 customTable 
 )); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "External table created using hivepartitioningoptions" 
 ); 
  
 } 
  
 catch 
  
 ( 
  BigQueryException 
 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "External table was not created" 
  
 + 
  
 e 
 . 
 toString 
 ()); 
  
 } 
  
 } 
 } 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: