Quickstart

Use a basic SELECT query.

Explore further

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

Code sample

C++

To learn how to install and use the client library for Spanner, see Spanner client libraries .

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  #include 
  
 "google/cloud/spanner/client.h" 
 void 
  
 Quickstart 
 ( 
 std 
 :: 
 string 
  
 const 
&  
 project_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 instance_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 database_id 
 ) 
  
 { 
  
 namespace 
  
 spanner 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 spanner 
 ; 
  
 auto 
  
 database 
  
 = 
  
 spanner 
 :: 
 Database 
 ( 
 project_id 
 , 
  
 instance_id 
 , 
  
 database_id 
 ); 
  
 auto 
  
 connection 
  
 = 
  
 spanner 
 :: 
 MakeConnection 
 ( 
 database 
 ); 
  
 auto 
  
 client 
  
 = 
  
 spanner 
 :: 
 Client 
 ( 
 connection 
 ); 
  
 auto 
  
 rows 
  
 = 
  
 client 
 . 
 ExecuteQuery 
 ( 
 spanner 
 :: 
 SqlStatement 
 ( 
 "SELECT 'Hello World'" 
 )); 
  
 using 
  
 RowType 
  
 = 
  
 std 
 :: 
 tuple<std 
 :: 
 string 
> ; 
  
 for 
  
 ( 
 auto 
&  
 row 
  
 : 
  
 spanner 
 :: 
 StreamOf<RowType> 
 ( 
 rows 
 )) 
  
 { 
  
 if 
  
 ( 
 ! 
 row 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 row 
 ). 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 std 
 :: 
 get<0> 
 ( 
 * 
 row 
 ) 
 << 
 " 
 \n 
 " 
 ; 
  
 } 
 } 
 

C#

To learn how to install and use the client library for Spanner, see Spanner client libraries .

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  using 
  
  Google.Cloud.Spanner.Data 
 
 ; 
 using 
  
 System 
 ; 
 using 
  
 System.Threading.Tasks 
 ; 
 namespace 
  
 GoogleCloudSamples.Spanner 
 { 
  
 public 
  
 class 
  
 QuickStart 
  
 { 
  
 static 
  
 async 
  
 Task 
  
 MainAsync 
 () 
  
 { 
  
 string 
  
 projectId 
  
 = 
  
 "YOUR-PROJECT-ID" 
 ; 
  
 string 
  
 instanceId 
  
 = 
  
 "my-instance" 
 ; 
  
 string 
  
 databaseId 
  
 = 
  
 "my-database" 
 ; 
  
 string 
  
 connectionString 
  
 = 
  
 $"Data Source=projects/{projectId}/instances/{instanceId}/" 
  
 + 
  
 $"databases/{databaseId}" 
 ; 
  
 // Create connection to Cloud Spanner. 
  
 using 
  
 ( 
 var 
  
 connection 
  
 = 
  
 new 
  
  SpannerConnection 
 
 ( 
 connectionString 
 )) 
  
 { 
  
 // Execute a simple SQL statement. 
  
 var 
  
 cmd 
  
 = 
  
 connection 
 . 
  CreateSelectCommand 
 
 ( 
  
 @"SELECT ""Hello World"" as test" 
 ); 
  
 using 
  
 ( 
 var 
  
 reader 
  
 = 
  
 await 
  
 cmd 
 . 
  ExecuteReaderAsync 
 
 ()) 
  
 { 
  
 while 
  
 ( 
 await 
  
 reader 
 . 
  ReadAsync 
 
 ()) 
  
 { 
  
 Console 
 . 
 WriteLine 
 ( 
  
 reader 
 . 
 GetFieldValue<string> 
 ( 
 "test" 
 )); 
  
 } 
  
 } 
  
 } 
  
 } 
  
 public 
  
 static 
  
 void 
  
 Main 
 ( 
 string 
 [] 
  
 args 
 ) 
  
 { 
  
 MainAsync 
 (). 
 Wait 
 (); 
  
 } 
  
 } 
 } 
 

Go

