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 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序