그래프 쿼리로 그래프 데이터 업데이트
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
그래프 쿼리를 사용하여 Spanner Graph의 데이터를 업데이트합니다.
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],[],[],[],null,["# Update graph data with a graph query\n\nUpdate data in a Spanner Graph using a graph query.\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 UpdateDataWithGraphQueryInDml(google::cloud::spanner::Client client) {\n using ::google::cloud::StatusOr;\n namespace spanner = ::google::cloud::spanner;\n auto commit_result = client.Commit(\n [&client](spanner::Transaction txn) -\u003e StatusOr\u003cspanner::Mutations\u003e {\n auto update =\n client.ExecuteDml(std::move(txn), spanner::SqlStatement(R\"\"\"(\n UPDATE Account SET is_blocked = true\n WHERE id IN {\n GRAPH FinGraph\n MATCH (a:Account WHERE a.id = 1)-[:TRANSFERS]-\u003e{1,2}(b:Account)\n RETURN b.id})\"\"\"));\n if (!update) return std::move(update).status();\n return spanner::Mutations{};\n });\n if (!commit_result) throw std::move(commit_result).status();\n std::cout \u003c\u003c \"Update was successful \"\n \u003c\u003c \"[spanner_update_graph_data_with_graph_query_in_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 updateGraphDataWithGraphQueryInDml(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 update the 'Account' table underpinning\n \t// 'Account' nodes in 'FinGraph'. The function run by ReadWriteTransaction\n \t// executes an 'UPDATE' SQL DML statement. Graph queries run after this\n \t// transaction is committed will observe the effects of the updates to 'Account's\n \t//\n \t// The update is performed for all 'Account's whose 'id' is returned by\n \t// the graph query in the 'IN' subquery, i.e., all 'Account's that have\n \t// received transfers directly or via one intermediary from an 'Account'\n \t// whose 'id' is 1.\n \t_, err = 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: `UPDATE Account SET is_blocked = true \n \t WHERE id IN {\n \t GRAPH FinGraph \n \t MATCH (a:Account WHERE a.id = 1)-[:TRANSFERS]-\u003e{1,2}(b:Account)\n \t RETURN b.id}`,\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) updated.\\n\", rowCount)\n \t\treturn err\n \t})\n\n \treturn err\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 updateUsingGraphQueryInDml(DatabaseClient dbClient) {\n dbClient\n .readWriteTransaction()\n .run(\n transaction -\u003e {\n String sql =\n \"UPDATE Account SET is_blocked = true \"\n + \"WHERE id IN {\"\n + \" GRAPH FinGraph\"\n + \" MATCH (a:Account WHERE a.id = 1)-[:TRANSFERS]-\u003e{1,2}(b:Account)\"\n + \" RETURN b.id}\";\n long rowCount = transaction.executeUpdate(Statement.of(sql));\n System.out.printf(\"%d Account record(s) updated.\\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 update_data_with_graph_query_in_dml(instance_id, database_id):\n \"\"\"Updates sample data from the 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 update_accounts(transaction):\n row_ct = transaction.execute_update(\n \"UPDATE Account SET is_blocked = true \"\n \"WHERE id IN {\"\n \" GRAPH FinGraph\"\n \" MATCH (a:Account WHERE a.id = 1)-[:TRANSFERS]-\u003e{1,2}(b:Account)\"\n \" RETURN b.id}\"\n )\n\n print(\"{} Account record(s) updated.\".format(row_ct))\n\n database.run_in_transaction(update_accounts)\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)."]]