Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Setup - Installing the gcloud CLI and setting up the Cloud Spanner API 
2.1.
Set up a Google Cloud project
2.2.
Set up authentication and authorization
2.3.
Run the Google Cloud CLI
3.
Create the database and query a database using gcloud CLI 
3.1.
Set a default project
3.2.
Instances
3.3.
List instance configurations
4.
Create an instance
4.1.
Set the default instance
4.2.
Create a database
4.3.
Create a schema
4.4.
Writing data
4.5.
Query data using SQL
4.6.
Cleanup
4.7.
Drop a database
4.8.
Delete an instance
5.
Frequently Asked Questions
5.1.
What is a Cloud Database?
5.2.
What type of database is Cloud spanner?
5.3.
How do Cloud SQL and Cloud spanner differ from one another?
5.4.
Is Spanner horizontally scalable?
5.5.
Does cloud spanner support MySQL?
6.
Conclusion
Last Updated: Mar 27, 2024

Command Line Tool in GCloud

Author Muskan Sharma
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Are you interested in knowing the set of tools provided by the google cloud? So let's learn about the Command Line Tool in GCloud 

The Google Cloud CLI is a set of tools to create and to manage Google Cloud resources. You can use these tools to perform many common platform tasks from command line or through scripts and other automation.

Setup - Installing the gcloud CLI and setting up the Cloud Spanner API 

You can follow the instructions on this page to set up the Cloud Spanner API for usage with the Google Cloud CLI and client libraries.

Set up a Google Cloud project

1.Open the Google Cloud console and navigate to the Projects page.

Go to the Projects page.

2. By clicking on the project name, you can start a brand-new Google Cloud project or access an already-existing one.

3.  Set your project as the default project for the Google Cloud CLI by opening a terminal window and changing MY PROJECT ID with your project ID (not your project name):

gcloud config set project MY_PROJECT_ID

4. A cloud project should have billing enabled.

5. For project, enable the Cloud Spanner API.

Enable the Cloud Spanner API

Set up authentication and authorization

OAuth 2.0 is used by Cloud Spanner for API authentication and authorization.

Use the Google Cloud CLI and Application Default Credentials to run your code locally while developing and testing. Open a command prompt and enter:

  gcloud auth application-default login

Run the Google Cloud CLI

Run the gcloud command-line tool for interaction with the Cloud Spanner now that your development environment and authentication have been configured:

gcloud Spanner instance-configs list

Your setup is finished!

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Create the database and query a database using gcloud CLI 

The gcloud command-line program is used to guide you through the following tasks in this tutorial.

Set a default project

Set the ID of the Google Cloud Platform project as the default project for the Google Cloud CLI if you haven't already:

gcloud config set project PROJECT_ID

The first input to gcloud Spanner for each of the commands following must be —project PROJECT ID if the default project is not set. For Instance:

gcloud spanner --project=PROJECT_ID instance-configs list

Instances

When using Cloud Spanner for the first time, you must build an instance, which is a resource allocation that Cloud Spanner databases use. You get to pick the location of your data storage and the Instance's computation capacity when you create it.

Instances and instance configurations

You must choose an instance configuration before you can establish an instance. An instance configuration is a kind of blueprint for your Instance that specifies the geographical placement and replication of your Cloud Spanner data.

List instance configurations

When you create an instance, you give it an instance configuration, which determines the location of the database and how they will be replicated there.

Command available set of Instance for your project 

gcloud Spanner instance-configs list

Create an instance

Using the regional Instance of configuration regional-us-central1 with one node, build an instance with the name test-instance and the display name My Instance.

gcloud spanner instances create test-instance --config=regional-us-central1 \
    --description="My Instance" --nodes=1

Set the default instance

If you don't specify an instance in your command, Cloud Spanner will utilize the default instance that you specify. Default instance setting

gcloud config set spanner/instance test-instance

Create a database

The example-db database should be created. Google Standard SQL is the default dialect for the database.

gcloud spanner databases create example-db

Create a schema

The Data Definition Language (DDL) for Cloud Spanner can be used to build, modify, or remove indexes as well as tables.

Setting up the 2 table.

gcloud spanner databases ddl update example-db \
  --ddl='CREATE TABLE Singers ( SingerId INT64 NOT NULL, FirstName STRING(1024), LastName STRING(1024), SingerInfo BYTES(MAX) ) PRIMARY KEY (SingerId)'
gcloud spanner databases ddl update example-db \
  --ddl='CREATE TABLE Albums ( SingerId INT64 NOT NULL, AlbumId INT64 NOT NULL, AlbumTitle STRING(MAX)) PRIMARY KEY (SingerId, AlbumId), INTERLEAVE IN PARENT Singers ON DELETE CASCADE.'

Writing data

Adding some data to our database

gcloud spanner rows insert --database=example-db \
      --table=Singers \
      --data=SingerId=1,FirstName=Marc,LastName=Richards

gcloud spanner rows insert --database=example-db \
      --table=Singers \
      --data=SingerId=2,FirstName=Catalina,LastName=Smith

gcloud spanner rows insert --database=example-db \
      --table=Singers \
      --data=SingerId=3,FirstName=Alice,LastName=Trentor

gcloud spanner rows insert --database=example-db \
      --table=Albums \
      --data=SingerId=1,AlbumId=1,AlbumTitle="Total Junk"

gcloud spanner rows insert --database=example-db \
      --table=Albums \
      --data=SingerId=2,AlbumId=1,AlbumTitle="Green"

gcloud spanner rows insert --database=example-db \
      --table=Albums \
      --data=^:^SingerId=2:AlbumId=2:AlbumTitle="Go, Go, Go"

Query data using SQL

Executing  a query command 

gcloud spanner databases execute-sql example-db \
    --sql='SELECT SingerId, AlbumId, AlbumTitle FROM Albums'

Cleanup

Drop the database and get rid of the Instance you made in order to keep your Google Cloud account from being charged extra for the resources used in this tutorial.

Drop a database

To drop a database

      gcloud spanner databases delete example-db

Delete an instance

To drop an instance 

      gcloud spanner instances delete test-instance

Frequently Asked Questions

What is a Cloud Database?

A database service created and accessible using a cloud platform is known as a cloud database.

What type of database is Cloud spanner?

Running on Google Cloud, Google Cloud Spanner is a distributed relational database service.

How do Cloud SQL and Cloud spanner differ from one another?

Cloud SQL has a 30 TB data storage capacity. More than 30 TB of data is kept in Cloud Spanner.

Is Spanner horizontally scalable?

Since Spanner automatically provides dynamic data resharding and data replication, achieving horizontal or vertical scaling requires no work.

Does cloud spanner support MySQL?

Different sets of data types are supported by Spanner versus MySQL.

Conclusion

This blog has extensively discussed Command Line Tool in GCloud, Installing gcloud CLI, etc. We hope This article was helpful and it enhanced your knowledge about the Command Line Tool in GCloud. If you want to learn more deeply, check out the excellent content on the Coding Ninjas Website:

Cloud Logging in GCP, Monitoring Agent

Refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. 

Refer to the links problemstop 100 SQL problemsresources, and mock tests to enhance your knowledge.

For placement preparations, visit interview experiences and interview bundle.

Thank You

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass