Spanner的使用

快速入门,界面上的使用方式

使用console

  • 创建一个Cloud Spanner instance、database、table。
  • 添加schema.
  • 写入或更新数据
  • 查询数据
    参考官网

深入,通过Google Cloud SDK的方式操作Spanner

为了在本地、测试、线上等环境通过代码来操作Spanner DB需要进行以下步骤:

下载好Google Cloud SDK

本地开发之前,需要下载并安装Google Cloud SDK,然后就可以使用gcloud工具。gcloud提供了很多Google Cloud Platform的命令行接口。无论是通过命令行、脚本或其他自动化方式,都可以使用gcloud工具执行许多常见的平台任务。
例如,可以使用gcloud 去创建管理以下资源:

  • Google Compute Engine virtual machine instances and other resources
  • Google Cloud SQL instances
  • Google Kubernetes Engine clusters
  • Google Cloud Dataproc clusters and jobs
  • Google Cloud DNS managed zones and record sets
  • Google Cloud Deployment manager deployments
    也可以使用 gcloud 去部署 App Engine 应用,执行任务
    更多关于gcloud的功能看官方文档 gcloud Reference
设置你的project为gcloud command-line工具的默认project

命令如下:

gcloud config set project [MY_PROJECT_ID]
验证授权

Cloud Spanner使用 OAuth 2.0做API验证和授权。跑以下命令:

gcloud auth application-default login

或者

gcloud auth login | gmail@address

或者直接

gcloud auth login

它会打开一个类似https://accounts.google.com/o/oauth2/auth?redirect_uri=XXXXXX的谷歌授权页面,直接点击当前用户并且赋予权限即可。但是这个授权方式只使用于本地环境,如果要部署到production环境,需要使用另外一种方式,请参考 Getting credentials for server-centric flow和Setting Up Authentication for Server to Server Production Applications

使用gcloud command-line工具

本地开发环境的授权已经完成,接下来就是正式使用了。
首先验证一下 gcloud command-line 是否可以连接并且操作Cloud Spanner。

gcloud spanner instance-configs list

通过以上命令可以看到project下面的一系列Cloud Spanner instance 配置信息。包含 regional 和 multi-region 配置信息。

更多操作参考gcloud spanner。

代码中使用Cloud Spanner client library操作Spanner DB

查询数据的一个例子:

// Imports the Google Cloud client library
const Spanner = require('@google-cloud/spanner');

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

// Creates a client
const spanner = new Spanner({
  projectId: projectId,
});

// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

// Reads rows from the Albums table
const albumsTable = database.table('Albums');

const query = {
  columns: ['SingerId', 'AlbumId', 'AlbumTitle'],
  keySet: {
    all: true,
  },
};

albumsTable
  .read(query)
  .then(results => {
    const rows = results[0];

    rows.forEach(row => {
      const json = row.toJSON();
      console.log(
        `SingerId: ${json.SingerId}, AlbumId: ${json.AlbumId}, AlbumTitle: ${
          json.AlbumTitle
        }`
      );
    });
  })
  .catch(err => {
    console.error('ERROR:', err);
  })
  .then(() => {
    // Close the database when finished.
    return database.close();
  });

除此以外,还支持很多操作:建数据库,建表,对数据的增删改查等等。更多的详情参考以下官网。

  • Getting Started with Cloud Spanner in C#
  • Getting Started with Cloud Spanner in Go
  • Getting Started with Cloud Spanner in Java
  • Getting Started with Cloud Spanner in Node.js
  • Getting Started with Cloud Spanner in PHP
  • Getting Started with Cloud Spanner in Python
  • Getting Started with Cloud Spanner in Ruby
  • Getting Started with Cloud Spanner in REST

你可能感兴趣的:(Spanner的使用)