在cassandra([cqlsh 5.0.1 | Cassandra 2.2.0 | CQL spec 3.3.0 | Native protocol v4]
)中,我创建了下表
create table flotilla.events1 (
f1 int,
f2 int,
f3 int,
f4 timestamp,
f5 text,
f6 text,
f7 text,
f8 int,
f9 text,
f10 double,
f11 int,
primary key (f2, f1)
);
并尝试插入以下数据(/tmp/b.csv
):
f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11
1,751168360,0,'2014-04-01T09:56:13.491','a','G','G',296,'G',242,242
2,751168360,0,'2014-04-01T09:56:13.491','a','G','G',296,'G',8,8
3,751168360,0,'2014-04-01T09:56:13.491','a','G','G',296,'G',0,0
4,751168360,0,'2014-04-01T09:56:13.491','a','G','G',296,'G',1,1
5,751168360,0,'2014-04-01T09:56:13.491','a','G','G',296,'G',0,0
6,751168360,0,'2014-04-01T09:56:13.491','a','G','G',296,'G',0,0
7,751168360,0,'2014-04-01T09:56:13.491','a','A','A',279,'P',2.55,255
8,751168360,0,'2014-04-01T09:56:13.491','a','A','A',279,'P',0,0
使用以下命令:
copy flotilla.events1 (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11) from '/tmp/b.csv' with header=true;
卡珊德拉说:
8 rows imported in 0.550 seconds.
当我查看表格时:
SELECT * from flotilla.events1 ;
f2 | f1 | f10 | f11 | f3 | f4 | f5 | f6 | f7 | f8 | f9
----+----+-----+-----+----+----+----+----+----+----+----
(0 rows)
没有一个错误。什么是黑客?
但是,如果我插入INSERT INTO
的第一行:
insert into flotilla.events1 (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11) values (1,751168360,0,'2014-04-01T09:56:13.123','a','G','G',296,'G',242,242) ; select * from flotilla.events1;
f2 | f1 | f10 | f11 | f3 | f4 | f5 | f6 | f7 | f8 | f9
-----------+----+-----+-----+----+--------------------------+----+----+----+-----+----
751168360 | 1 | 242 | 242 | 0 | 2014-04-01 09:56:13+0200 | a | G | G | 296 | G
(1 rows)
真是个黑客?
答案 0 :(得分:3)
与我一小时前回答question的方式类似,要实现此功能,我只需从CSV文件中删除所有单引号。
f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11
1,751168360,0,2014-04-01T09:56:13.491,a,G,G,296,G,242,242
2,751168360,0,2014-04-01T09:56:13.491,a,G,G,296,G,8,8
3,751168360,0,2014-04-01T09:56:13.491,a,G,G,296,G,0,0
4,751168360,0,2014-04-01T09:56:13.491,a,G,G,296,G,1,1
5,751168360,0,2014-04-01T09:56:13.491,a,G,G,296,G,0,0
6,751168360,0,2014-04-01T09:56:13.491,a,G,G,296,G,0,0
7,751168360,0,2014-04-01T09:56:13.491,a,A,A,279,P,2.55,255
8,751168360,0,2014-04-01T09:56:13.491,a,A,A,279,P,0,0
然后,COPY
命令运行正常:
aploetz@cqlsh:stackoverflow> COPY events1 (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11) FROM '/home/aploetz/cassandra_stack/b.csv' WITH header=true;
8 rows imported in 0.627 seconds.
aploetz@cqlsh:stackoverflow> SELECT * FROM events1 ;
f2 | f1 | f10 | f11 | f3 | f4 | f5 | f6 | f7 | f8 | f9
-----------+----+------+-----+----+--------------------------+----+----+----+-----+----
751168360 | 1 | 242 | 242 | 0 | 2014-04-01 09:56:13-0500 | a | G | G | 296 | G
751168360 | 2 | 8 | 8 | 0 | 2014-04-01 09:56:13-0500 | a | G | G | 296 | G
751168360 | 3 | 0 | 0 | 0 | 2014-04-01 09:56:13-0500 | a | G | G | 296 | G
751168360 | 4 | 1 | 1 | 0 | 2014-04-01 09:56:13-0500 | a | G | G | 296 | G
751168360 | 5 | 0 | 0 | 0 | 2014-04-01 09:56:13-0500 | a | G | G | 296 | G
751168360 | 6 | 0 | 0 | 0 | 2014-04-01 09:56:13-0500 | a | G | G | 296 | G
751168360 | 7 | 2.55 | 255 | 0 | 2014-04-01 09:56:13-0500 | a | A | A | 279 | P
751168360 | 8 | 0 | 0 | 0 | 2014-04-01 09:56:13-0500 | a | A | A | 279 | P
(8 rows)
删除单引号,然后它应该可以正常工作。
答案 1 :(得分:1)
我发布了一个额外的答案,因为最初的问题是导入这些数据(它位于压缩的csv.gz文件中):
f1;f2;f3;f4;f5;f6;f7;f8;f9;f10;f11
1;751168360;0;'2014-04-01T09:56:13.491111';'a';'G';'G';296;'G';242;242
2;751168360;0;'2014-04-01T09:56:13.491111';'a';'G';'G';296;'G';8;8
3;751168360;0;'2014-04-01T09:56:13.491111';'a';'G';'G';296;'G';0;0
4;751168360;0;'2014-04-01T09:56:13.491111';'a';'G';'G';296;'G';1;1
5;751168360;0;'2014-04-01T09:56:13.491111';'a';'G';'G';296;'G';0;0
6;751168360;0;'2014-04-01T09:56:13.491111';'a';'G';'G';296;'G';0;0
7;751168360;0;'2014-04-01T09:56:13.491111';'a';'A';'A';279;'P';2.55;255
8;751168360;0;'2014-04-01T09:56:13.491111';'a';'A';'A';279;'P';0;0
仔细选择参数(DELIMITER, QUOTE, HEADER
)可以无错误地导入数据:
zcat /tmp/a.csv.gz | cqlsh -e "copy flotilla.events1 (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11) from STDIN WITH DELIMITER=';' and QUOTE='''' and header=true;"
然而我仍然认为微秒(.491111
)会导致错误,因为cassandra只能处理几毫秒