Skip to main content

How to use Google Cloud SDK?

Let’s go through some practical examples of the services that can be accessed using Google Cloud SDK which is an easy way to interact with Google Cloud service. SDK refers to software development kit and it can enable one to run scripts or programs that help to automate tasks that are repetitive as for example in deploying virtual machines.

What are Cloud Client Libraries?

Google Cloud SDK contains cloud client libraries which allow the API access by program implemented in the supported language like Python, Ruby, PHP, C#, Java, Go, and Node. js. These libraries make it easier to call APIs and also contribute towards the reduction of the amount of code. They can be installed using package management tools such as pip and npm among others.

Cloud client Libraries
Cloud client Libraries

Prerequisites and Installation

Before we dive into the lab, let’s cover the prerequisites:

  • A GCP account
  • The right version of the desired client library (in this case, Python)
  • Visual Studio Code (or your preferred IDE)

Install Google Cloud SDK

To install the Google Cloud SDK, go to https://cloud.google.com/sdk/docs/install-sdk and follow the instructions.

Navigating to Google cloud SDK docs
Navigating to Google cloud SDK docs

Download the Google Cloud CLI installer and follow the steps in the wizard to install the CLI.

Download the Google Cloud CLI installer
Download the Google Cloud CLI installer
Finish Installation steps using Wizard
Finish Installation steps using Wizard
Google Cloud CLI
Google Cloud CLI

Configuring the Service Account

After the SDK install we have to set up the Google Cloud service account and download the credentials. To do this, let’s generate a key for our service account and download the JSON file with the key.

For that, firstly Go to IAM and ADMIN and from there select “Service Account” from the left navigation menu.

Navigating to Service Accounts
Navigating to Service Accounts

Next, from the list of available Service Accounts click on the three dots besides your required Service Account and select Manage Keys.

Add key to your service account
Add key to your service account
Manage key for your service account
Manage key for your service account

Once you are on the Keys page, click on “ADD KEY” button and select “Create New Key”.

Create new key
Create new key

Then from the popup, select JSON and click on Create button.

Selecting Key Type
Selecting Key Type

Your private key will be saved on your computer.

Next, in order to view all the Service Accounts from your CLI, you have to run the following command. It will provide a list of all available Service Accounts along with their status

Viewing all service accounts from CLI
Viewing all service accounts from CLI

Using the Python Client Library

Now, let’s open Visual Studio Code and let’s start writing the Python program required for our project. We’ll access the BigQuery API through our Python Cloud Library.

First step is to let our IDE find our Service Account Credentials in order to connect us to the desired Google Cloud Service. For that we will create an environment variable that will contain the path of JSON file that contains the key for our Service Account.

Create environment variable containing path of the key
Create environment variable containing path of the key

Next, we’ll import the BigQuery client and create a BigQuery client. Next, we will come up with a query that will seek to get the ten most viewed questions with the tag Google BigQuery on the platform – stack overflow.

import os
os.environ[“GOOGLE_APPLICATION_CREDENTIALS”] = “C:/Users/clicks/Downloads/oceanic-toolbox-352912-85c6285090dc.json”
from google.cloud import bigquery

client = bigquery.Client()
# Perform a query.
QUERY=(“””
SELECT
CONCAT(
‘https://stackoverflow.com/questions/’,
CAST(id as STRING)) as url,
view_count
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE tags like ‘%google-bigquery%’
ORDER BY view_count DESC
LIMIT 10″””)
Query to get the ten most viewed questions with the tag Google BigQuery on stack overflow
Query to get the ten most viewed questions with the tag Google BigQuery on stack overflow

Now we need to make an API request:

query_job = client.query(QUERY)

results = query_job.result() # Waits for job to complete.

# Print result
for row in results:
print(“{} : {} views”.format(row.url, row.view_count))
.

Running the Program

Finally, we’ll run our program in the terminal and see the results of our query. The above query will fetch the required data from the Stack Overflow platform

Running the program
Running the program
0
    0
    Your Cart
    Your cart is emptyReturn to Courses