GoCQL : Marshal string into timestamp

青春壹個敷衍的年華 提交于 2019-12-10 11:22:49

问题


I am developing a time series data model with clustering column i.e.

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

I wish to perform a select against the partition column 'id' and clustering column 'time'. For example, id:='1', timestamp:='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),
        }
}

After checking err on iterable.Close(), found an error in marshalling

{"errors":["can not marshal string into timestamp"]}

How could I fix this?


回答1:


Here is how I end up solving this by transforming string literal (of timestamp) to type time.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{}{}
}


来源:https://stackoverflow.com/questions/46640544/gocql-marshal-string-into-timestamp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!