Cloud Firestore の Datastore モードでの使用

Firestore は、自動スケーリングと高性能を実現し、アプリケーション開発を簡素化するように構築された NoSQL ドキュメント データベースです。これは Datastore の最新バージョンで、Datastore のいくつかの点が改善されています。

Datastore モードの Firestore は、サーバーのユースケースと App Engine 用に最適化されているため、主に App Engine アプリで使用されるデータベースには Datastore モードの Firestore を使用することをおすすめします。ネイティブ モードの Firestore は、モバイルとリアルタイムの通知のユースケースに最適です。Firestore モードの詳細については、ネイティブ モードと Datastore モードからの選択をご覧ください。

このドキュメントでは、Google Cloud Client Library を使用して Datastore モードのデータベースにデータを格納する方法、または Datastore モードのデータベースからデータを取得する方法について説明します。

前提条件と設定

App Engine の Go 用「Hello, World!」の説明に従って環境とプロジェクトを設定し、App Engine での Go アプリの構造を理解します。このドキュメントで説明しているサンプルアプリを実行する際に必要となるため、プロジェクト ID を書き留めておきます。

リポジトリのクローン作成

サンプルをダウンロード(クローンを作成)します。

go get -d -v github.com/GoogleCloudPlatform/golang-samples/datastore
cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/appengine_flexible/datastore

プロジェクト構成の編集と依存関係の設定

app.yaml で、プロジェクト用に GCLOUD_DATASET_ID を設定します。この値はプロジェクト ID です。

# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

runtime: go
env: flex

automatic_scaling:
  min_num_instances: 1

env_variables:
  GCLOUD_DATASET_ID: your-project-id

アプリケーション コード

このサンプル アプリケーションは訪問者の IP のログ記録、取得、表示を行います。ログエントリがタイプ visit の単純な 2 フィールド クラスであり、Datastore クライアントの put コマンドを使用して Datastore モードに保存されることを確認できます。さらに、Datastore クライアントの NewQuery および GetAll コマンドを使用して、訪問者のアクセスの最新 10 件が降順で取得されます。


// Sample datastore demonstrates use of the cloud.google.com/go/datastore package from App Engine flexible.
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"cloud.google.com/go/datastore"
	"google.golang.org/appengine"
)

var datastoreClient *datastore.Client

func main() {
	ctx := context.Background()

	// Set this in app.yaml when running in production.
	projectID := os.Getenv("GCLOUD_DATASET_ID")

	var err error
	datastoreClient, err = datastore.NewClient(ctx, projectID)
	if err != nil {
		log.Fatal(err)
	}

	http.HandleFunc("/", handle)
	appengine.Main()
}

func handle(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}

	ctx := context.Background()

	// Get a list of the most recent visits.
	visits, err := queryVisits(ctx, 10)
	if err != nil {
		msg := fmt.Sprintf("Could not get recent visits: %v", err)
		http.Error(w, msg, http.StatusInternalServerError)
		return
	}

	// Record this visit.
	if err := recordVisit(ctx, time.Now(), r.RemoteAddr); err != nil {
		msg := fmt.Sprintf("Could not save visit: %v", err)
		http.Error(w, msg, http.StatusInternalServerError)
		return
	}

	fmt.Fprintln(w, "Previous visits:")
	for _, v := range visits {
		fmt.Fprintf(w, "[%s] %s\n", v.Timestamp, v.UserIP)
	}
	fmt.Fprintln(w, "\nSuccessfully stored an entry of the current request.")
}

type visit struct {
	Timestamp time.Time
	UserIP    string
}

func recordVisit(ctx context.Context, now time.Time, userIP string) error {
	v := &visit{
		Timestamp: now,
		UserIP:    userIP,
	}

	k := datastore.IncompleteKey("Visit", nil)

	_, err := datastoreClient.Put(ctx, k, v)
	return err
}

func queryVisits(ctx context.Context, limit int64) ([]*visit, error) {
	// Print out previous visits.
	q := datastore.NewQuery("Visit").
		Order("-Timestamp").
		Limit(10)

	visits := make([]*visit, 0)
	_, err := datastoreClient.GetAll(ctx, q, &visits)
	return visits, err
}

index.yaml ファイルの使用

このサンプルアプリは簡単なクエリを実行しています。複雑な Datastore クエリには 1 つ以上のインデックスが必要です。インデックスは、アプリとともにアップロードする index.yaml ファイルで指定する必要があります。

ローカルテスト

アプリケーションを開発してローカルでテストする必要がある場合は、Datastore モードエミュレータを使用できます。

詳細情報

最適化やコンセプトなどの Datastore モードの詳細は、Datastore モードの Firestore に関するドキュメントをご覧ください。