Insert graph data with DML
Stay organized with collections
Save and categorize content based on your preferences.
Insert data into a Spanner Graph using DML.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Insert graph data with DML\n\nInsert data into a Spanner Graph using DML.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Insert, update, or delete Spanner Graph data](/spanner/docs/graph/insert-update-delete-data)\n\nCode sample\n-----------\n\n### C++\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n void InsertDataWithDml(google::cloud::spanner::Client client) {\n using ::google::cloud::StatusOr;\n namespace spanner = ::google::cloud::spanner;\n\n std::int64_t rows_inserted;\n auto commit_result = client.Commit(\n [&client, &rows_inserted](\n spanner::Transaction txn) -\u003e StatusOr\u003cspanner::Mutations\u003e {\n auto insert =\n client.ExecuteDml(std::move(txn), spanner::SqlStatement(R\"\"\"(\n INSERT INTO Account (id, create_time, is_blocked)\n VALUES\n (1, CAST('2000-08-10 08:18:48.463959-07:52' AS TIMESTAMP), false),\n (2, CAST('2000-08-12 07:13:16.463959-03:41' AS TIMESTAMP), true)\n )\"\"\"));\n if (!insert) return std::move(insert).status();\n rows_inserted = insert-\u003eRowsModified();\n return spanner::Mutations{};\n });\n if (!commit_result) throw std::move(commit_result).status();\n std::cout \u003c\u003c \"Rows inserted into Account: \" \u003c\u003c rows_inserted \u003c\u003c \"\\n\";\n\n commit_result = client.Commit(\n [&client, &rows_inserted](\n spanner::Transaction txn) -\u003e StatusOr\u003cspanner::Mutations\u003e {\n auto insert =\n client.ExecuteDml(std::move(txn), spanner::SqlStatement(R\"\"\"(\n INSERT INTO AccountTransferAccount (id, to_id, create_time, amount)\n VALUES\n (1, 2, CAST('2000-09-11 03:11:18.463959-06:36' AS TIMESTAMP), 100),\n (1, 1, CAST('2000-09-12 04:09:34.463959-05:12' AS TIMESTAMP), 200)\n )\"\"\"));\n if (!insert) return std::move(insert).status();\n rows_inserted = insert-\u003eRowsModified();\n return spanner::Mutations{};\n });\n if (!commit_result) throw std::move(commit_result).status();\n std::cout \u003c\u003c \"Rows inserted into AccountTransferAccount: \" \u003c\u003c rows_inserted\n \u003c\u003c \"\\n\";\n\n std::cout \u003c\u003c \"Insert was successful [spanner_insert_graph_data_with_dml]\\n\";\n }\n\n### Go\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/spanner\"\n )\n\n func insertGraphDataWithDml(w io.Writer, db string) error {\n \tctx := context.Background()\n \tclient, err := spanner.NewClient(ctx, db)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer client.Close()\n\n \t// Execute a ReadWriteTransaction to insert values into the 'Account' table\n \t// underpinning 'Account' nodes in 'FinGraph'. The function run by ReadWriteTransaction\n \t// executes an 'INSERT' SQL DML statement. Graph queries run after this\n \t// transaction is committed will observe the effects of the new 'Account's\n \t// added to the graph.\n \t_, err1 := client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {\n \t\tstmt := spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Statement{\n \t\t\tSQL: `INSERT INTO Account (id, create_time, is_blocked)\n \t\tVALUES\n \t \t(1, CAST('2000-08-10 08:18:48.463959-07:52' AS TIMESTAMP), false),\n \t\t\t(2, CAST('2000-08-12 07:13:16.463959-03:41' AS TIMESTAMP), true)`,\n \t\t}\n \t\trowCount, err := txn.Update(ctx, stmt)\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Fprintf(w, \"%d Account record(s) inserted.\\n\", rowCount)\n \t\treturn err\n \t})\n\n \tif err1 != nil {\n \t\treturn err1\n \t}\n\n \t// Execute a ReadWriteTransaction to insert values into the 'AccountTransferAccount'\n \t// table underpinning 'AccountTransferAccount' edges in 'FinGraph'. The function run\n \t// by ReadWriteTransaction executes an 'INSERT' SQL DML statement.\n \t// Graph queries run after this transaction is committed will observe the effects\n \t// of the edges added to the graph.\n \t_, err2 := client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {\n \t\tstmt := spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Statement{\n \t\t\tSQL: `INSERT INTO AccountTransferAccount (id, to_id, create_time, amount)\n \t\t\t\t\tVALUES\n \t\t\t\t\t\t(1, 2, CAST('2000-09-11 03:11:18.463959-06:36' AS TIMESTAMP), 100),\n \t\t\t\t\t\t(1, 1, CAST('2000-09-12 04:09:34.463959-05:12' AS TIMESTAMP), 200)`,\n \t\t}\n \t\trowCount, err := txn.Update(ctx, stmt)\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Fprintf(w, \"%d AccountTransferAccount record(s) inserted.\\n\", rowCount)\n \t\treturn err\n \t})\n\n \treturn err2\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n static void insertUsingDml(DatabaseClient dbClient) {\n dbClient\n .readWriteTransaction()\n .run(\n transaction -\u003e {\n String sql =\n \"INSERT INTO Account (id, create_time, is_blocked) \"\n + \" VALUES\"\n + \" (1, CAST('2000-08-10 08:18:48.463959-07:52' AS TIMESTAMP), false),\"\n + \" (2, CAST('2000-08-12 07:13:16.463959-03:41' AS TIMESTAMP), true)\";\n long rowCount = transaction.executeUpdate(Statement.of(sql));\n System.out.printf(\"%d record(s) inserted into Account.\\n\", rowCount);\n return null;\n });\n\n dbClient\n .readWriteTransaction()\n .run(\n transaction -\u003e {\n String sql =\n \"INSERT INTO AccountTransferAccount (id, to_id, create_time, amount) \"\n + \" VALUES\"\n + \" (1, 2, CAST('2000-09-11 03:11:18.463959-06:36' AS TIMESTAMP), 100),\"\n + \" (1, 1, CAST('2000-09-12 04:09:34.463959-05:12' AS TIMESTAMP), 200) \";\n long rowCount = transaction.executeUpdate(Statement.of(sql));\n System.out.printf(\"%d record(s) inserted into AccountTransferAccount.\\n\", rowCount);\n return null;\n });\n }\n\n### Python\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n def insert_data_with_dml(instance_id, database_id):\n \"\"\"Inserts sample data into the given database using a DML statement.\"\"\"\n\n spanner_client = spanner.Client()\n instance = spanner_client.instance(instance_id)\n database = instance.database(database_id)\n\n def insert_accounts(transaction):\n row_ct = transaction.execute_update(\n \"INSERT INTO Account (id, create_time, is_blocked) \"\n \" VALUES\"\n \" (1, CAST('2000-08-10 08:18:48.463959-07:52' AS TIMESTAMP), false),\"\n \" (2, CAST('2000-08-12 07:13:16.463959-03:41' AS TIMESTAMP), true)\"\n )\n\n print(\"{} record(s) inserted into Account.\".format(row_ct))\n\n def insert_transfers(transaction):\n row_ct = transaction.execute_update(\n \"INSERT INTO AccountTransferAccount (id, to_id, create_time, amount) \"\n \" VALUES\"\n \" (1, 2, CAST('2000-09-11 03:11:18.463959-06:36' AS TIMESTAMP), 100),\"\n \" (1, 1, CAST('2000-09-12 04:09:34.463959-05:12' AS TIMESTAMP), 200) \"\n )\n\n print(\"{} record(s) inserted into AccountTransferAccount.\".format(row_ct))\n\n database.run_in_transaction(insert_accounts)\n database.run_in_transaction(insert_transfers)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=spanner)."]]