GCP – BigTable Hello world golang

GCP

Target

Google provides Golang sample application(Go hello world) to learn BigTable

Originally, in my team, we are considering KVS in a team, and compare several choices. Now, we are using GCP in a team, but KVS is not GCP managed service. So, I investigated BigTable for comparison.

In this entry, I tried Go hello world in my own account. Let’s start Go hello world

Steps

  1. Create go project
  2. Import bigtable library with go get
  3. Copy codes from Go hello world
  4. Build (go build)
  5. Open GCP console and create an instance
  6. Run application (set project ID and instance Name via command line)

Create go project

mkdir gobigtable
cd gobigtable
go mod init gobigtable
touch main.go

Prepare main.go and go.mod

Import bigtable library with go get

go get cloud.google.com/go/bigtable 

go.mod and go.sum is updated, it’s ready to prepare codes.

Copy codes from Go hello world

Just copy codes. I did not explain details, but explain little bit for future use

  • Get project ID (GCP project ID), instance ID (Bigtable instance ID) using flag
  • Create Admin Client to manipulate Bigtable
  • Get target table instance (if cannot get, create new table)
  • Insert data into the target table
  • Test getting data using rowkey
  • Read all rows from the table
  • Delete table

After running this problem, your GCP does not remain anything (only instance)

Build

Let’s build this program

go build

Open GCP console and create an instance

Go to GCP console (web) and create an instance

“インスタンスを作成” Create Instance

Set Instance Name and Instance ID, Instance Name is only for display, we need Instance ID for a program

We need to select SSD or HDD, SSD is high performance, if no specific reason, we should choose SSD

This is cluster setting, important point is “Region”. I choose “Singapore” region, and Zone is anything. Bigtable price depends on Region, but you should use your application nearest place to reduce network latency

Instance will be established within 1 minutes. It’s ready to start program

Run application (set project ID and instance Name via command line)

In this time, I would like to run sample application in my local machine. To connect BigTable from my machine, need to activate gcloud command in my local machine

gcloud auth application-default login

Please select your target project

Let’s fun program

./gobigtable --project (GCP project name) --instance dj-first

Please use your project name, and instance name dj-first is my instance name

You can see following

You can see output like this.

コメント

タイトルとURLをコピーしました