GoCQL:将字符串转换为时间戳 [英] GoCQL : Marshal string into timestamp
本文介绍了GoCQL:将字符串转换为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发带有聚类列的时间序列数据模型,即
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)
我希望针对分区列 id和集群列 time执行选择。例如,id:='1',时间戳:='2017-10-09'
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),
}
}
检查iterable.Close()的err后,发现编组错误
After checking err on iterable.Close(), found an error in marshalling
{错误 :[无法将字符串编组为时间戳]}
{"errors":["can not marshal string into timestamp"]}
我该如何解决?
推荐答案
下面是我如何通过将(时间戳)字符串文字转换为类型time来解决此问题的方法。
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{}{}
}
这篇关于GoCQL:将字符串转换为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文