小编典典

GoCQL:将字符串转换为时间戳

go

我正在开发一个具有聚类列的时间序列数据模型,即

CREATE TABLE events (
    id text,
    time timestamp,
    type text,
    val double,
    PRIMARY KEY (id, time)
) WITH CLUSTERING ORDER BY (time DESC)

我希望针对分区列“ id”和集群列“ time”执行选择。例如,id:=‘1’,时间戳:=‘2017-10-09’

query := "SELECT id, time, type, val FROM events WHERE id=? AND time>=?"
iterable := Cassandra.Session.Query(query, id, timestamp).Consistency(gocql.One).Iter()
for iterable.MapScan(m) {
        found = true
        event = Event{
                ID:       m["id"].(string),
                Time:     m["time"].(time.Time),
                Type:     m["type"].(string),
                Val:      m["val"].(float64),
        }
}

检查iterable.Close()的err后,发现编组错误

{“错误”:[“无法将字符串编组为时间戳”]}

我该如何解决?


阅读 439

收藏
2020-07-02

共1个答案

小编典典

这是我最后通过将字符串文字(带有时间戳)转换为类型time来解决此问题的方法。

timestamp = "2017-10-09T13:25:00.000Z"
tsAfter,err = time.Parse(model.TimeLayout, timestamp)
if err != nil {
    errs = append(errs, err.Error())
}

log.Printf("GET param [id = %s]", idStr)
log.Printf("GET param [after = %s]", tsAfter.String())

m := map[string]interface{}{}
query := "SELECT id, time, type, val FROM events WHERE id = ? AND time >= ?"
iterable := cql.Session.Query(query, idStr, tsAfter).Consistency(gocql.One).Iter()

for iterable.MapScan(m) {
    eventList = append(eventList, model.Event{
        ID:         m["id"].(string),
        Time:       m["time"].(time.Time),
        Type:       m["type"].(string),
        Val:        m["val"].(float64),
    })
    m = map[string]interface{}{}
}
2020-07-02