[英]Connect to Cassandra using JDBC driver
I am trying to connect to Cassandra using JDBC driver. 我正在尝试使用JDBC驱动程序连接到Cassandra。 My Cassandra version is 2.2.4 and java 1.7.0_101: 我的Cassandra版本是2.2.4和Java 1.7.0_101:
Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
Connection con = DriverManager.getConnection("jdbc:cassandra://10.11.15.242:9160/edgeview");
Where "edgeview" is my keyspace. 其中“ edgeview”是我的关键空间。 I have connected to cassandra using 9160 port cuz it does not allow me to connect without a libthrift library(Cross language library). 我已使用9160端口cuz连接到cassandra,它不允许我在没有libthrift库(跨语言库)的情况下进行连接。
If I am trying to query cassandra it always gives me error as "String index out of range: -1" . 如果我要查询cassandra,它总是给我错误,例如“字符串索引超出范围:-1”。
It seems like some compatibility issue as lots of my finding says Cassandra 2.2+ with thrift has been deprecated. 似乎存在一些兼容性问题,因为我的很多发现都说过时的Cassandra 2.2+已过时。
Any help on how can I proceed with this? 我该如何进行任何帮助?
Thanx in advance 提前感谢
I am using JPA with cassandra and therefore jdbc 我正在将JPA与cassandra和jdbc一起使用
<persistence-unit name="Cassandra" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.github.cassandra.jdbc.CassandraDriver" />
</properties>
</persistence-unit>
You can use jdbc with cassandra though there is a non jdbc solution from datastax 尽管datastax提供了非jdbc解决方案,但您可以将jdbc与cassandra一起使用
maven dependency Maven依赖
com.github.zhicwu enter code here
cassandra-jdbc-driver 0.6.1 com.github.zhicwu enter code here
cassandra-jdbc-driver 0.6.1
regards 问候
I don't know if it mandatory for you to use jdbc. 我不知道您是否必须使用jdbc。 If not I would suggest that you use the datastax driver for cassandra . 如果没有,我建议您为cassandra使用datastax驱动程序 。 I have used it and it is really easy. 我用过它,真的很容易。 If you have trouble setting it up I can help. 如果您在设置时遇到问题,可以提供帮助。
following the Cassandra documentation you should used the Datastax Java API. 根据Cassandra文档,您应该使用Datastax Java API。 Cassandra it's not a SQL database but a NOSQL therefore there are totally a different system. Cassandra不是SQL数据库,而是NOSQL,因此存在完全不同的系统。
If you have a maven project add these dependencies on the pom.xml configuration file: 如果您有一个maven项目,请在pom.xml配置文件中添加以下依赖项:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-extras</artifactId>
<version>3.1.0</version>
</dependency>
Here, the maven repository URL: https://mvnrepository.com/artifact/com.datastax.cassandra/cassandra-driver-core 这里是Maven资料库的网址: https : //mvnrepository.com/artifact/com.datastax.cassandra/cassandra-driver-core
After, create your first Hello Cassandra main like below: 之后,创建您的第一个Hello Cassandra主程序,如下所示:
package cassandraMain;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
public class Main {
public static void main(String[] args) {
// Only a short example
// You must to take care about the several exception and the usage of the cluster and session objects...
Cluster cluster;
Session session;
// Connect to the local cluster with the keyspace demodb
cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
session = cluster.connect("demodb");
// Insert one record - only for test...
session.execute("INSERT INTO test (username, name, surname)"
+ " VALUES ('test_"+System.currentTimeMillis()+"', 'Andrea', 'Giassi')");
session.close();
cluster.close();
}
}
I hope that this can help you 希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.