dsfo22654 2016-06-01 21:05
浏览 29
已采纳

使用Go处理Google数据存储中的架构更改?

I added a new attribute to Go struct, persisted in a datastore entity kind.

I tried to load entities using a filter on the new attribute:

q := datastore.NewQuery("Person").Filter("Employed =", false)

This only worked as intended for persons created after the new attribute was added.

I had expected persons created before the attribute was added to be incleded in the filter, but they didnt have the attribute at all, so they were be excluded by the filter.

I have thought of two ways to handle this:

  • First load all entities and filter in a second step with a loop. This adds complexity to the code.
  • Batch load and resave all entities, which adds the property and sets it to false. I have to remember to do this every time I add a new attribute.

Is there a batter way to handle these types of entity schema changes?

  • 写回答

1条回答 默认 最新

  • douqing0713 2016-06-02 08:30
    关注

    The proper way is to re-save old entities if you can afford it.

    You can use Python map/reduce library to update old entities.

    If you want to stick to Go you can go with simple batch processing using task queues & query cursors.

    In our Python app we prefer "in-flight hot update" approach whenever it is possible - new version loads entities and check for missing properties. If some props are obsolete we would keep them for a month or to and then add logic to remove them. This allows to migrate gradually and often rollback without issues (shit happens) or at least limit impact to small % of records. Also it's more cost effective as you do not pay for additional reads/writes. Once we sure we ok we can run a mapreduce. This does not involve downtime. You may need or don't need downtime - it's really depend on your change.

    With Go it may be a bit tricky as Go would panic if you try to load entity that has no corresponding fields on a struct. There should be some workarounds about it (I remember something about special interface or a struct where you can load arbitrary entity) but I have no much practical experience with Go on GAE yet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)