DEREK DOWNEY: Hi, everyone.
I'm Derek Downey, a developer advocate at Google.
I want to show you how easy it is
to get started developing on Cloud Spanner, Google's highly
scalable relational database.
Over the course of several videos,
you will learn how to set up your development environment
to work with Spanner.
You will see how to set up a Spanner instance and database,
as well as details on defining your schema.
And finally, you will learn how to read and write
your data to Spanner instances using standard SQL transactions
and Spanner mutations.
In this first quick start video, I
will connect a simple Java application
to the Cloud Spanner emulator.
The emulator allows you to mimic Spanner functionality
on your local development machine.
This quick start will also highlight
creating the Spanner schema, as well as reading
and writing data into Spanner.
This won't be a typical "Hello, world" application.
I'm going to build the start of a simple banking app.
This app will allow customers to create an account and log in.
I will use the JetBrains IntelliJ IDE.
If you don't use IntelliJ, don't worry.
You can still get started quickly
using your IDE of choice.
So let's get to it.
I will be connecting into Cloud Spanner in this banking
application.
However, since I'm just getting started,
I want to use the Cloud Spanner emulator, which runs directly
on my machine.
The Spanner emulator mimics the functionality
of a Spanner instance.
I can switch to a real Spanner instance
later when this application is ready for collaboration.
So the first thing I need to do is install the gcloud command
line tool.
There are instructions in the video description
of how to do this for your operating system.
But once gcloud is installed, I will create a new configuration
for our banking project.
The project I specified doesn't have
to be a real Google Cloud project, since the emulator
won't validate it.
But this is needed for our application
to use Spanner's Java library.
All right.
Now I will start the emulator as a background task.
Once that is running, I will create an instance called
"banking."
This uses the emulator configuration,
since we are using the emulator.
Now, we have our Spanner instance running.
But it doesn't do any good without a database and schema.
I could create this in the command line.
But I want to make sure this step is repeatable,
since I will eventually move this to a real Spanner
instance.
So I have created a file that has the DDL commands to create
the database and my table.
For now, I am creating a single customers table
that stores some basic information about the user,
including a username and password for authentication.
Once I've provided the instance and database configuration,
I can run the file from my IDE.
Great.
Now I have the database defined with a customers table.
One neat thing is the ability to explore the database directly
within the IDE.
To do this in IntelliJ, I need to add the Cloud Spanner data
source from the database tools.
With that added, I need to populate the instance
and project information.
Since I'm using the emulator, I don't
need to authenticate to the instance.
But I will need the URL to connect to the local host
on port 9010, which is the default for the emulator.
I'll also pass in the autoconfig emulator equals true parameter.
Perfect.
I could see my schema that I created previously.
I'm almost done.
To finish up, I want to write some data
and then read it back.
I'll need to be able to have users sign up to my bank.
And I do that in my createCustomer function.
I am using standard SQL within a read/write transaction
to insert a user into the customers table based on input
provided to the application.
Once a customer is signed up, then they will need to log in.
The login function will issue a read from Spanner
to validate the user's credentials.
With the ability for customers to sign up and log in,
I am ready to run the application.
I tell the application that I want to sign up.
And then I provide my information.
Once my customer is created, I can log in
by providing username and password.
And I've successfully logged in.
Back in my editor, I can see the new user
that was entered from the application.
Fantastic.
It didn't take long to get my simple application interacting
with the Spanner emulator.
This is great for getting my feet wet with Spanner.
But soon, I will need other developers
to collaborate on this application.
And for that, I will need to create an actual Spanner
instance.
In the next video, I will go over
what a Spanner instance is and the different options
for creating one.
In the meantime, go ahead and try out the Spanner emulator
for yourself.
And let us know in the comments your experience.
[MUSIC PLAYING]