duanlang0025
2018-06-04 04:46
浏览 541
已采纳

如何使用google.protobuf.timestamp和cassandra

I'm using "google/protobuf/timestamp.proto" to define timestamp type in golang struct:

import "google/protobuf/timestamp.proto";    
message User {
    string id = 1; 
    ...
    google.protobuf.Timestamp created_at = 12;
    google.protobuf.Timestamp updated_at = 13;
    google.protobuf.Timestamp last_login = 14;
}

and when insert to cassandra using cqlx:

req.CreatedAt = ptypes.TimestampNow()
// I also try with:
// req.CreatedAt = &timestamp.Timestamp{Seconds: time.Now().Unix(), Nanos: int32(time.Now().Nanosecond())}

I always get this error message: can not marshal timestamp.Timestamp into timestamp. Anyone have work with this please help

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpg76975 2018-06-04 05:03
    已采纳

    I just ran into the same thing last week. You need to use ptypes

    ts, err := ptypes.Timestamp(result.GetDateDone())
    if err != nil {
        ts = time.Now().UTC()
    }
    err = trr.cassandra.Execute(query,
        result.TaskID,
        result.TaskName,
        result.Status,
        result.Result,
        ts,
        result.Traceback,
        result.Children,
    )
    

    Here result is a proto message defined by:

    message TaskResult {
        string TaskID = 1;
        string TaskName = 2;
        string Status = 3;
        bytes Result = 4;
        google.protobuf.Timestamp DateDone = 5;
        bytes Traceback = 6;
        bytes Children = 7;
    }
    

    And the column family has a field

    date_done timestamp,
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题