在 Datastore 模式下使用 Cloud Firestore

Firestore 是一个 NoSQL 文档数据库,能够自动扩缩、具备出色的性能,并且易于进行应用开发。Firestore 是 Datastore 的最新版本;与 Datastore 相比,这个新版本引入了多项改进功能。

由于 Datastore 模式 Firestore 针对服务器用例和 App Engine 进行了优化,因此,对于主要将由 App Engine 应用使用的数据库,我们建议使用 Datastore 模式 Firestore。原生模式 Firestore 最适合移动和实时通知用例。如需详细了解 Firestore 模式,请参阅选择原生模式或 Datastore 模式

本文档介绍了如何使用 Google Cloud 客户端库将数据存储在 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 的简单双字段类,并通过 Datastore 客户端 put 命令保存至 Datastore。然后,该应用使用 Datastore 客户端 NewQueryGetAll 命令,按降序顺序检索最近的十条访问记录。


// 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 模式查询需要使用一个或多个索引,您必须在随应用一起上传的 index.yaml 文件中指定这些索引。此文件可以手动创建,也可以在本地测试应用时自动生成。

本地测试

如果您需要在本地开发和测试应用,则可以使用 Datastore 模式模拟器

了解详情

如需全面了解 Datastore 模式(包括优化和概念),请参阅 Datastore 模式 Firestore 文档