|
| 1 | +# Copyright 2016, Google, Inc. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +# [START datastore_admin_client_create] |
| 15 | +from google.cloud.datastore_admin_v1.gapic import datastore_admin_client |
| 16 | + |
| 17 | + |
| 18 | +def client_create(): |
| 19 | + """Creates a new Datastore admin client.""" |
| 20 | + client = datastore_admin_client.DatastoreAdminClient() |
| 21 | + |
| 22 | + print("Admin client created\n") |
| 23 | + return client |
| 24 | +# [END datastore_admin_client_create] |
| 25 | + |
| 26 | + |
| 27 | +# [START datastore_admin_entities_export] |
| 28 | +def export_entities(project_id, output_url_prefix): |
| 29 | + """ |
| 30 | + Exports a copy of all or a subset of entities from |
| 31 | + Datastore to another storage system, such as Cloud Storage. |
| 32 | + """ |
| 33 | + # project_id = "project-id" |
| 34 | + # output_url_prefix = "gs://bucket-name" |
| 35 | + client = datastore_admin_client.DatastoreAdminClient() |
| 36 | + |
| 37 | + op = client.export_entities(project_id, output_url_prefix) |
| 38 | + response = op.result(timeout=200) |
| 39 | + |
| 40 | + print("Entities were exported\n") |
| 41 | + return response |
| 42 | +# [END datastore_admin_entities_export] |
| 43 | + |
| 44 | + |
| 45 | +# [START datastore_admin_entities_import] |
| 46 | +def import_entities(project_id, input_url): |
| 47 | + """Imports entities into Datastore.""" |
| 48 | + # project_id := "project-id" |
| 49 | + # input_url := "gs://bucket-name/overall-export-metadata-file" |
| 50 | + client = datastore_admin_client.DatastoreAdminClient() |
| 51 | + |
| 52 | + op = client.import_entities(project_id, input_url) |
| 53 | + response = op.result(timeout=200) |
| 54 | + |
| 55 | + print("Entities were imported\n") |
| 56 | + return response |
| 57 | +# [END datastore_admin_entities_import] |
| 58 | + |
| 59 | + |
| 60 | +# [START datastore_admin_index_get] |
| 61 | +def get_index(project_id, index_id): |
| 62 | + """Gets an index.""" |
| 63 | + # project_id := "my-project-id" |
| 64 | + # index_id := "my-index" |
| 65 | + client = datastore_admin_client.DatastoreAdminClient() |
| 66 | + index = client.get_index(project_id, index_id) |
| 67 | + |
| 68 | + print("Got index: %v\n", index.index_id) |
| 69 | + return index |
| 70 | +# [END datastore_admin_index_get] |
| 71 | + |
| 72 | + |
| 73 | +# [START datastore_admin_index_list] |
| 74 | +def list_indexes(project_id): |
| 75 | + """Lists the indexes.""" |
| 76 | + # project_id := "my-project-id" |
| 77 | + client = datastore_admin_client.DatastoreAdminClient() |
| 78 | + |
| 79 | + indexes = [] |
| 80 | + for index in client.list_indexes(project_id): |
| 81 | + indexes.append(index) |
| 82 | + print("Got index: %v\n", index.index_id) |
| 83 | + |
| 84 | + print("Got list of indexes\n") |
| 85 | + return indexes |
| 86 | +# [END datastore_admin_index_list] |
0 commit comments