【问题标题】:Java Driver for Cassandra?Cassandra的Java驱动程序?
【发布时间】:2016-08-04 15:17:42
【问题描述】:

如果我今天开始一个项目,我可以为 Cassandra 使用的最佳 Java 驱动程序是什么?

【问题讨论】:

  • 谢谢大家。我也是这么想的!!感谢你的回复。我不能投票给任何人,因为你们所有人的答案都是一样的,我不能投票给所有人:)

标签: cassandra datastax


【解决方案1】:

奇怪的问题,官方Java驱动只有1个:https://github.com/datastax/java-driver

并不是说你有十几个选择......

【讨论】:

  • 当我用谷歌搜索时,我得到的选项很少,我们可以使用这些选项从 cassandra 访问数据,例如 astyanax 等。我的意思是。还是很奇怪吗?
  • 是的,这很奇怪,因为 Astyanax 多年来一直没有更新。在他们的 Github 上查看最后一次提交的日期 ...
  • 作为 Cassandra 的新手,我对不同的包装器实现感到困惑,到目前为止人们已经做过,包括你的“Achilles”:)。反正我得到了答案。谢谢。
  • Achilles 是一个对象映射器,比 Java 驱动级别高很多
【解决方案2】:

我们正在使用datastax cassandra driver。它提供了我们需要的所有功能。

【讨论】:

    【解决方案3】:

    只有官方的 Java 驱动来自 datastax,它有社区版和企业版的 cassandra。

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

    不过,它也有来自 datastax 的对象映射器,它可以将您的表映射到您的 pojo 类,有点像您在休眠中的操作方式。

    【讨论】:

      【解决方案4】:

      Datastax 驱动程序是最好的,有很多支持和广泛使用。 https://github.com/datastax/java-driver

      【讨论】:

        【解决方案5】:

        这里是在java中使用Datastax驱动的CRUD操作示例

        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', 'bob@example.com', '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>
        

        【讨论】:

          猜你喜欢
          • 2020-03-30
          • 1970-01-01
          • 1970-01-01
          • 2018-02-15
          • 2017-07-13
          • 2014-09-15
          • 2014-05-04
          • 2016-07-11
          • 1970-01-01
          相关资源
          最近更新 更多