duandaodao6951 2017-10-08 18:57
浏览 13
已采纳

如何在Go Lang中将实体追加到数据存储

Need info on how to append an entity in Google cloud data store.

Put function is always overwritten with new values.

Any suggestions would be helpful.

09-OCT-2017:

I Used below code, but still updating entity instead of appending.(its erasing old & updated with new value, but i want to retain both values)

client, err := datastore.NewClient(ctx, projectID)
tx, err := client.NewTransaction(ctx)
if err != nil {
    log.Fatalf("Failed to create client: %v", err)
}

fmt.Fprint(w, input)
taskKey := datastore.NameKey("Entity", "stringID", nil)
var task Entity
if err := tx.Get(taskKey, &task); err != nil {
    log.Fatalf("tx.Get: %v", err)
}
task.Value = input
if _, err := tx.Put(taskKey, &task); err != nil {
    log.Fatalf("tx.Put: %v", err)
}
if _, err := tx.Commit(); err != nil {
    log.Fatalf("tx.Commit: %v", err)
}
  • 写回答

2条回答 默认 最新

  • dpylt7626401 2017-10-09 08:49
    关注

    There can be only one instance, only one entity bound / denoted by the same datastore key.

    And entities (bound to a key) can only be overwritten, not updated / extended / appended gradually.

    So if you already have an entity saved, to update/ modify it, you have to first load it, then modify the entity in memory, and write out (save) the modified entity. This save will overwrite the existing entity in the datastore.

    If for a property you want to store multiple values, the type of that property must support storing multiple values. Slices in Go are such types.

    So in your example your entity should look like this:

    type Entity struct {
        Values []string 
    }
    

    When you load an existing Entity, you have to append the new value to its Values field, something like this (in pseudo code):

    e := ... // load existing entity
    e.Values = append(e.Values, input) // Append new data to Values
    // And now save Entity (e) with the same key
    

    In code:

    client, err := datastore.NewClient(ctx, projectID)
    tx, err := client.NewTransaction(ctx)
    if err != nil {
        log.Fatalf("Failed to create client: %v", err)
    }
    
    fmt.Fprint(w, input)
    taskKey := datastore.NameKey("Entity", "stringID", nil)
    var task Entity
    if err := tx.Get(taskKey, &task); err != nil {
        log.Fatalf("tx.Get: %v", err)
    }
    task.Values = append(task.Values, input)
    if _, err := tx.Put(taskKey, &task); err != nil {
        log.Fatalf("tx.Put: %v", err)
    }
    if _, err := tx.Commit(); err != nil {
        log.Fatalf("tx.Commit: %v", err)
    }
    

    If you need to index by this Values property, you might run into troubles if it contains many values. See this possible duplicate for more details: App Engine Datastore: How to set multiple values on a property using golang?

    If you run into this problem, you should consider modeling and storing your data in a different format, e.g. saving in multiple entities, where one entity would only store a single input, connected with the key it belongs to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论