ייצוא עותק של כל הישויות או של קבוצת משנה של ישויות

ייצוא עותק של כל הישויות או של קבוצת משנה של ישויות מ-Datastore למערכת אחסון אחרת, כמו Cloud Storage.

דוגמת קוד

C#

מידע על התקנת ספריית הלקוח למצב Datastore ושימוש בה מופיע במאמר ספריות הלקוח של מצב Datastore. מידע נוסף מופיע במאמרי העזרה של Datastore mode C# API.

כדי לבצע אימות במצב Datastore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.


using Google.Cloud.Datastore.Admin.V1;
using System;
using System.Collections.Generic;

public class ExportEntitiesSample
{
    public string ExportEntities(
        string projectId = "your-project-id",
        string outputUrlPrefix = "gs://your-bucket-name",
        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 }
        };

        var response = datastoreAdminClient.ExportEntities(projectId, labels, entityFilter, outputUrlPrefix);

        // Poll until the returned long-running operation is complete
        var completedResponse = response.PollUntilCompleted();

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

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

        ExportEntitiesResponse result = completedResponse.Result;

        return result.OutputUrl;
    }
}

Go

מידע על התקנת ספריית הלקוח למצב Datastore ושימוש בה מופיע במאמר ספריות הלקוח של מצב Datastore. מידע נוסף מופיע במאמרי העזרה של Datastore mode Go API.

כדי לבצע אימות במצב Datastore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import (
	"context"
	"fmt"
	"io"

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

// entitiesExport exports a copy of all or a subset of entities from
// Datastore to another storage system, such as Cloud Storage.
func entitiesExport(w io.Writer, projectID, outputURLPrefix string) (*adminpb.ExportEntitiesResponse, error) {
	// projectID := "project-id"
	// outputURLPrefix := "gs://bucket-name"
	ctx := context.Background()
	client, err := admin.NewDatastoreAdminClient(ctx)
	if err != nil {
		return nil, fmt.Errorf("admin.NewDatastoreAdminClient: %w", err)
	}
	defer client.Close()

	req := &adminpb.ExportEntitiesRequest{
		ProjectId:       projectID,
		OutputUrlPrefix: outputURLPrefix,
	}
	op, err := client.ExportEntities(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("ExportEntities: %w", err)
	}
	resp, err := op.Wait(ctx)
	if err != nil {
		return nil, fmt.Errorf("Wait: %w", err)
	}
	fmt.Fprintln(w, "Entities were exported")
	return resp, nil
}

Python

מידע על התקנת ספריית הלקוח למצב Datastore ושימוש בה מופיע במאמר ספריות הלקוח של מצב Datastore. מידע נוסף מופיע במאמרי העזרה של Datastore mode Python API.

כדי לבצע אימות במצב Datastore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

def export_entities(project_id, output_url_prefix):
    """
    Exports a copy of all or a subset of entities from
    Datastore to another storage system, such as Cloud Storage.
    """
    # project_id = "project-id"
    # output_url_prefix = "gs://bucket-name"
    client = DatastoreAdminClient()

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

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

Ruby

מידע על התקנת ספריית הלקוח למצב Datastore ושימוש בה מופיע במאמר ספריות הלקוח של מצב Datastore. מידע נוסף מופיע במאמרי העזרה של Datastore mode Ruby API.

כדי לבצע אימות במצב Datastore, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

# project_id = "project-id"
# output_url_prefix = "gs://bucket-name"
op = client.export_entities project_id: project_id, output_url_prefix: output_url_prefix

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 exported"

המאמרים הבאים

כדי לחפש ולסנן דוגמאות קוד למוצרים אחרים של Google Cloud , אפשר להיעזר בGoogle Cloud דפדפן לדוגמה.