I have been trying to use COPY FROM
to insert into a Cassandra table that has a timestamp
type column. However, I encountered the following error:
code=2200 [Invalid query] message="unable to coerce '2015-03-06 18:11:33GMT' to a formatted date (long)"
Aborting import at record #3. Previously-inserted values still present.
0 rows imported in 0.211 seconds.
The content of the CSV file was actually created with a COPY TO command. My TZ
environment variable has been set to GMT
.
I did some searching and found a post here that mentioned using Z
instead of GMT
as the timezone in the data string, i.e. '2015-03-06 18:11:33Z'. If I replace all the GMT
in my CSV with Z
, COPY FROM
worked. Link for the post here:
unable to coerce '2012/11/11' to a formatted date (long)
When I run a SELECT
on this table, the datetime column shows up in the format of: 2015-03-06 17:53:23GMT.
Further info, there was a bug about 'Z' timezone but it was fixed. Link: https://issues.apache.org/jira/browse/CASSANDRA-6973
So my question is, is there a way that I can run COPY TO
so that it writes Z
instead of GMT
for time zone?
Alternatively, is there a way I can make COPY FROM
work with GMT
?
Thanks.
Note: The solution is in the comment from @Aaron for this post. Yes, it's a hack but it works.
Best Answer
I think what is happening here, is that you are getting bit by your
time_format
property in your~/.cassandra/cqlshrc
file.COPY
uses this setting when exporting your timestamp data during aCOPY TO
. CQLSH uses the Python strftime formats. It is interesting to note that the lowercase %z and uppercase %Z seem to represent your problem.When I
SELECT
timestamp data with %Z (upper), it looks like this:When I try to
INSERT
a record using that date format, it fails:But when I alter
time_format
to use the (lowercase) %z the same query produces this:I can also
INSERT
data in this format:It also appears in this way when I run a
COPY TO
, and aCOPY FROM
of the same data/file also works.In summary, check your
~/.cassandra/cqlshrc
and make sure that you are either using the default setting, or this setting in the[ui]
section:It won't get you the 'Z' like you asked for, but it will allow you to
COPY TO/FROM
your data without having to muck with the CSV file.Edit
For those of you poor souls out there using CQLSH (or Cassandra, God help you) on Windows, the default location of the
cqlshrc
file isc:\Users\%USERNAME%\.cassandra\cqlshrc
.Edit - 20150903
Inspired by this question, I submitted a patch (CASSANDRA-8970) to allow users to specify a custom time format with
COPY
, and it was marked as "Ready To Commit" yesterday. Basically, this patch will allow this problem to be solved by doing the following:Edit - 20161010
The COPY command was improved in Cassandra 2.2.5, and the
TIMEFORMAT
option has been renamed toDATETIMEFORMAT
.From New options and better performance in cqlsh copy: