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.

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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改