MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

spanner/rpc-priority-query-partitions.js:21–93  ·  view source on GitHub ↗
(instanceId, databaseId, projectId)

Source from the content-addressed store, hash-verified

19'use strict';
20
21async function main(instanceId, databaseId, projectId) {
22 // [START spanner_rpc_priority_query_partitions]
23 // Imports the Google Cloud client library.
24 const {Spanner, protos} = require('@google-cloud/spanner');
25 const Priority = protos.google.spanner.v1.RequestOptions.Priority;
26
27 /**
28 * TODO(developer): Uncomment the following lines before running the sample.
29 */
30 // const projectId = 'my-project-id';
31 // const instanceId = 'my-instance';
32 // const databaseId = 'my-database';
33
34 // Creates a client
35 const spanner = new Spanner({
36 projectId: projectId,
37 });
38
39 async function createAndExecuteQueryPartitionsWithRpcPriority(
40 instanceId,
41 databaseId
42 ) {
43 let database;
44 let transaction;
45 try {
46 // Gets a reference to a Cloud Spanner instance and database
47 const instance = spanner.instance(instanceId);
48 database = instance.database(databaseId);
49 [transaction] = await database.createBatchTransaction();
50
51 const [partitions] = await transaction.createQueryPartitions({
52 sql: 'SELECT * FROM Singers',
53 requestOptions: {
54 priority: Priority.PRIORITY_LOW,
55 },
56 });
57 console.log(
58 `Successfully created ${partitions.length} query partitions using low RPC priority.`
59 );
60
61 let rowCount = 0;
62 const promises = partitions.map(async partition => {
63 // Partition object inherits priority from createQueryPartitions call.
64 // Priority for a partition can also be assigned or changed while executing partition.
65 partition.requestOptions = {
66 priority: Priority.PRIORITY_MEDIUM,
67 };
68
69 const [results] = await transaction.execute(partition);
70 const rows = results.map(row => row.toJSON());
71 rowCount += rows.length;
72 });
73
74 await Promise.all(promises);
75
76 console.log(
77 `Successfully received ${rowCount} from executed partitions.`
78 );

Tested by

no test coverage detected