GoCQL:将字符串编组为时间戳
GoCQL : Marshal string into timestamp
我正在开发一个带有聚类列的时间序列数据模型,即
CREATE TABLE events (
id text,
time timestamp,
type text,
val double,
PRIMARY KEY (id, time)
) WITH CLUSTERING ORDER BY (time DESC)
我希望对分区列 'id' 和集群列 'time' 执行 select。例如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() 错误后,发现编组错误
{"errors":["can not marshal string into timestamp"]}
我该如何解决这个问题?
这是我最终通过将字符串文字(时间戳)转换为类型 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{}{}
}
我正在开发一个带有聚类列的时间序列数据模型,即
CREATE TABLE events (
id text,
time timestamp,
type text,
val double,
PRIMARY KEY (id, time)
) WITH CLUSTERING ORDER BY (time DESC)
我希望对分区列 'id' 和集群列 'time' 执行 select。例如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() 错误后,发现编组错误
{"errors":["can not marshal string into timestamp"]}
我该如何解决这个问题?
这是我最终通过将字符串文字(时间戳)转换为类型 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{}{}
}