|
| 1 | +// Copyright 2018 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +async function createAndExecuteQueryPartitions( |
| 18 | + instanceId, |
| 19 | + databaseId, |
| 20 | + projectId |
| 21 | +) { |
| 22 | + // [START spanner_batch_client] |
| 23 | + // Imports the Google Cloud client library |
| 24 | + const {Spanner} = require('@google-cloud/spanner'); |
| 25 | + |
| 26 | + /** |
| 27 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 28 | + */ |
| 29 | + // const projectId = 'my-project-id'; |
| 30 | + // const instanceId = 'my-instance'; |
| 31 | + // const databaseId = 'my-database'; |
| 32 | + |
| 33 | + // Creates a client |
| 34 | + const spanner = new Spanner({ |
| 35 | + projectId: projectId, |
| 36 | + }); |
| 37 | + |
| 38 | + // Gets a reference to a Cloud Spanner instance and database |
| 39 | + const instance = spanner.instance(instanceId); |
| 40 | + const database = instance.database(databaseId); |
| 41 | + |
| 42 | + let transaction; |
| 43 | + |
| 44 | + try { |
| 45 | + [transaction] = await database.createBatchTransaction(); |
| 46 | + |
| 47 | + const query = { |
| 48 | + sql: 'SELECT * FROM Singers', |
| 49 | + // DataBoost option is an optional parameter which can also be used for partition read |
| 50 | + // and query to execute the request via spanner independent compute resources. |
| 51 | + dataBoostEnabled: true, |
| 52 | + }; |
| 53 | + |
| 54 | + // A Partition object is serializable and can be used from a different process. |
| 55 | + const [partitions] = await transaction.createQueryPartitions(query); |
| 56 | + console.log(`Successfully created ${partitions.length} query partitions.`); |
| 57 | + |
| 58 | + let rowCount = 0; |
| 59 | + const promises = partitions.map(partition => |
| 60 | + transaction.execute(partition).then(results => { |
| 61 | + const rows = results[0].map(row => row.toJSON()); |
| 62 | + rowCount += rows.length; |
| 63 | + }) |
| 64 | + ); |
| 65 | + await Promise.all(promises); |
| 66 | + console.log(`Successfully received ${rowCount} from executed partitions.`); |
| 67 | + } catch (err) { |
| 68 | + console.error('Error executing query partitions:', err); |
| 69 | + } finally { |
| 70 | + if (transaction) { |
| 71 | + transaction.close(); |
| 72 | + } |
| 73 | + await database.close(); |
| 74 | + } |
| 75 | + // [END spanner_batch_client] |
| 76 | +} |
| 77 | + |
| 78 | +async function executePartition( |
| 79 | + instanceId, |
| 80 | + databaseId, |
| 81 | + identifier, |
| 82 | + partition, |
| 83 | + projectId |
| 84 | +) { |
| 85 | + // [START spanner_batch_execute_partitions] |
| 86 | + // Imports the Google Cloud client library |
| 87 | + const {Spanner} = require('@google-cloud/spanner'); |
| 88 | + |
| 89 | + /** |
| 90 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 91 | + */ |
| 92 | + // const projectId = 'my-project-id'; |
| 93 | + // const instanceId = 'my-instance'; |
| 94 | + // const databaseId = 'my-database'; |
| 95 | + // const identifier = {}; |
| 96 | + // const partition = {}; |
| 97 | + |
| 98 | + // Creates a client |
| 99 | + const spanner = new Spanner({ |
| 100 | + projectId: projectId, |
| 101 | + }); |
| 102 | + |
| 103 | + // Gets a reference to a Cloud Spanner instance and database |
| 104 | + const instance = spanner.instance(instanceId); |
| 105 | + const database = instance.database(databaseId); |
| 106 | + const transaction = database.batchTransaction(identifier); |
| 107 | + |
| 108 | + const [rows] = await transaction.execute(partition); |
| 109 | + console.log(`Successfully received ${rows.length} from executed partition.`); |
| 110 | + // [END spanner_batch_execute_partitions] |
| 111 | +} |
| 112 | + |
| 113 | +require('yargs') |
| 114 | + .demandCommand(1) |
| 115 | + .command( |
| 116 | + 'create-and-execute-query-partitions <instanceName> <databaseName> <projectId>', |
| 117 | + 'Creates query partitions and executes them.', |
| 118 | + {}, |
| 119 | + opts => |
| 120 | + createAndExecuteQueryPartitions( |
| 121 | + opts.instanceName, |
| 122 | + opts.databaseName, |
| 123 | + opts.projectId |
| 124 | + ) |
| 125 | + ) |
| 126 | + .command( |
| 127 | + 'execute-partition <instanceName> <databaseName> <identifier> <partition> <projectId>', |
| 128 | + 'Executes a partition.', |
| 129 | + {}, |
| 130 | + opts => |
| 131 | + executePartition( |
| 132 | + opts.instanceName, |
| 133 | + opts.databaseName, |
| 134 | + JSON.parse(opts.identifier), |
| 135 | + JSON.parse(opts.partition), |
| 136 | + opts.projectId |
| 137 | + ) |
| 138 | + ) |
| 139 | + .example( |
| 140 | + 'node $0 create-and-execute-query-partitions "my-instance" "my-database" "my-project-id"' |
| 141 | + ) |
| 142 | + .example( |
| 143 | + 'node $0 execute-partition "my-instance" "my-database" "{}" "{}" "my-project-id"' |
| 144 | + ) |
| 145 | + .wrap(120) |
| 146 | + .recommendCommands() |
| 147 | + .epilogue('For more information, see https://cloud.google.com/spanner/docs') |
| 148 | + .strict() |
| 149 | + .help().argv; |
0 commit comments