gocqlgocql

GoCQL : Marshal string into timestamp


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?


Solution

  • 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{}{}
    }