COPY 只能在cqlsh 中使用,所以我不能在Java 中将它用作CQL 命令。有没有类似的方法可以将 CSV 文件导入 table?

COPY can only be used in cqlsh, and so I can't use it as a CQL command in Java. Is there a similar way to import a CSV into a table?

我有一个 CSV 文本文件,我想用它来填充我的键空间中的 table(使用 Java)。

我尝试使用 COPY 命令,但我刚刚意识到 COPY 命令不是实际的 CQL 命令:它只能 运行 与 cqlsh shell。

我尝试做的事情:

session.execute("COPY table(Column1,Column2,Column3) 
                 FROM ('textfile.txt') 
                 WITH DELIMITER='\t'");

(以及之前的代码,如果您需要它作为上下文):

    Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
    Session session = cluster.connect( );
    session.execute("CREATE KEYSPACE test WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '1'};";);
    session.execute("USE test");
    session.execute("CREATE TABLE table(Column1 text, Column2 text, Column3 text, PRIMARY KEY(Column1))");

我收到的主要错误信息是:

Exception in thread "main" com.datastax.driver.core.exceptions.SyntaxError: line 1:0 no viable alternative at input 'COPY' ([COPY]...)

我用谷歌搜索了一下,发现只能通过 cqlsh 使用 COPY。

是否有其他方法可以轻松地将 CSV 文件导入密钥空间?我将能够通过 Java?

中的 CQL 命令使用的一个

谢谢!

你说得对 'copy' 是一个 cqlsh (shell) 命令而不是 CQL(协议)命令。以下是解释适用于每种情况的相关文档:

要从 Java 应用中的 CSV 文件导入,您需要自己编写。一种流行的工具是用 Java 编写的 cassandraloader。您可能还可以从其他源代码片段中学习。