To learn how to install and use the client library for Spanner, see Spanner client libraries .

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  // Sample spanner_quickstart is a basic program that uses Cloud Spanner. 
 package 
  
 main 
 import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "log" 
  
 "cloud.google.com/go/spanner" 
  
 "google.golang.org/api/iterator" 
 ) 
 func 
  
 main 
 () 
  
 { 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 // This database must exist. 
  
 databaseName 
  
 := 
  
 "projects/your-project-id/instances/your-instance-id/databases/your-database-id" 
  
 client 
 , 
  
 err 
  
 := 
  
 spanner 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 databaseName 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 log 
 . 
 Fatalf 
 ( 
 "Failed to create client %v" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 stmt 
  
 := 
  
 spanner 
 . 
  Statement 
 
 { 
 SQL 
 : 
  
 "SELECT 1" 
 } 
  
 iter 
  
 := 
  
 client 
 . 
  Single 
 
 (). 
 Query 
 ( 
 ctx 
 , 
  
 stmt 
 ) 
  
 defer 
  
 iter 
 . 
 Stop 
 () 
  
 for 
  
 { 
  
 row 
 , 
  
 err 
  
 := 
  
 iter 
 . 
 Next 
 () 
  
 if 
  
 err 
  
 == 
  
 iterator 
 . 
 Done 
  
 { 
  
 fmt 
 . 
 Println 
 ( 
 "Done" 
 ) 
  
 return 
  
 } 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 log 
 . 
 Fatalf 
 ( 
 "Query failed with %v" 
 , 
  
 err 
 ) 
  
 } 
  
 var 
  
 i 
  
 int64 
  
 if 
  
 row 
 . 
  Columns 
 
 ( 
& i 
 ) 
  
 != 
  
 nil 
  
 { 
  
 log 
 . 
 Fatalf 
 ( 
 "Failed to parse row %v" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Printf 
 ( 
 "Got value %v\n" 
 , 
  
 i 
 ) 
  
 } 
 } 
 

Java

To learn how to install and use the client library for Spanner, see Spanner client libraries .

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  // Imports the Google Cloud client library 
 import 
  
 com.google.cloud.spanner. DatabaseClient 
 
 ; 
 import 
  
 com.google.cloud.spanner. DatabaseId 
 
 ; 
 import 
  
 com.google.cloud.spanner. ResultSet 
 
 ; 
 import 
  
 com.google.cloud.spanner. Spanner 
 
 ; 
 import 
  
 com.google.cloud.spanner. SpannerOptions 
 
 ; 
 import 
  
 com.google.cloud.spanner. Statement 
 
 ; 
 /** 
 * A quick start code for Cloud Spanner. It demonstrates how to setup the Cloud Spanner client and 
 * execute a simple query using it against an existing database. 
 */ 
 public 
  
 class 
 QuickstartSample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 ... 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 if 
  
 ( 
 args 
 . 
 length 
  
 != 
  
 2 
 ) 
  
 { 
  
 System 
 . 
 err 
 . 
 println 
 ( 
 "Usage: QuickStartSample <instance_id> <database_id>" 
 ); 
  
 return 
 ; 
  
 } 
  
 // Instantiates a client 
  
  SpannerOptions 
 
  
 options 
  
 = 
  
  SpannerOptions 
 
 . 
 newBuilder 
 (). 
 build 
 (); 
  
  Spanner 
 
  
 spanner 
  
 = 
  
  options 
 
 . 
 getService 
 (); 
  
 // Name of your instance & database. 
  
 String 
  
 instanceId 
  
 = 
  
 args 
 [ 
 0 
 ] 
 ; 
  
 String 
  
 databaseId 
  
 = 
  
 args 
 [ 
 1 
 ] 
 ; 
  
 try 
  
 { 
  
 // Creates a database client 
  
  DatabaseClient 
 
  
 dbClient 
  
 = 
  
 spanner 
 . 
  getDatabaseClient 
 
 ( 
 DatabaseId 
 . 
 of 
 ( 
 options 
 . 
 getProjectId 
 (), 
  
 instanceId 
 , 
  
 databaseId 
 )); 
  
 // Queries the database 
  
  ResultSet 
 
  
 resultSet 
  
 = 
  
 dbClient 
 . 
  singleUse 
 
 (). 
 executeQuery 
 ( 
  Statement 
 
 . 
 of 
 ( 
 "SELECT 1" 
 )); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "\n\nResults:" 
 ); 
  
 // Prints the results 
  
 while 
  
 ( 
 resultSet 
 . 
  next 
 
 ()) 
  
 { 
  
 System 
 . 
 out 
 . 
 printf 
 ( 
 "%d\n\n" 
 , 
  
 resultSet 
 . 
 getLong 
 ( 
 0 
 )); 
  
 } 
  
 } 
  
 finally 
  
 { 
  
 // Closes the client which will free up the resources used 
  
 spanner 
 . 
  close 
 
 (); 
  
 } 
  
 } 
 } 
 

Node.js

