2016-04-13 98 views
0

如果我今天開始一個項目什麼是我可以用於Cassandra的最佳Java驅動程序?Cassandra的Java驅動程序?

+0

由於所有的實施例。我也是這麼想的!謝謝您的回覆。我不能upvote任何人,因爲你們都是相同的答案,我不能投票所有:) –

回答

4

奇怪的問題,只有1官方Java驅動程序:https://github.com/datastax/java-driver

它不象你有選擇的打...

+0

當我GOOGLE了我有幾個選項,使用我們可以從卡桑德拉例如astyanax等訪問數據。我的意思是。這是不是很奇怪? –

+0

是的,這很奇怪,因爲Astyanax自多年以來一直沒有更新。看看他們的Github上最後一次提交的日期... – doanduyhai

+0

作爲Cassandra的新手,我對不同的包裝實現感到困惑,人們迄今爲止已經完成了,其中包括你的「Achilles」:)。無論如何,我得到了我的答案。謝謝。 –

0

只有官方Java驅動程序來自datastax,它同時具有cassandra的社區版和企業版。

https://github.com/datastax/java-driver

但是,它有可從datastax太對象映射器,可你的表映射到你的POJO類,有點,你在休眠怎麼做。

0

這裏是CRUD操作使用Datastax駕駛員在Java

public class CassandraCRUDdemo { 

/* 
* CREATE KEYSPACE demo WITH REPLICATION = { 'class' : 'SimpleStrategy', 
* 'replication_factor' : 1 }; 
* 
* CREATE TABLE users (user_name varchar PRIMARY KEY, password varchar, 
* gender varchar, session_token varchar, state varchar, birth_year bigint 
*); 
*/ 

public static void main(String[] args) { 

    Cluster cluster; 
    Session session; 

    // Connect to the cluster and keyspace "demo" 
    cluster = Cluster.builder().addContactPoint("sbshad10").withPort(9042) 
      .build(); 
    session = cluster.connect("demo"); 

    // Insert one record into the users table 
    session.execute("INSERT INTO users (user_name, password, gender, session_token, state,birth_year) VALUES ('Jones', 'pwd', 'Austin', '[email protected]', 'Bob',35)"); 

    // Use select to get the user we just entered 
    ResultSet results = session 
      .execute("SELECT * FROM users WHERE user_name='Jones'"); 
    for (Row row : results) { 
     System.out.format("%s %d\n", row.getString("user_name"), 
       row.getLong("birth_year")); 
    } 

    // Update the same user with a new age 
    session.execute("update users set birth_year = 36 where user_name = 'Jones'"); 
    // Select and show the change 
    results = session.execute("select * from users where user_name='Jones'"); 
    for (Row row : results) { 
     System.out.format("%s %d\n", row.getString("user_name"), 
       row.getLong("birth_year")); 

    } 

    // Delete the user from the users table 
    session.execute("DELETE FROM users WHERE user_name = 'Jones'"); 
    // Show that the user is gone 
    results = session.execute("SELECT * FROM users"); 
    for (Row row : results) { 
     System.out.format("%s %d %s %s %s\n", row.getString("user_name"), 
       row.getLong("birth_year"), row.getString("state"), 
       row.getString("session_token"), row.getString("gender")); 
    } 

    // Clean up the connection by closing it 
    cluster.close(); 
}} 

和pom.xml中

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>cassandra</groupId> 
<artifactId>cassandra-demo</artifactId> 
<version>0.0.1-SNAPSHOT</version> 

<dependencies> 
    <!-- https://mvnrepository.com/artifact/com.datastax.cassandra/cassandra-driver-core --> 
    <dependency> 
     <groupId>com.datastax.cassandra</groupId> 
     <artifactId>cassandra-driver-core</artifactId> 
     <version>3.1.0</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> 
    <dependency> 
     <groupId>com.google.guava</groupId> 
     <artifactId>guava</artifactId> 
     <version>19.0</version> 
    </dependency> 
    <!-- <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> 
     <version>1.2</version> </dependency> --> 
    <!-- https://mvnrepository.com/artifact/log4j/log4j --> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.14</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>1.6.1</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> 
    <dependency> 
     <groupId>io.netty</groupId> 
     <artifactId>netty-all</artifactId> 
     <version>4.0.27.Final</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/com.codahale.metrics/metrics-core --> 
    <dependency> 
     <groupId>com.codahale.metrics</groupId> 
     <artifactId>metrics-core</artifactId> 
     <version>3.0.2</version> 
    </dependency> 


</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

相關問題