From c93dfe71dfe7666c7f14edf50fd5231c448a5857 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Mon, 25 Mar 2019 11:20:45 -0700 Subject: [PATCH 1/4] Add Cloud Spanner Batch DML sample --- spanner/cloud-client/snippets.py | 39 +++++++++++++++++++++++++++ spanner/cloud-client/snippets_test.py | 10 +++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index 1b065bb4cf5..9578b7507b0 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -1039,6 +1039,40 @@ def delete_data_with_partitioned_dml(instance_id, database_id): # [END spanner_dml_partitioned_delete] +def update_with_batch_dml(instance_id, database_id): + """Updates sample data in the database using Batch DML. """ + # [START spanner_dml_batch_update] + # instance_id = "your-spanner-instance" + # database_id = "your-spanner-db-id" + + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + database = instance.database(database_id) + + insert_statement = ( + "INSERT INTO Albums " + "(SingerId, AlbumId, AlbumTitle, MarketingBudget) " + "VALUES (1, 3, 'Test Album Title', 10000)" + ) + + update_statement = ( + "UPDATE Albums " + "SET MarketingBudget = MarketingBudget * 2 " + "WHERE SingerId = 1 and AlbumId = 3" + ) + + def update_albums(transaction): + row_cts = transaction.batch_update([ + insert_statement, + update_statement, + ]) + + print("Executed {} SQL statements using Batch DML.".format(len(row_cts))) + + database.run_in_transaction(update_albums) + # [END spanner_dml_batch_update] + + if __name__ == '__main__': # noqa: C901 parser = argparse.ArgumentParser( description=__doc__, @@ -1118,6 +1152,9 @@ def delete_data_with_partitioned_dml(instance_id, database_id): subparsers.add_parser( 'delete_data_with_partitioned_dml', help=delete_data_with_partitioned_dml.__doc__) + subparsers.add_parser( + 'update_with_batch_dml', + help=update_with_batch_dml.__doc__) args = parser.parse_args() @@ -1195,3 +1232,5 @@ def delete_data_with_partitioned_dml(instance_id, database_id): update_data_with_partitioned_dml(args.instance_id, args.database_id) elif args.command == 'delete_data_with_partitioned_dml': delete_data_with_partitioned_dml(args.instance_id, args.database_id) + elif args.command == 'update_with_batch_dml': + update_with_batch_dml(args.instance_id, args.database_id) diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 2ea6aec6429..d3283c5b3d8 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -190,13 +190,13 @@ def test_insert_data_with_timestamp(capsys): def test_write_struct_data(capsys): snippets.write_struct_data(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr() - assert 'Inserted sample data for STRUCT queries' + assert 'Inserted sample data for STRUCT queries' in out def test_query_with_struct(capsys): snippets.query_with_struct(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr() - assert 'SingerId: Elena' + assert 'SingerId: Elena' in out def test_query_with_array_of_struct(capsys): @@ -277,3 +277,9 @@ def delete_data_with_partitioned_dml(capsys): snippets.delete_data_with_partitioned_dml(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr() assert "5 record(s) deleted" in out + + +def update_with_batch_dml(capsys): + snippets.update_with_batch_dml(INSTANCE_ID, DATABASE_ID) + out, _ = capsys.readouterr() + assert "Executed 2 SQL statements using Batch DML" in out \ No newline at end of file From f59f7010e1b63cf2c558f3992f93a46e2dc9104f Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Mon, 25 Mar 2019 11:54:02 -0700 Subject: [PATCH 2/4] Fix test. --- spanner/cloud-client/snippets_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index d3283c5b3d8..6dfc037fcb8 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -196,7 +196,7 @@ def test_write_struct_data(capsys): def test_query_with_struct(capsys): snippets.query_with_struct(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr() - assert 'SingerId: Elena' in out + assert 'SingerId: 6' in out def test_query_with_array_of_struct(capsys): From e20fe421b6dca73345d6f858386787e2b5aa5501 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Mon, 25 Mar 2019 12:15:13 -0700 Subject: [PATCH 3/4] Lint. --- spanner/cloud-client/snippets.py | 13 +++++++------ spanner/cloud-client/snippets_test.py | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index 9578b7507b0..9bbe0fbd8c2 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -701,7 +701,7 @@ def query_with_array_of_struct(instance_id, database_id): param_types={'names': param_types.Array(name_type)}) for row in results: - print(u'SingerId: {}'.format(*row)) + print(u'SingerId: {}'.format(*row)) # [END spanner_query_data_with_array_of_struct] @@ -725,7 +725,7 @@ def query_struct_field(instance_id, database_id): param_types={'name': name_type}) for row in results: - print(u'SingerId: {}'.format(*row)) + print(u'SingerId: {}'.format(*row)) # [START spanner_field_access_on_struct_parameters] @@ -768,7 +768,7 @@ def query_nested_struct_field(instance_id, database_id): ) for row in results: - print(u'SingerId: {} SongName: {}'.format(*row)) + print(u'SingerId: {} SongName: {}'.format(*row)) # [END spanner_field_access_on_nested_struct_parameters] @@ -998,7 +998,7 @@ def transfer_budget(transaction): ) print("Transferred {} from Album1's budget to Album2's".format( - transfer_amount)) + transfer_amount)) database.run_in_transaction(transfer_budget) # [END spanner_dml_getting_started_update] @@ -1067,7 +1067,8 @@ def update_albums(transaction): update_statement, ]) - print("Executed {} SQL statements using Batch DML.".format(len(row_cts))) + print("Executed {} SQL statements using Batch DML.".format( + len(row_cts))) database.run_in_transaction(update_albums) # [END spanner_dml_batch_update] @@ -1154,7 +1155,7 @@ def update_albums(transaction): help=delete_data_with_partitioned_dml.__doc__) subparsers.add_parser( 'update_with_batch_dml', - help=update_with_batch_dml.__doc__) + help=update_with_batch_dml.__doc__) args = parser.parse_args() diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 6dfc037fcb8..9e460f68ea2 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -282,4 +282,5 @@ def delete_data_with_partitioned_dml(capsys): def update_with_batch_dml(capsys): snippets.update_with_batch_dml(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr() - assert "Executed 2 SQL statements using Batch DML" in out \ No newline at end of file + assert "Executed 2 SQL statements using Batch DML" in out + \ No newline at end of file From 735cc0c9a0f329a063548935c2e663c3042398fd Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Mon, 25 Mar 2019 12:34:53 -0700 Subject: [PATCH 4/4] More Lint. --- spanner/cloud-client/snippets_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 9e460f68ea2..9bd39d27a57 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -283,4 +283,3 @@ def update_with_batch_dml(capsys): snippets.update_with_batch_dml(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr() assert "Executed 2 SQL statements using Batch DML" in out - \ No newline at end of file