使用gocql查询解组时间戳

时间:2014-04-10 18:11:26

标签: go cassandra

如何接受Cassandra的时间戳并使用gocql转换为字符串? 什么是查询?

1 个答案:

答案 0 :(得分:7)

gocql可以将其读入time.Time,然后可以通过调用String()将其转换为字符串。例如:

if err := session.Query(`CREATE TABLE events (
        event text,
        event_time timestamp,
        PRIMARY KEY (event, event_time),
    ) WITH CLUSTERING ORDER BY (event_time DESC);
    `).Exec(); err != nil {
    log.Fatal("create table events:", err)
}

tm := time.Now()
if err := session.Query(`INSERT INTO events
        (event, event_time)
        VALUES (?, ?)`,
    "click", tm).Exec(); err != nil {
    log.Fatal("insert into events: ", err)
}

var event_time time.Time

if err := session.Query(`SELECT event_time FROM events LIMIT 1`).
    Consistency(gocql.One).Scan(&event_time); err != nil {
    log.Fatal("select from events:", err)
}

// prints 2014-04-17 01:55:26.434 +0000 UTC
fmt.Printf(event_time.String())