Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spanner samples #313

Merged
merged 3 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@google-cloud/monitoring": "0.1.4",
"@google-cloud/pubsub": "0.8.0",
"@google-cloud/resource": "0.6.0",
"@google-cloud/spanner": "0.1.0",
"@google-cloud/speech": "0.6.0",
"@google-cloud/storage": "0.7.0",
"@google-cloud/translate": "0.7.0",
Expand Down
130 changes: 130 additions & 0 deletions spanner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>

# Cloud Spanner Node.js Samples

[Cloud Spanner][spanner_docs] is a managed, mission-critical, globally
consistent and scalable relational database service. Cloud Spanner solves the
need for a horizontally-scaling database with consistent global transaction and
SQL semantics.

## Table of Contents

* [Setup](#setup)
* [Samples](#samples)
* [Getting started with Google Cloud Spanner API](#getting-started-with-google-cloud-spanner-api)

## Setup

1. Read [Prerequisites][prereq] and [How to run a sample][run] first.
1. Install dependencies:

npm install

[prereq]: ../README.md#prerequisities
[run]: ../README.md#how-to-run-a-sample

## Samples

### Getting started with Google Cloud Spanner API

View the [Spanner documentation][spanner_docs] or the [samples][spanner_samples].

__Run the samples:__

```sh
node schema.js --help
```

```
Commands:
createDatabase <instanceName> <databaseName> Creates an example database with two tables in a Cloud Spanner instance.
addColumn <instanceName> <databaseName> Adds an example MarketingBudget column to an example Cloud Spanner
table.
queryNewColumn <instanceName> <databaseName> Executes a read-only SQL query against an example Cloud Spanner table
with an additional column (MarketingBudget) added by addColumn.

Options:
--help Show help [boolean]

Examples:
node schema.js createDatabase "my-instance" "my-database"
node schema.js addColumn "my-instance" "my-database"
node schema.js queryNewColumn "my-instance" "my-database"

For more information, see https://cloud.google.com/spanner/docs
```

```sh
node crud.js --help
```

```
Commands:
update <instanceName> <databaseName> Modifies existing rows of data in an example Cloud Spanner table.
query <instanceName> <databaseName> Executes a read-only SQL query against an example Cloud Spanner table.
insert <instanceName> <databaseName> Inserts new rows of data into an example Cloud Spanner table.
read <instanceName> <databaseName> Reads data in an example Cloud Spanner table.

Options:
--help Show help [boolean]

Examples:
node crud.js update "my-instance" "my-database"
node crud.js query "my-instance" "my-database"
node crud.js insert "my-instance" "my-database"
node crud.js read "my-instance" "my-database"

For more information, see https://cloud.google.com/spanner/docs
```

```sh
node indexing.js --help
```

```
Commands:
createIndex <instanceName> <databaseName> Creates a new index in an example Cloud Spanner table.
createStoringIndex <instanceName> <databaseName> Creates a new value-storing index in an example Cloud Spanner table.
queryIndex <instanceName> <databaseName> Executes a read-only SQL query against an example Cloud Spanner
table using an existing index.
readIndex <instanceName> <databaseName> Reads data from an example Cloud Spanner table using an existing
index.
readStoringIndex <instanceName> <databaseName> Reads data from an example Cloud Spanner table using an existing
storing index.

Options:
--help Show help [boolean]

Examples:
node indexing.js createIndex "my-instance" "my-database"
node indexing.js createStoringIndex "my-instance" "my-database"
node indexing.js queryIndex "my-instance" "my-database"
node indexing.js readIndex "my-instance" "my-database"
node indexing.js readStoringIndex "my-instance" "my-database"

For more information, see https://cloud.google.com/spanner/docs
```

```sh
node transaction.js --help
```

```
Commands:
readOnly <instanceName> <databaseName> Execute a read-only transaction on an example Cloud Spanner table.
readWrite <instanceName> <databaseName> Execute a read-write transaction on an example Cloud Spanner table.

Options:
--help Show help [boolean]

Examples:
node transaction.js readOnly "my-instance" "my-database"
node transaction.js readWrite "my-instance" "my-database"

For more information, see https://cloud.google.com/spanner/docs
```

For more information, see [the docs][spanner_docs].

[spanner_samples]: ../
[spanner_docs]: https://cloud.google.com/spanner/docs/
203 changes: 203 additions & 0 deletions spanner/crud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/**
* Copyright 2017, Google, Inc.
* 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
*
* http://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.
*/

'use strict';

function updateData (instanceId, databaseId) {
// [START update_data]
// Imports the Google Cloud client library
const Spanner = require('@google-cloud/spanner');

// Instantiates a client
const spanner = Spanner();

// Uncomment these lines to specify the instance and database to use
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

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

// Update a row in the Albums table
// Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so they
// must be converted to strings before being inserted as INT64s
const albumsTable = database.table('Albums');

albumsTable.update([
{ SingerId: '1', AlbumId: '1', MarketingBudget: '100000' },
{ SingerId: '2', AlbumId: '2', MarketingBudget: '500000' }
])
.then(() => {
console.log('Updated data.');
});
// [END update_data]
}

function insertData (instanceId, databaseId) {
// [START insert_data]
// Imports the Google Cloud client library
const Spanner = require('@google-cloud/spanner');

// Instantiates a client
const spanner = Spanner();

// Uncomment these lines to specify the instance and database to use
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

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

// Instantiate Spanner table objects
const singersTable = database.table('Singers');
const albumsTable = database.table('Albums');

Promise.all([
// Inserts rows into the Singers table
// Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so
// they must be converted to strings before being inserted as INT64s
singersTable.insert([
{ SingerId: '1', FirstName: 'Marc', LastName: 'Richards' },
{ SingerId: '2', FirstName: 'Catalina', LastName: 'Smith' },
{ SingerId: '3', FirstName: 'Alice', LastName: 'Trentor' },
{ SingerId: '4', FirstName: 'Lea', LastName: 'Martin' },
{ SingerId: '5', FirstName: 'David', LastName: 'Lomond' }
]),

// Inserts rows into the Albums table
albumsTable.insert([
{ SingerId: '1', AlbumId: '1', AlbumTitle: 'Go, Go, Go' },
{ SingerId: '1', AlbumId: '2', AlbumTitle: 'Total Junk' },
{ SingerId: '2', AlbumId: '1', AlbumTitle: 'Green' },
{ SingerId: '2', AlbumId: '2', AlbumTitle: 'Forever Hold your Peace' },
{ SingerId: '2', AlbumId: '3', AlbumTitle: 'Terrified' }
])
])
.then(() => {
console.log('Inserted data.');
});
// [END insert_data]
}

function queryData (instanceId, databaseId) {
// [START query_data]
// Imports the Google Cloud client library
const Spanner = require('@google-cloud/spanner');

// Instantiates a client
const spanner = Spanner();

// Uncomment these lines to specify the instance and database to use
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

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

const query = {
sql: 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums'
};

// Queries rows from the Albums table
database.run(query)
.then((results) => {
const rows = results[0];

rows.forEach((row) => {
const json = row.toJSON();
console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`);
});
});
// [END query_data]
}

function readData (instanceId, databaseId) {
// [START read_data]
// Imports the Google Cloud client library
const Spanner = require('@google-cloud/spanner');

// Instantiates a client
const spanner = Spanner();

// Uncomment these lines to specify the instance and database to use
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

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

// Read 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.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`);
});
});
// [END read_data]
}

const cli = require(`yargs`)
.demand(1)
.command(
`update <instanceName> <databaseName>`,
`Modifies existing rows of data in an example Cloud Spanner table.`,
{},
(opts) => updateData(opts.instanceName, opts.databaseName)
)
.command(
`query <instanceName> <databaseName>`,
`Executes a read-only SQL query against an example Cloud Spanner table.`,
{},
(opts) => queryData(opts.instanceName, opts.databaseName)
)
.command(
`insert <instanceName> <databaseName>`,
`Inserts new rows of data into an example Cloud Spanner table.`,
{},
(opts) => insertData(opts.instanceName, opts.databaseName)
)
.command(
`read <instanceName> <databaseName>`,
`Reads data in an example Cloud Spanner table.`,
{},
(opts) => readData(opts.instanceName, opts.databaseName)
)
.example(`node $0 update "my-instance" "my-database"`)
.example(`node $0 query "my-instance" "my-database"`)
.example(`node $0 insert "my-instance" "my-database"`)
.example(`node $0 read "my-instance" "my-database"`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/spanner/docs`);

if (module === require.main) {
cli.help().strict().argv;
}
Loading