將管理員實體匯入 Datastore
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 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"]],[],[[["\u003cp\u003eThis content provides code samples and instructions for importing admin entities into Datastore using C#, Go, Node.js, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eEach language section offers guidance on using the respective client library for Datastore mode, including how to install and use it.\u003c/p\u003e\n"],["\u003cp\u003eThe content emphasizes the importance of setting up Application Default Credentials for authentication in a local development environment, directing users to the appropriate documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples in each language demonstrate the process of importing entities, including specifying the project ID and the input URL where the data is located.\u003c/p\u003e\n"],["\u003cp\u003eUsers can use the Google Cloud sample browser to find code samples related to other Google Cloud products.\u003c/p\u003e\n"]]],[],null,["# Import admin entities to Datastore\n\nCode sample\n-----------\n\n### C#\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode C# API\nreference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest).\n\n\nTo authenticate to Datastore mode, 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 using https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.Admin.V1/latest/Google.Cloud.Datastore.Admin.V1.html;\n using https://cloud.google.com/dotnet/docs/reference/Google.LongRunning/latest/Google.LongRunning.html;\n using https://cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.html;\n using System;\n using System.Collections.Generic;\n\n public class ImportEntitiesSample\n {\n public bool ImportEntities(\n string projectId = \"your-project-id\",\n // For information on accepted input URL formats, see\n // https://cloud.google.com/datastore/docs/reference/admin/rpc/google.datastore.admin.v1#google.datastore.admin.v1.ImportEntitiesRequest\n string inputUrl = \"[URL to file containing data]\",\n string kind = \"Task\",\n string namespaceId = \"default\")\n {\n // Create client\n https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.Admin.V1/latest/Google.Cloud.Datastore.Admin.V1.DatastoreAdminClient.html datastoreAdminClient = https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.Admin.V1/latest/Google.Cloud.Datastore.Admin.V1.DatastoreAdminClient.html.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.Admin.V1/latest/Google.Cloud.Datastore.Admin.V1.DatastoreAdminClient.html#Google_Cloud_Datastore_Admin_V1_DatastoreAdminClient_Create();\n\n IDictionary\u003cstring, string\u003e labels = new Dictionary\u003cstring, string\u003e { { \"cloud_datastore_samples\", \"true\" }, };\n https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.Admin.V1/latest/Google.Cloud.Datastore.Admin.V1.EntityFilter.html entityFilter = new https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.Admin.V1/latest/Google.Cloud.Datastore.Admin.V1.EntityFilter.html()\n {\n Kinds = { kind },\n NamespaceIds = { namespaceId }\n };\n\n Operation\u003cEmpty, ImportEntitiesMetadata\u003e response = datastoreAdminClient.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.Admin.V1/latest/Google.Cloud.Datastore.Admin.V1.DatastoreAdminClient.html#Google_Cloud_Datastore_Admin_V1_DatastoreAdminClient_ImportEntities_Google_Cloud_Datastore_Admin_V1_ImportEntitiesRequest_Google_Api_Gax_Grpc_CallSettings_(projectId, labels, inputUrl, entityFilter);\n\n // Poll until the returned long-running operation is complete\n Operation\u003cEmpty, ImportEntitiesMetadata\u003e completedResponse = response.PollUntilCompleted();\n\n if (completedResponse.IsFaulted)\n {\n Console.WriteLine($\"Error while Importing Entities: {completedResponse.Exception}\");\n throw completedResponse.Exception;\n }\n\n Console.WriteLine($\"Entities imported successfully.\");\n\n return completedResponse.IsCompleted;\n }\n }\n\n### Go\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Go API\nreference documentation](https://cloud.google.com/go/docs/reference/cloud.google.com/go/datastore/latest).\n\n\nTo authenticate to Datastore mode, 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 import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tadmin \"cloud.google.com/go/datastore/admin/apiv1\"\n \t\"cloud.google.com/go/datastore/admin/apiv1/adminpb\"\n )\n\n // entitiesImport imports entities into Datastore.\n func entitiesImport(w io.Writer, projectID, inputURL string) error {\n \t// projectID := \"project-id\"\n \t// inputURL := \"gs://bucket-name/overall-export-metadata-file\"\n \tctx := context.Background()\n \tclient, err := admin.NewDatastoreAdminClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"admin.NewDatastoreAdminClient: %w\", err)\n \t}\n \tdefer client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/datastore/latest/admin/apiv1.html#cloud_google_com_go_datastore_admin_apiv1_DatastoreAdminClient_Close()\n\n \treq := &adminpb.ImportEntitiesRequest{\n \t\tProjectId: projectID,\n \t\tInputUrl: inputURL,\n \t}\n \top, err := client.ImportEntities(ctx, req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"ImportEntities: %w\", err)\n \t}\n \tif err = op.Wait(ctx); err != nil {\n \t\treturn fmt.Errorf(\"Wait: %w\", err)\n \t}\n \tfmt.Fprintf(w, \"Entities were imported\\n\")\n \treturn nil\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Node.js API\nreference documentation](https://cloud.google.com/nodejs/docs/reference/datastore/latest).\n\n\nTo authenticate to Datastore mode, 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 const {Datastore} = require('https://cloud.google.com/nodejs/docs/reference/datastore/latest/overview.html');\n const datastore = new https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/datastore.html();\n\n async function importEntities() {\n /**\n * TODO(developer): Uncomment these variables before running the sample.\n */\n // const file = 'YOUR_FILE_NAME';\n\n const [importOperation] = await datastore.https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/datastore.html({file});\n\n // Uncomment to await the results of the operation.\n // await importOperation.promise();\n\n // Or cancel the operation.\n await importOperation.cancel();\n\n // You may also choose to include only specific kinds and namespaces.\n const [specificImportOperation] = await datastore.https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/datastore.html({\n file,\n kinds: ['Employee', 'Task'],\n namespaces: ['Company'],\n });\n\n // Uncomment to await the results of the operation.\n // await specificImportOperation.promise();\n\n // Or cancel the operation.\n await specificImportOperation.cancel();\n }\n\n importEntities();\n\n### Python\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Python API\nreference documentation](https://cloud.google.com/python/docs/reference/datastore/latest).\n\n\nTo authenticate to Datastore mode, 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 import_entities(project_id, input_url):\n \"\"\"Imports entities into Datastore.\"\"\"\n # project_id := \"project-id\"\n # input_url := \"gs://bucket-name/overall-export-metadata-file\"\n client = DatastoreAdminClient()\n\n op = client.import_entities({\"project_id\": project_id, \"input_url\": input_url})\n response = op.result(timeout=300)\n\n print(\"Entities were imported\\n\")\n return response\n\n### Ruby\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Ruby API\nreference documentation](/ruby/docs/reference/google-cloud-datastore/latest).\n\n\nTo authenticate to Datastore mode, 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 = \"project-id\"\n # input_url = \"gs://bucket-name/overall-export-metadata-file\"\n op = client.import_entities project_id: project_id, input_url: input_url\n\n op.wait_until_done!\n raise op.error.message if op.error?\n\n response = op.response\n # Process the response.\n\n metadata = op.metadata\n # Process the metadata.\n\n puts \"Entities were imported\"\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=datastore)."]]