我正在开发一个带有聚类列的时间序列数据模型,即
CREATE TABLE events (
id text,
time timestamp,
type text,
val double,
PRIMARY KEY (id, time)
) WITH CLUSTERING ORDER BY (time DESC)
我希望对分区列执行选择' id'和聚类列#time;#39;例如,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()上检查错误后,在编组中发现错误
{"错误":["无法将字符串编组到时间戳"]}
我该如何解决这个问题?
答案 0 :(得分:1)
以下是我最终通过将字符串文字(时间戳)转换为类型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{}{}
}