【问题标题】:GoCQL : Marshal string into timestampGoCQL:将字符串编组为时间戳
【发布时间】:2017-10-10 20:35:03
【问题描述】:

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

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

我希望对分区列“id”和聚类列“时间”执行选择。例如 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后,发现marshalling出错

{"errors":["无法将字符串编组为时间戳"]}

我该如何解决这个问题?

【问题讨论】:

  • 你可以尝试使用thisfmt.Println(reflect.TypeOf(m["time"])打印m["time"]的类型吗?
  • 类型为time.Time。
  • 我已经解决了这个问题。似乎时间戳需要是 time.Time 的类型,它目前使用字符串文字。在我将字符串转换为 time.Time 并输入函数后,一切正常。
  • 很高兴你修好了 :)

标签: go cql gocql


【解决方案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{}{}
}

【讨论】:

    猜你喜欢
    • 2020-11-22
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    相关资源
    最近更新 更多