A beginners guide to read/write data in to Google DataStore using .NET Core 2.1 Console Application.
Before you begin, I assume you already have, .NET Core 2.1 installed in your PC or Mac. If not be my guest and install it in your machine. Here we go with the link
.NET Downloads for Linux, macOS, and Windows
Free downloads for building and running .NET applications on Linux, macOS, and Windows. Get downloads for the latest…
Follow the instructions in the download site and you are good to go. We will be going full command line and will use Visual Studio Code to write code. If you don’t have Visual Studio Code, visit the below link and you are good to go.
Visual Studio Code - Code Editing. Redefined
Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud…
Umm, yes thats it, you have all the tools you need at your side. Next step is to have a Google Cloud Platform account. You can get a free account here.
Google Cloud including GCP & G Suite - Try Free | Google Cloud
Build, innovate, and scale with Google Cloud Platform. Collaborate and be more productive with G Suite. See what's…
Use your existing google account to create GCP account. Instructions in the page are fare simple to follow. All right !!! Let’s get our teeth in to this.
Preparing the Project
Nice, by this time you have all installed and you have a GCP account too. Lets fire-up the terminal (or Command in Windows) and type the following command to create the console application.
$ dotnet new console -o datastoreconsole
This will create you the console application with .NET Core. Get into the newly created folder.
$ cd datastoreconsole
And do a quick run using
$ dotnet run
Next step, we need to add the .NET client library for Google Datastore. You can simply execute this command in the shell.
$ dotnet add package Google.Cloud.Datastore.V1
And then do a restore to get the packages to your environment executing this command.
$ dotnet restore
Excellent !!! Let’s open the project folder using Visual Studio code and click on the project file (dotnetstoreconsole.csproj) and the <ItemGroup> should look like this.
OK, our local environment is almost done. Let’s prepare the GCP environment. For that we will need:
- A project at GCP
- A DataStore Database for the above project
- A Service Account with a key to access the DataStore
1. Create a project at GCP
By the time you reach here you must have a free GCP account. If not go back to top and follow the link and create one. Follow this link
Google Cloud Platform
Google Cloud Platform lets you build, deploy, and scale applications, websites, and services on the same infrastructure…
After all those logins and stuff, you will reach the GCP Console. In the console, and in the left menu pane. click on IAM & admin and then Manage resources
This will lead you to the resources you have. Now in the top click on + CREATE PROJECT button. Give a project name and leave the default for Location. Click CREATE button. GCP will take few seconds to create the project.
Do you see that small line below the Project Name? Yep thats very important. Make sure you copy it down. You will need the Project ID to access the DatasStore.
2. Create the DataStore Database for the project
Now click on the Google Cloud Platform logo top right. Next to the logo you should see a drop-down. This drop down will contain the projects you have in the GCP. Click on it and select the project you created in the above step.
Now your top left menu bar should look like this,
Now, scroll down the left menu pane till you reach Datastore label. And click on the label. You will be presented with what sort of database service you need in your project. Click on the blue SELECT button, in the Cloud Datastore card.
Now click the blue NEXT button bottom of the screen. Next stop will be for you to select a location close to you. Select one and hit CREATE DATABASE button.
GCP will take few seconds to create your database. And one more task left in the list. Let’s create a Service Account to access this database.
3. Create a Service Account
Again lets click on the Google Cloud Platform logo on top left to get the menu. And move the mouse IAM & admin and then click on Service account. Now in the Service accounts pane, click on + CREATE SERVICE ACCOUNT button.
In this page you need to give a Service account name, an id (Id will be automatically created for you, if you are not happy with the one create you can always choose one) and a description. Description is optional. And then click on the blue CREATE button.
Now you have to select a role for the service account. Click the “Select a role” drop down and select Owner
And then click the blue, CONTINUE button. Now one last step here. We need to create a key. Now click on the + CREATE KEY button. Select JSON in the popup and click the blue CREATE button.
This will download a JSON file with information required to access services in the GCP. Make sure you copy it to a location and you will need it to access the Datastore.
OK, we are done with the setup let’s do some coding.
Open Visual Studio Code and open the folder contains the console application we just created above. Let’s click on the “Program.cs” add the following using directive.
Lets add the following method right below the Main() method inside the “Program.cs”
OK lets go through this code. In the line 3 we have created a string to store the projectId from GCP. As you remember we copied it down when we created the project. Line 6 we have tell what kind of an entity is this to the Datastore. In the line 8 we have created a Database object for the DataStore using the projectId we stored. Then we created an entity list. If you see at the line 17 we have created a key for our entity. This is a unique key identifier for each entity. And then lines 18 to 20 we have created key value pairs.
Finally from lines 25 to 29 we used transaction object to send the list of movies to the datastore.
Now replace the code in the Main() method with this one.
Now go back to your shell (or Command in Windows), execute the following command to setup the environment with the JSON key we downloaded. This is required to access the Datastore service.
The syntax should look like this
$ export GOOGLE_APPLICATION_CREDENTIALS=[path_to_your_key_file]
and the here is the example with my JSON key.
$ export GOOGLE_APPLICATION_CREDENTIALS=~/PlayGround/GCP/keys/dotnetds-222013-fe4248ec93fb.json
You are almost done. Next lets execute our console application and see. If everything is OK you should see an output like this.
Excellent. Lets make sure, all the data is sent to the Datastore. Now go back to your GCP Console. And click on the Google Cloud Platform logo on the top right of the window. In the menu scroll down and click Datastore
Your data should look like this in the Datastore
Excellent. We have saved our data at the data store. Let’s retrieve them writing this method.
Go back to your “Program.cs” add the following GetMovies() method next to the CreateMovies() method.
Again in the above method we have created a query in line 9 and then executed the query in line 10, picked up all the movies. Finally created a string in line 12 with returned movies list.
And modify the Main() as shown below.
Now go back to your shell (or Command in Windows) execute the following command.
Excellent and thats it. Enjoy coding.
Now you have a starting base on .NET Core and Google DataStore. Follow this documentation for more information.
Google Cloud Datastore Documentation | Cloud Datastore | Google Cloud
Google Cloud Datastore is a NoSQL document database built for automatic scaling, high performance, and ease of…
Learn how to use .NET to create a variety of applications on any platform using C#, Visual Basic, and F#. Browse API…
Grab the source code for this tutorial