How to get started with Cloud Spanner in 5 minutes
The general availability of Cloud Spanner is really good news for developers. For the first time, you have direct access to horizontally-scaling, cloud-native infrastructure that provides global transactions (think apps that involve payments, inventory, ticketing, or financial trading) and that defaults to the “gold standard” of strong/external consistency without compromising latency. Try that with either a traditional RDBMS or non-relational database.
Thanks to the GCP Free Trial that offers $300 in credits for one year, you can get your feet wet with a single-node Cloud Spanner cluster over the course of a couple weeks. Here’s how to do that, using the Spanner API via gcloud. (Click here for the console-based approach.)
1. In Cloud Console, go to the Projects page and either create a new project or open an existing project by clicking on the project name.
2. Open a terminal window and set your project as the default for gcloud. Do this by substituting your project ID (not project name) with the command:
gcloud config set project [MY_PROJECT_ID]
gcloud auth application-default login
API client libraries now automatically pick up the created credentials. You need to run the command only once per local user environment. (Note: This approach is suitable for local development; for production use, you’ll want to use a different method for auth.)
6. Next, create a single-node instance:
gcloud spanner instances create test-instance
--description="Test Instance" --nodes=1
7. Finally, create a database. To create a database called test-db:
gcloud spanner databases create test-db --instance=test-instance
Alternatively, you can download sample data and interact with it using the language of your choice.That’s it — you now have your very own Cloud Spanner database. Again, your GCP credit should allow you to run it cost-free for a couple weeks. From there, you can download sample data and interact with it using the language of your choice.