duanlang0025
duanlang0025
2018-06-04 04:46

如何使用google.protobuf.timestamp和cassandra

  • 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 dpg76975 3年前

    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,
    
    点赞 评论 复制链接分享

为你推荐