使用gocql查询解析时间戳。

huangapple go评论91阅读模式
英文:

Unmarshal timestamp using gocql query

问题

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

英文:

How to accept Cassandra's timestamp and convert into string using gocql?
What will be the query?

答案1

得分: 9

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)
}

// 输出 2014-04-17 01:55:26.434 +0000 UTC
fmt.Printf(event_time.String())
英文:

gocql can read it into a time.Time, which can then be converted to string by calling String(). For example:

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())

huangapple
  • 本文由 发表于 2014年4月11日 02:11:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/22995792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定