英文:
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())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论