Membuat kueri data dalam grafik
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Buat kueri data di Spanner Graph.
Mempelajari lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],[],[],[],null,["# Query data in a graph\n\nQuery data in a Spanner Graph.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Set up and query Spanner Graph](/spanner/docs/graph/set-up)\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 QueryData(google::cloud::spanner::Client client) {\n namespace spanner = ::google::cloud::spanner;\n\n spanner::SqlStatement select(R\"\"\"(\n Graph FinGraph\n MATCH (a:Person)-[o:Owns]-\u003e()-[t:Transfers]-\u003e()\u003c-[p:Owns]-(b:Person)\n RETURN a.name AS sender,\n b.name AS receiver,\n t.amount,\n t.create_time AS transfer_at\n )\"\"\");\n using RowType =\n std::tuple\u003cstd::string, std::string, double, spanner::Timestamp\u003e;\n auto rows = client.ExecuteQuery(std::move(select));\n for (auto& row : spanner::StreamOf\u003cRowType\u003e(rows)) {\n if (!row) throw std::move(row).status();\n std::cout \u003c\u003c \"sender: \" \u003c\u003c std::get\u003c0\u003e(*row) \u003c\u003c \"\\t\";\n std::cout \u003c\u003c \"receiver: \" \u003c\u003c std::get\u003c1\u003e(*row) \u003c\u003c \"\\t\";\n std::cout \u003c\u003c \"amount: \" \u003c\u003c std::get\u003c2\u003e(*row) \u003c\u003c \"\\t\";\n std::cout \u003c\u003c \"transfer_at: \" \u003c\u003c std::get\u003c3\u003e(*row) \u003c\u003c \"\\n\";\n }\n\n std::cout \u003c\u003c \"Query completed for [spanner_query_graph_data]\\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 \t\"time\"\n\n \t\"cloud.google.com/go/spanner\"\n\n \t\"google.golang.org/api/iterator\"\n )\n\n func queryGraphData(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 Spanner query statement comprising a graph query. Graph queries\n \t// are characterized by 'MATCH' statements describing node and edge\n \t// patterns.\n \t//\n \t// This statement finds entities ('Account's) owned by all 'Person's 'b' to\n \t// which transfers have been made by entities ('Account's) owned by any\n \t// 'Person' 'a' in the graph called 'FinGraph'. It then returns the names of\n \t// all such 'Person's 'a' and 'b', and the amount and time of the transfer.\n \tstmt := spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Statement{SQL: `Graph FinGraph \n \t\t MATCH (a:Person)-[o:Owns]-\u003e()-[t:Transfers]-\u003e()\u003c-[p:Owns]-(b:Person)\n \t\t RETURN a.name AS sender, b.name AS receiver, t.amount, t.create_time AS transfer_at`}\n \titer := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Client_Single().Query(ctx, stmt)\n \tdefer iter.Stop()\n\n \t// The results are returned in tabular form. Iterate over the\n \t// result rows and print them.\n \tfor {\n \t\trow, err := iter.Next()\n \t\tif err == iterator.Done {\n \t\t\treturn nil\n \t\t}\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tvar sender, receiver string\n \t\tvar amount float64\n \t\tvar transfer_at time.Time\n \t\tif err := row.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Row_Columns(&sender, &receiver, &amount, &transfer_at); err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Fprintf(w, \"%s %s %f %s\\n\", sender, receiver, amount, transfer_at.Format(time.RFC3339))\n \t}\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 query(DatabaseClient dbClient) {\n try (ResultSet resultSet =\n dbClient\n .singleUse() // Execute a single query against Cloud Spanner.\n .executeQuery(\n Statement.of(\n \"Graph FinGraph MATCH\"\n + \" (a:Person)-[o:Owns]-\u003e()-[t:Transfers]-\u003e()\u003c-[p:Owns]-(b:Person)RETURN\"\n + \" a.name AS sender, b.name AS receiver, t.amount, t.create_time AS\"\n + \" transfer_at\"))) {\n while (resultSet.next()) {\n System.out.printf(\n \"%s %s %f %s\\n\",\n resultSet.getString(0),\n resultSet.getString(1),\n resultSet.getDouble(2),\n resultSet.getTimestamp(3));\n }\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 query_data(instance_id, database_id):\n \"\"\"Queries sample data from the database using GQL.\"\"\"\n spanner_client = spanner.Client()\n instance = spanner_client.instance(instance_id)\n database = instance.database(database_id)\n\n with database.snapshot() as snapshot:\n results = snapshot.execute_sql(\n \"\"\"Graph FinGraph\n MATCH (a:Person)-[o:Owns]-\u003e()-[t:Transfers]-\u003e()\u003c-[p:Owns]-(b:Person)\n RETURN a.name AS sender, b.name AS receiver, t.amount, t.create_time AS transfer_at\"\"\"\n )\n\n for row in results:\n print(\"sender: {}, receiver: {}, amount: {}, transfer_at: {}\".format(*row))\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)."]]