2016-04-13 13 views
0

もし私が今日プロジェクトを始めるなら、Cassandraに使える最高のJavaドライバは何ですか?私たちは、datastax cassandra driverを使用しているJava Driver for Cassandra?

+0

おかげですべてのJavaでDatastaxドライバを使用してCRUD操作の一例です。私もそう思っていた!ご返信ありがとうございます。私はあなたのすべてが実際に同じ答えであり、私はすべて投票できないので、誰もがupvoteできません:) –

答えて

4

奇妙な質問は、1つだけの公式のJavaドライバがあります。それは私たちが必要とするすべての機能を提供します。

+0

私は私が私たちがcassandra例astyanaxなどからデータにアクセスすることができるいくつかのオプションを使用してGoogle検索しました。私はそれを意味しました。まだ変だ? –

+0

はい、それはAstyanaxが何年も更新されていないので変です。彼らのGithubの最後のコミットの日付を見てください... – doanduyhai

+0

カサンドラの新しい人として、私は別のラッパー実装の間で混乱していました。私はとにかく私の答えを得た。ありがとう。 –

0

公式のJavaドライバは、datastaxのもので、これにはcassandraのコミュニティ版とエンタープライズ版があります。

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

しかし、それはあなたが休止状態で行う方法、多少、あなたのPOJOクラスにあなたのテーブルをマッピングすることができますあまりにもdatastaxから使用可能なオブジェクトマッパーを、持っています。

0

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>