如何更改 google 云数据存储种类名称?
How to change google cloud datastore kind name ?
我们正在使用 google cloud datastore
来存储数据,不幸的是最初没有遵循命名约定 kinds
现在我们想更改 数据存储中已经存在的种类的名称.
我们已经积累了大量数据,并且生成这些数据涉及大量计算,因此再次填充完整数据只是为了重命名一种类型对我们来说不是一个选择。
曾尝试找出它,但没有成功。那么有什么我错过的东西可以帮助实现它吗?
You cannot rename a kind
in datastore, Because the kind name is embedded with the key of each element in the datastore. You will have to write a
script to migrate all the data to the new kind
.
如果您只是想保持代码整洁,您可以在代码中为模型添加别名作为一种简单的解决方案。不过,在使用 GQL 时,您仍然需要通过原始名称来引用您的种类。
正如@Akash Dathan 所说,您可以通过编写脚本来迁移数据。我会提供更多细节:
GQL 不能用于此类迁移。请参阅以下文档(针对 Python),其中指出 'GQL is a SQL-like language for retrieving entities and keys':
https://cloud.google.com/appengine/docs/standard/python/datastore/gqlreference?csw=1
Python
有关实用的 Python 示例,请在此处参考幻灯片 29 '重命名模型:
https://www.slideshare.net/RyanMorlok/data-migrations-in-the-app-engine-datastore
Java
如果您正在使用 Java,您可能想查看 Java DataStore API -
如果您的实体管理是使用某种普遍实现的工作单元模式完成的(这很常见),那么使用 Java DataStore API 的基本迁移代码可能看起来像类似于:
List<MyKind1> allKind1Entities = myDataService.GetAll(MyKind1);
for (MyKind1 myKind1: allKind1Entities) {
MyKind2 myKind2entity = MyKind1.MapToMyKind2(myKind1);
try {
long myKind2entity Id = myDataService.Add(myKind2entity);
} catch (Exception e) {
e.printStackTrace();
}
}
myDataService.PurgeAll(MyKind1);
如果您通过另一个包含链接实体的 'kind' 在实体之间进行任何手动链接,那么它会变得更加复杂,因为您需要更新您的 ID 引用(因为新 myKind2
实体都将拥有新的 ID)
正在更新生产模式
如果您的应用程序 运行 在 App Engine 下并且您需要实时更新架构,您将在此处找到有关在代码中更新模型时的注意事项的更多详细信息 - https://cloud.google.com/appengine/articles/update_schema
这是来自 Google 的关于如何在不停机的情况下进行实时迁移的非常好的 YouTube - https://youtube.com/watch?v=qFU5aTT1Eqk
我们正在使用 google cloud datastore
来存储数据,不幸的是最初没有遵循命名约定 kinds
现在我们想更改 数据存储中已经存在的种类的名称.
我们已经积累了大量数据,并且生成这些数据涉及大量计算,因此再次填充完整数据只是为了重命名一种类型对我们来说不是一个选择。
曾尝试找出它,但没有成功。那么有什么我错过的东西可以帮助实现它吗?
You cannot rename a
kind
in datastore, Because the kind name is embedded with the key of each element in the datastore. You will have to write a script to migrate all the data to the newkind
.
如果您只是想保持代码整洁,您可以在代码中为模型添加别名作为一种简单的解决方案。不过,在使用 GQL 时,您仍然需要通过原始名称来引用您的种类。
正如@Akash Dathan 所说,您可以通过编写脚本来迁移数据。我会提供更多细节:
GQL 不能用于此类迁移。请参阅以下文档(针对 Python),其中指出 'GQL is a SQL-like language for retrieving entities and keys':
https://cloud.google.com/appengine/docs/standard/python/datastore/gqlreference?csw=1
Python
有关实用的 Python 示例,请在此处参考幻灯片 29 '重命名模型:
https://www.slideshare.net/RyanMorlok/data-migrations-in-the-app-engine-datastore
Java
如果您正在使用 Java,您可能想查看 Java DataStore API -
如果您的实体管理是使用某种普遍实现的工作单元模式完成的(这很常见),那么使用 Java DataStore API 的基本迁移代码可能看起来像类似于:
List<MyKind1> allKind1Entities = myDataService.GetAll(MyKind1);
for (MyKind1 myKind1: allKind1Entities) {
MyKind2 myKind2entity = MyKind1.MapToMyKind2(myKind1);
try {
long myKind2entity Id = myDataService.Add(myKind2entity);
} catch (Exception e) {
e.printStackTrace();
}
}
myDataService.PurgeAll(MyKind1);
如果您通过另一个包含链接实体的 'kind' 在实体之间进行任何手动链接,那么它会变得更加复杂,因为您需要更新您的 ID 引用(因为新 myKind2
实体都将拥有新的 ID)
正在更新生产模式
如果您的应用程序 运行 在 App Engine 下并且您需要实时更新架构,您将在此处找到有关在代码中更新模型时的注意事项的更多详细信息 - https://cloud.google.com/appengine/articles/update_schema
这是来自 Google 的关于如何在不停机的情况下进行实时迁移的非常好的 YouTube - https://youtube.com/watch?v=qFU5aTT1Eqk