To learn how to install and use the client library for Spanner, see Spanner client libraries .

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  // Imports the Google Cloud client library 
 const 
  
 { 
 Spanner 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/spanner 
' 
 ); 
 // Creates a client 
 const 
  
 spanner 
  
 = 
  
 new 
  
  Spanner 
 
 ({ 
 projectId 
 }); 
 // Gets a reference to a Cloud Spanner instance and database 
 const 
  
 instance 
  
 = 
  
 spanner 
 . 
 instance 
 ( 
 instanceId 
 ); 
 const 
  
 database 
  
 = 
  
 instance 
 . 
 database 
 ( 
 databaseId 
 ); 
 // The query to execute 
 const 
  
 query 
  
 = 
  
 { 
  
 sql 
 : 
  
 'SELECT 1' 
 , 
 }; 
 // Execute a simple SQL statement 
 const 
  
 [ 
 rows 
 ] 
  
 = 
  
 await 
  
 database 
 . 
 run 
 ( 
 query 
 ); 
 console 
 . 
 log 
 ( 
 `Query: 
 ${ 
 rows 
 . 
 length 
 } 
 found.` 
 ); 
 rows 
 . 
 forEach 
 ( 
 row 
  
 = 
>  
 console 
 . 
 log 
 ( 
 row 
 )); 
 

PHP

To learn how to install and use the client library for Spanner, see Spanner client libraries .

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  # Includes the autoloader for libraries installed with composer 
 require __DIR__ . '/vendor/autoload.php'; 
 # Imports the Google Cloud client library 
 use Google\Cloud\Spanner\SpannerClient; 
 # Your Google Cloud Platform project ID 
 $projectId = 'YOUR_PROJECT_ID'; 
 # Instantiates a client 
 $spanner = new SpannerClient([ 
 'projectId' => $projectId 
 ]); 
 # Your Cloud Spanner instance ID. 
 $instanceId = 'your-instance-id'; 
 # Get a Cloud Spanner instance by ID. 
 $instance = $spanner->instance($instanceId); 
 # Your Cloud Spanner database ID. 
 $databaseId = 'your-database-id'; 
 # Get a Cloud Spanner database by ID. 
 $database = $instance->database($databaseId); 
 # Execute a simple SQL statement. 
 $results = $database->execute('SELECT "Hello World" as test'); 
 foreach ($results as $row) { 
 print($row['test'] . PHP_EOL); 
 } 
 

Python

To learn how to install and use the client library for Spanner, see Spanner client libraries .

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  # Imports the Google Cloud Client Library. 
 from 
  
 google.cloud 
  
 import 
  spanner 
 
 # Your Cloud Spanner instance ID. 
 # instance_id = "my-instance-id" 
 # 
 # Your Cloud Spanner database ID. 
 # database_id = "my-database-id" 
 # Instantiate a client. 
 spanner_client 
 = 
  spanner 
 
 . 
  Client 
 
 () 
 # Get a Cloud Spanner instance by ID. 
 instance 
 = 
 spanner_client 
 . 
  instance 
 
 ( 
 instance_id 
 ) 
 # Get a Cloud Spanner database by ID. 
 database 
 = 
 instance 
 . 
 database 
 ( 
 database_id 
 ) 
 # Execute a simple SQL statement. 
 with 
 database 
 . 
 snapshot 
 () 
 as 
 snapshot 
 : 
 results 
 = 
 snapshot 
 . 
 execute_sql 
 ( 
 "SELECT 1" 
 ) 
 for 
 row 
 in 
 results 
 : 
 print 
 ( 
 row 
 ) 
 

Ruby

To learn how to install and use the client library for Spanner, see Spanner client libraries .

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  # Imports the Google Cloud client library 
 require 
  
 "google/cloud/spanner" 
 # Your Google Cloud Platform project ID 
 project_id 
  
 = 
  
 "YOUR_PROJECT_ID" 
 # Instantiates a client 
 spanner 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Spanner 
 
 . 
 new 
  
 project 
 : 
  
 project_id 
 # Your Cloud Spanner instance ID 
 instance_id 
  
 = 
  
 "my-instance" 
 # Your Cloud Spanner database ID 
 database_id 
  
 = 
  
 "my-database" 
 # Gets a reference to a Cloud Spanner instance database 
 database_client 
  
 = 
  
 spanner 
 . 
  client 
 
  
 instance_id 
 , 
  
 database_id 
 # Execute a simple SQL statement 
 results 
  
 = 
  
 database_client 
 . 
 execute_query 
  
 "SELECT 1" 
 results 
 . 
 rows 
 . 
  each 
 
  
 do 
  
 | 
 row 
 | 
  
 puts 
  
 row 
 end 
 

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: