Array parameters

Run a query with array parameters.

Explore further

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

Code sample

C#

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

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

  using 
  
  Google.Cloud.BigQuery.V2 
 
 ; 
 using 
  
 System 
 ; 
 public 
  
 class 
  
 BigQueryQueryWithArrayParameters 
 { 
  
 public 
  
 void 
  
 QueryWithArrayParameters 
 ( 
 string 
  
 projectId 
  
 = 
  
 "your-project-id" 
 ) 
  
 { 
  
 var 
  
 gender 
  
 = 
  
 "M" 
 ; 
  
 string 
 [] 
  
 states 
  
 = 
  
 { 
  
 "WA" 
 , 
  
 "WI" 
 , 
  
 "WV" 
 , 
  
 "WY" 
  
 }; 
  
 // Note: Standard SQL is required to use query parameters. 
  
 var 
  
 query 
  
 = 
  
 @" 
 SELECT name, sum(number) as count 
 FROM `bigquery-public-data.usa_names.usa_1910_2013` 
 WHERE gender = @gender 
 AND state IN UNNEST(@states) 
 GROUP BY name 
 ORDER BY count DESC 
 LIMIT 10;" 
 ; 
  
 // Initialize client that will be used to send requests. 
  
 var 
  
 client 
  
 = 
  
  BigQueryClient 
 
 . 
  Create 
 
 ( 
 projectId 
 ); 
  
 var 
  
 parameters 
  
 = 
  
 new 
  
  BigQueryParameter 
 
 [] 
  
 { 
  
 new 
  
  BigQueryParameter 
 
 ( 
 "gender" 
 , 
  
  BigQueryDbType 
 
 . 
  String 
 
 , 
  
 gender 
 ), 
  
 new 
  
  BigQueryParameter 
 
 ( 
 "states" 
 , 
  
  BigQueryDbType 
 
 . 
  Array 
 
 , 
  
 states 
 ) 
  
 }; 
  
 var 
  
 job 
  
 = 
  
 client 
 . 
 CreateQueryJob 
 ( 
  
 sql 
 : 
  
 query 
 , 
  
 parameters 
 : 
  
 parameters 
 , 
  
 options 
 : 
  
 new 
  
  QueryOptions 
 
  
 { 
  
 UseQueryCache 
  
 = 
  
 false 
  
 }); 
  
 // Wait for the job to complete. 
  
 job 
  
 = 
  
 job 
 . 
  PollUntilCompleted 
 
 (). 
 ThrowOnAnyError 
 (); 
  
 // Display the results 
  
 foreach 
  
 ( 
  BigQueryRow 
 
  
 row 
  
 in 
  
 client 
 . 
 GetQueryResults 
 ( 
 job 
 . 
 Reference 
 )) 
  
 { 
  
 Console 
 . 
 WriteLine 
 ( 
 $"{row[" 
 name 
 "]}: {row[" 
 count 
 "]}" 
 ); 
  
 } 
  
 } 
 } 
 

Go

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

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

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "cloud.google.com/go/bigquery" 
  
 "google.golang.org/api/iterator" 
 ) 
 // queryWithArrayParams demonstrates issuing a query and specifying query parameters that include an 
 // array of strings. 
 func 
  
 queryWithArrayParams 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 bigquery 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "bigquery.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 q 
  
 := 
  
 client 
 . 
 Query 
 ( 
  
 `SELECT 
 name, 
 sum(number) as count 
 FROM ` 
  
 + 
  
 "`bigquery-public-data.usa_names.usa_1910_2013`" 
  
 + 
  
 ` 
 WHERE 
 gender = @gender 
 AND state IN UNNEST(@states) 
 GROUP BY 
 name 
 ORDER BY 
 count DESC 
 LIMIT 10;` 
 ) 
  
 q 
 . 
 Parameters 
  
 = 
  
 [] 
 bigquery 
 . 
  QueryParameter 
 
 { 
  
 { 
  
 Name 
 : 
  
 "gender" 
 , 
  
 Value 
 : 
  
 "M" 
 , 
  
 }, 
  
 { 
  
 Name 
 : 
  
 "states" 
 , 
  
 Value 
 : 
  
 [] 
 string 
 { 
 "WA" 
 , 
  
 "WI" 
 , 
  
 "WV" 
 , 
  
 "WY" 
 }, 
  
 }, 
  
 } 
  
 // Run the query and process the returned row iterator. 
  
 it 
 , 
  
 err 
  
 := 
  
 q 
 . 
 Read 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "query.Read(): %w" 
 , 
  
 err 
 ) 
  
 } 
  
 for 
  
 { 
  
 var 
  
 row 
  
 [] 
 bigquery 
 . 
  Value 
 
  
 err 
  
 := 
  
 it 
 . 
 Next 
 ( 
& row 
 ) 
  
 if 
  
 err 
  
 == 
  
 iterator 
 . 
 Done 
  
 { 
  
 break 
  
 } 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 err 
  
 } 
  
 fmt 
 . 
 Fprintln 
 ( 
 w 
 , 
  
 row 
 ) 
  
 } 
  
 return 
  
 nil 
 } 
 

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. QueryJobConfiguration 
 
 ; 
 import 
  
 com.google.cloud.bigquery. QueryParameterValue 
 
 ; 
 import 
  
 com.google.cloud.bigquery. TableResult 
 
 ; 
 // Sample to running a query with array query parameters. 
 public 
  
 class 
 QueryWithArrayParameters 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 { 
  
 String 
  
 gender 
  
 = 
  
 "M" 
 ; 
  
 String 
 [] 
  
 states 
  
 = 
  
 { 
 "WA" 
 , 
  
 "WI" 
 , 
  
 "WV" 
 , 
  
 "WY" 
 }; 
  
 String 
  
 query 
  
 = 
  
 "SELECT name, sum(number) as count\n" 
  
 + 
  
 "FROM `bigquery-public-data.usa_names.usa_1910_2013`\n" 
  
 + 
  
 "WHERE gender = @gender\n" 
  
 + 
  
 "AND state IN UNNEST(@states)\n" 
  
 + 
  
 "GROUP BY name\n" 
  
 + 
  
 "ORDER BY count DESC\n" 
  
 + 
  
 "LIMIT 10;" 
 ; 
  
 queryWithArrayParameters 
 ( 
 query 
 , 
  
 gender 
 , 
  
 states 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 queryWithArrayParameters 
 ( 
 String 
  
 query 
 , 
  
 String 
  
 gender 
 , 
  
 String 
 [] 
  
 states 
 ) 
  
 { 
  
 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 
 (); 
  
 // Note: Standard SQL is required to use query parameters. 
  
  QueryJobConfiguration 
 
  
 queryConfig 
  
 = 
  
  QueryJobConfiguration 
 
 . 
 newBuilder 
 ( 
 query 
 ) 
  
 . 
  addNamedParameter 
 
 ( 
 "gender" 
 , 
  
  QueryParameterValue 
 
 . 
  string 
 
 ( 
 gender 
 )) 
  
 . 
  addNamedParameter 
 
 ( 
 "states" 
 , 
  
  QueryParameterValue 
 
 . 
  array 
 
 ( 
 states 
 , 
  
 String 
 . 
 class 
 )) 
  
 . 
 build 
 (); 
  
  TableResult 
 
  
 results 
  
 = 
  
 bigquery 
 . 
  query 
 
 ( 
 queryConfig 
 ); 
  
 // Print the results. 
  
 results 
  
 . 
  iterateAll 
 
 () 
  
 . 
 forEach 
 ( 
 row 
  
 - 
>  
 row 
 . 
 forEach 
 ( 
 val 
  
 - 
>  
 System 
 . 
 out 
 . 
 printf 
 ( 
 "%s," 
 , 
  
 val 
 . 
 toString 
 ()))); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Query with arrays parameters performed successfully" 
 ); 
  
 } 
  
 catch 
  
 ( 
  BigQueryException 
 
  
 | 
  
 InterruptedException 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Query not performed \n" 
  
 + 
  
 e 
 . 
 toString 
 ()); 
  
 } 
  
 } 
 } 
 

Node.js

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

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

  // Run a query using array query parameters 
 // Import the Google Cloud client library 
 const 
  
 { 
 BigQuery 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/bigquery 
' 
 ); 
 const 
  
 bigquery 
  
 = 
  
 new 
  
  BigQuery 
 
 (); 
 async 
  
 function 
  
 queryParamsArrays 
 () 
  
 { 
  
 // The SQL query to run 
  
 const 
  
 sqlQuery 
  
 = 
  
 `SELECT name, sum(number) as count 
 FROM \`bigquery-public-data.usa_names.usa_1910_2013\` 
 WHERE gender = @gender 
 AND state IN UNNEST(@states) 
 GROUP BY name 
 ORDER BY count DESC 
 LIMIT 10;` 
 ; 
  
 const 
  
 options 
  
 = 
  
 { 
  
 query 
 : 
  
 sqlQuery 
 , 
  
 // Location must match that of the dataset(s) referenced in the query. 
  
 location 
 : 
  
 'US' 
 , 
  
 params 
 : 
  
 { 
 gender 
 : 
  
 'M' 
 , 
  
 states 
 : 
  
 [ 
 'WA' 
 , 
  
 'WI' 
 , 
  
 'WV' 
 , 
  
 'WY' 
 ]}, 
  
 }; 
  
 // Run the query 
  
 const 
  
 [ 
 rows 
 ] 
  
 = 
  
 await 
  
 bigquery 
 . 
 query 
 ( 
 options 
 ); 
  
 console 
 . 
 log 
 ( 
 'Rows:' 
 ); 
  
 rows 
 . 
 forEach 
 ( 
 row 
  
 = 
>  
 console 
 . 
 log 
 ( 
 row 
 )); 
 } 
 

Python

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

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

  from 
  
 google.cloud 
  
 import 
  bigquery 
 
 # Construct a BigQuery client object. 
 client 
 = 
  bigquery 
 
 . 
  Client 
 
 () 
 query 
 = 
 """ 
 SELECT name, sum(number) as count 
 FROM `bigquery-public-data.usa_names.usa_1910_2013` 
 WHERE gender = @gender 
 AND state IN UNNEST(@states) 
 GROUP BY name 
 ORDER BY count DESC 
 LIMIT 10; 
 """ 
 job_config 
 = 
  bigquery 
 
 . 
  QueryJobConfig 
 
 ( 
 query_parameters 
 = 
 [ 
  bigquery 
 
 . 
  ScalarQueryParameter 
 
 ( 
 "gender" 
 , 
 "STRING" 
 , 
 "M" 
 ), 
  bigquery 
 
 . 
  ArrayQueryParameter 
 
 ( 
 "states" 
 , 
 "STRING" 
 , 
 [ 
 "WA" 
 , 
 "WI" 
 , 
 "WV" 
 , 
 "WY" 
 ]), 
 ] 
 ) 
 rows 
 = 
 client 
 . 
  query_and_wait 
 
 ( 
 query 
 , 
 job_config 
 = 
 job_config 
 ) 
 # Make an API request. 
 for 
 row 
 in 
 rows 
 : 
 print 
 ( 
 " 
 {} 
 : 
 \t 
 {} 
 " 
 . 
 format 
 ( 
 row 
 . 
 name 
 , 
 row 
 . 
 count 
 )) 
 

What's next

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

Design a Mobile Site
View Site in Mobile | Classic
Share by: