-2

Desired format to write time stamp, in cassandra database:

  2021-01-17 21:51:46.195

Using cqlsh syntax for insert query:

  1. am able to write the above format using toTimeStamp(toDate(now()))

  2. Using cqlsh, am unable to write the above format using toUnixTimestamp(now()). toUnixTimestamp(now()) writes the format 2021-01-17 21:51:46.195000+0000.


    layout := "2006-01-02T15:04:05.000Z"

    timestamp := "2021-01-17T21:51:46.195Z"
    createdDate, err := time.Parse(layout, timestamp)
    insertQueryString = "INSERT INTO mytable(created_date) " + "VALUES (?)"

gocql.Session.Query(insertQueryString, createdDate).Exec() code also writes createdDate into database similar to the format that cqlsh's toUnixTimestamp(now()) does, which is 2021-01-17 21:51:46.195000+0000 as shown below:

enter image description here


JimB already explained: "Again, time.Time has no format. You can format it as a string if you want, which is explained in numerous other answers."

But,

How to make gocql.Session.Query(insertQueryString, createdDate).Exec() write createdDate as 2021-01-17 21:51:46.195? Because cqlsh is able to write this desired format using toTimeStamp(toDate(now()))

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • You've been a regular on SO for quite some time now. You should know by now to [not paste images of text](https://meta.stackoverflow.com/q/303812/13860). – Jonathan Hall Jan 23 '21 at 19:28

1 Answers1

2

Cassandra doesn't keep timestamp as string, it's stored inside dataabase as long number with milliseconds precision 8 bytes long (see protocol specification). Conversion of the number into the string is the responsibility of the client program, and cqlsh is the python-based client shipped with Cassandra. And cqlsh has the configuration option that controls how the timestamp will be rendered - this is the datetimeformat option of the cqlshrc configuration file that you can customize to print dates as needed.

Alex Ott
  • 80,552
  • 8
  • 87
  • 132