简体   繁体   中英

Connect to Cassandra using JDBC driver

I am trying to connect to Cassandra using JDBC driver. My Cassandra version is 2.2.4 and 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. I have connected to cassandra using 9160 port cuz it does not allow me to connect without a libthrift library(Cross language library).

If I am trying to query cassandra it always gives me error as "String index out of range: -1" .

It seems like some compatibility issue as lots of my finding says Cassandra 2.2+ with thrift has been deprecated.

Any help on how can I proceed with this?

Thanx in advance

I am using JPA with cassandra and therefore 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

maven dependency

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. If not I would suggest that you use the datastax driver for cassandra . 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 it's not a SQL database but a NOSQL therefore there are totally a different system.

If you have a maven project add these dependencies on the pom.xml configuration file:

    <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

After, create your first Hello Cassandra main like below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM