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

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