{GCP Cloud Spanner}gcloud CLI を使用してデータベースを作成し、クエリを実行する

 

https://cloud.google.com/spanner/docs/getting-started/gcloud?hl=ja

 

Cloud Spanner は、リクエスト負荷やデータサイズに基づいてデータを自動的にシャーディングしてパフォーマンスを最適化します。

 

1 ノードあたりの料金/時間 $1.17
プロビジョニングしたコンピューティング容量に対して、最低でも 1 時間分の料金が課金されます。

ストレージ(GB 単位/月)$0.39


※台湾がコンピューティング $0.90なので、台湾(asia-east1)で実施


-- 1. 前作業

gcloud init
gcloud auth list

gcloud --version

gcloud projects create project01-9999999 \
--name="project01"

gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-east1 --quiet
gcloud config set compute/zone asia-east1-a --quiet

gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111

gcloud services enable compute.googleapis.com --project project01-9999999

gcloud components update


-- 2. Cloud Spanner API を有効にする

gcloud services list --enabled

gcloud services enable  spanner.googleapis.com


-- 3. 認証と権限付与の設定
Spanner は、API の認証と権限付与に OAuth 2.0 を使用します。

gcloud auth application-default login


-- 4. インスタンスの作成

gcloud spanner instance-configs list

gcloud spanner instance-configs describe regional-asia-east1


gcloud spanner instances create instance01 \
--config=regional-asia-east1 \
--description="instance01" \
--instance-type=provisioned \
--nodes=1

gcloud spanner instances list
gcloud spanner instances describe instance01


-- 5. データベースの作成

gcloud spanner databases create db01 \
--instance=instance01


gcloud spanner databases list \
--instance=instance01

gcloud spanner databases describe  db01 \
--instance=instance01

 

-- 6. テーブルの作成

gcloud spanner databases ddl update db01 \
--instance=instance01 \
--ddl='create table tab1 
         ( col11 int64 not null,
           col12 string(1024), 
           col13 string(1024), 
           col14 bytes(max) ) 
         primary key (col11)'


-- 7. データを書き込む

gcloud spanner rows insert \
--instance=instance01 \
--database=db01 \
--table=tab1 \
--data=col11=1,col12="Marc",col13="Richards"

 


-- 8. SQL を使用したデータのクエリ


gcloud spanner databases execute-sql db01 \
--instance=instance01 \
--sql='select col11, col12, col13, col14 from tab1'

 


-- 9. クリーンアップ

gcloud spanner databases delete db01 \
--instance=instance01

gcloud spanner databases list \
--instance=instance01

gcloud spanner instances delete instance01
gcloud spanner instances list

 

gcloud projects list
gcloud projects delete project01-9999999