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

如何使用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,
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况