将管理员实体导入 Datastore

将管理员实体导入 Datastore

代码示例

C#

如需了解如何安装和使用适用于 Datastore 模式的客户端库,请参阅 Datastore 模式客户端库。 如需了解详情,请参阅 Datastore 模式 C# API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


using Google.Cloud.Datastore.Admin.V1;
using Google.LongRunning;
using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;

public class ImportEntitiesSample
{
    public bool ImportEntities(
        string projectId = "your-project-id",
        string inputUrl = "gs://datastore-admin-bucket/data_to_import",
        string kind = "Task",
        string namespaceId = "default")
    {
        // Create client
        DatastoreAdminClient datastoreAdminClient = DatastoreAdminClient.Create();

        IDictionary<string, string> labels = new Dictionary<string, string> { { "cloud_datastore_samples", "true" }, };
        EntityFilter entityFilter = new EntityFilter()
        {
            Kinds = { kind },
            NamespaceIds = { namespaceId }
        };

        Operation<Empty, ImportEntitiesMetadata> response = datastoreAdminClient.ImportEntities(projectId, labels, inputUrl, entityFilter);

        // Poll until the returned long-running operation is complete
        Operation<Empty, ImportEntitiesMetadata> completedResponse = response.PollUntilCompleted();

        if (completedResponse.IsFaulted)
        {
            Console.WriteLine($"Error while Importing Entities: {completedResponse.Exception}");
            throw completedResponse.Exception;
        }

        Console.WriteLine($"Entities imported successfully.");

        return completedResponse.IsCompleted;
    }
}

Go

如需了解如何安装和使用适用于 Datastore 模式的客户端库,请参阅 Datastore 模式客户端库。 有关详情,请参阅 Datastore 模式 Go API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	admin "cloud.google.com/go/datastore/admin/apiv1"
	"cloud.google.com/go/datastore/admin/apiv1/adminpb"
)

// entitiesImport imports entities into Datastore.
func entitiesImport(w io.Writer, projectID, inputURL string) error {
	// projectID := "project-id"
	// inputURL := "gs://bucket-name/overall-export-metadata-file"
	ctx := context.Background()
	client, err := admin.NewDatastoreAdminClient(ctx)
	if err != nil {
		return fmt.Errorf("admin.NewDatastoreAdminClient: %w", err)
	}
	defer client.Close()

	req := &adminpb.ImportEntitiesRequest{
		ProjectId: projectID,
		InputUrl:  inputURL,
	}
	op, err := client.ImportEntities(ctx, req)
	if err != nil {
		return fmt.Errorf("ImportEntities: %w", err)
	}
	if err = op.Wait(ctx); err != nil {
		return fmt.Errorf("Wait: %w", err)
	}
	fmt.Fprintf(w, "Entities were imported\n")
	return nil
}

Node.js

如需了解如何安装和使用 Datastore 模式的客户端库,请参阅 Datastore 模式客户端库。 如需了解详情,请参阅 Datastore 模式 Node.js API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();

async function importEntities() {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const file = 'YOUR_FILE_NAME';

  const [importOperation] = await datastore.import({file});

  // Uncomment to await the results of the operation.
  // await importOperation.promise();

  // Or cancel the operation.
  await importOperation.cancel();

  // You may also choose to include only specific kinds and namespaces.
  const [specificImportOperation] = await datastore.import({
    file,
    kinds: ['Employee', 'Task'],
    namespaces: ['Company'],
  });

  // Uncomment to await the results of the operation.
  // await specificImportOperation.promise();

  // Or cancel the operation.
  await specificImportOperation.cancel();
}

importEntities();

Python

如需了解如何安装和使用适用于 Datastore 模式的客户端库,请参阅 Datastore 模式客户端库。 有关详情,请参阅 Datastore 模式 Python API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

def import_entities(project_id, input_url):
    """Imports entities into Datastore."""
    # project_id := "project-id"
    # input_url := "gs://bucket-name/overall-export-metadata-file"
    client = DatastoreAdminClient()

    op = client.import_entities({"project_id": project_id, "input_url": input_url})
    response = op.result(timeout=300)

    print("Entities were imported\n")
    return response

Ruby

如需了解如何安装和使用适用于 Datastore 模式的客户端库,请参阅 Datastore 模式客户端库。 有关详情,请参阅 Datastore 模式 Ruby API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

# project_id = "project-id"
# input_url = "gs://bucket-name/overall-export-metadata-file"
op = client.import_entities project_id: project_id, input_url: input_url

op.wait_until_done!
raise op.error.message if op.error?

response = op.response
# Process the response.

metadata = op.metadata
# Process the metadata.

puts "Entities were imported"

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器