Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshal timestamp using gocql query

Tags:

go

cassandra

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

like image 722
BigDataLearner Avatar asked Feb 03 '26 19:02

BigDataLearner


1 Answers

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())
like image 118
Dan Weaver Avatar answered Feb 05 '26 08:02

Dan Weaver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!