Daten lesen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Daten lesen.
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],[],[],[],null,["Read data.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Getting started with Spanner in C++](/spanner/docs/getting-started/cpp)\n- [Getting started with Spanner in Go](/spanner/docs/getting-started/go)\n- [Getting started with Spanner in Java](/spanner/docs/getting-started/java)\n- [Getting started with Spanner in Node.js](/spanner/docs/getting-started/nodejs)\n- [Getting started with Spanner in PHP](/spanner/docs/getting-started/php)\n- [Getting started with Spanner in Python](/spanner/docs/getting-started/python)\n- [Getting started with Spanner in Ruby](/spanner/docs/getting-started/ruby)\n- [Reads outside of transactions](/spanner/docs/reads)\n\nCode sample \n\nC++\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 ReadData(google::cloud::spanner::Client client) {\n namespace spanner = ::google::cloud::spanner;\n\n auto rows = client.Read(\"Albums\", google::cloud::spanner::KeySet::All(),\n {\"SingerId\", \"AlbumId\", \"AlbumTitle\"});\n using RowType = std::tuple\u003cstd::int64_t, std::int64_t, std::string\u003e;\n for (auto& row : spanner::StreamOf\u003cRowType\u003e(rows)) {\n if (!row) throw std::move(row).status();\n std::cout \u003c\u003c \"SingerId: \" \u003c\u003c std::get\u003c0\u003e(*row) \u003c\u003c \"\\t\";\n std::cout \u003c\u003c \"AlbumId: \" \u003c\u003c std::get\u003c1\u003e(*row) \u003c\u003c \"\\t\";\n std::cout \u003c\u003c \"AlbumTitle: \" \u003c\u003c std::get\u003c2\u003e(*row) \u003c\u003c \"\\n\";\n }\n\n std::cout \u003c\u003c \"Read completed for [spanner_read_data]\\n\";\n }\n\nGo\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 \t\"google.golang.org/api/iterator\"\n )\n\n func read(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 \titer := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Client_Single().Read(ctx, \"Albums\", spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_KeySet_AllKeys(),\n \t\t[]string{\"SingerId\", \"AlbumId\", \"AlbumTitle\"})\n \tdefer iter.Stop()\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 singerID, albumID int64\n \t\tvar albumTitle string\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(&singerID, &albumID, &albumTitle); err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Fprintf(w, \"%d %d %s\\n\", singerID, albumID, albumTitle)\n \t}\n }\n\nJava\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 read(DatabaseClient dbClient) {\n try (ResultSet resultSet =\n dbClient\n .singleUse()\n .read(\n \"Albums\",\n KeySet.all(), // Read all rows in a table.\n Arrays.asList(\"SingerId\", \"AlbumId\", \"AlbumTitle\"))) {\n while (resultSet.next()) {\n System.out.printf(\n \"%d %d %s\\n\", resultSet.getLong(0), resultSet.getLong(1), resultSet.getString(2));\n }\n }\n }\n\nNode.js\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 // Imports the Google Cloud client library\n const {Spanner} = require('https://cloud.google.com/nodejs/docs/reference/spanner/latest/overview.html');\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // const projectId = 'my-project-id';\n // const instanceId = 'my-instance';\n // const databaseId = 'my-database';\n\n // Creates a client\n const spanner = new https://cloud.google.com/nodejs/docs/reference/spanner/latest/spanner/spanner.html({\n projectId: projectId,\n });\n\n // Gets a reference to a Cloud Spanner instance and database\n const instance = spanner.instance(instanceId);\n const database = instance.database(databaseId);\n\n // Reads rows from the Albums table\n const albumsTable = database.table('Albums');\n\n const query = {\n columns: ['SingerId', 'AlbumId', 'AlbumTitle'],\n keySet: {\n all: true,\n },\n };\n\n try {\n const [rows] = await albumsTable.read(query);\n\n rows.forEach(row =\u003e {\n const json = row.toJSON();\n console.log(\n `SingerId: ${json.SingerId}, AlbumId: ${json.AlbumId}, AlbumTitle: ${json.AlbumTitle}`,\n );\n });\n } catch (err) {\n console.error('ERROR:', err);\n } finally {\n // Close the database when finished.\n await database.close();\n }\n\nPHP\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 use Google\\Cloud\\Spanner\\SpannerClient;\n\n /**\n * Reads sample data from the database.\n * Example:\n * ```\n * read_data($instanceId, $databaseId);\n * ```\n *\n * @param string $instanceId The Spanner instance ID.\n * @param string $databaseId The Spanner database ID.\n */\n function read_data(string $instanceId, string $databaseId): void\n {\n $spanner = new SpannerClient();\n $instance = $spanner-\u003einstance($instanceId);\n $database = $instance-\u003edatabase($databaseId);\n\n $keySet = $spanner-\u003ekeySet(['all' =\u003e true]);\n $results = $database-\u003eread(\n 'Albums',\n $keySet,\n ['SingerId', 'AlbumId', 'AlbumTitle']\n );\n\n foreach ($results-\u003erows() as $row) {\n printf('SingerId: %s, AlbumId: %s, AlbumTitle: %s' . PHP_EOL,\n $row['SingerId'], $row['AlbumId'], $row['AlbumTitle']);\n }\n }\n\nPython\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 read_data(instance_id, database_id):\n \"\"\"Reads sample data from the database.\"\"\"\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 keyset = spanner.KeySet(all_=True)\n results = snapshot.read(\n table=\"Albums\", columns=(\"SingerId\", \"AlbumId\", \"AlbumTitle\"), keyset=keyset\n )\n\n for row in results:\n print(\"SingerId: {}, AlbumId: {}, AlbumTitle: {}\".format(*row))\n\nRuby\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 # project_id = \"Your Google Cloud project ID\"\n # instance_id = \"Your Spanner instance ID\"\n # database_id = \"Your Spanner database ID\"\n\n require \"google/cloud/spanner\"\n\n spanner = Google::Cloud::https://cloud.google.com/ruby/docs/reference/activerecord-spanner-adapter/latest/Google-Cloud-Spanner.html.new project: project_id\n client = spanner.https://cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Project.html instance_id, database_id\n\n client.read(\"Albums\", [:SingerId, :AlbumId, :AlbumTitle]).rows.https://cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchWriteResults.html do |row|\n puts \"#{row[:SingerId]} #{row[:AlbumId]} #{row[:AlbumTitle]}\"\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=spanner)."]]