码迷,mamicode.com
首页 > 数据库 > 详细

Cassandra数据库Java访问

时间:2014-10-24 18:53:52      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   os   ar   使用   java   for   sp   

针对的时Cassandra 2.0 数据库

Java本地客户端访问Cassandra,首先建立Java工程,使用Maven进行管理。

引入依赖:

<dependency>
    <groupId>com.datastax.cassandra</groupId>
    <artifactId>cassandra-driver-core</artifactId>
    <version>2.1.0</version>
</dependency>

1. 类似Elasticsearch的方式,现在客户端构建一个集群对象:

Cluster cluster = Cluster.builder()
                .addContactPoint("your ip")
                .build();
        Metadata metadata = cluster.getMetadata();
        System.out.printf("Connected to cluster: %s\n",
                metadata.getClusterName());
        for (Host host : metadata.getAllHosts()) {
            System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
                    host.getDatacenter(), host.getAddress(), host.getRack());
        }

2. 通过一个Session对象,实现对Cassandra的所有增删改查。

Session session = cluster.connect();


3. 通过Session对象实现所有的DML等操作。 (PS:在对 Cassandra 进行操作之前,建议先了解 Cassandra 的架构以及数据组织形式)

a. 我们首先建立一个Schema:

<pre name="code" class="java">ResultSet results = session.execute("SELECT * FROM simplex.playlists ");

        System.out.println(String.format("%-30s\t%-20s\t%-20s\n%s", "title", "album", "artist",
                "-------------------------------+-----------------------+--------------------"));
        for (Row row : results) {
            System.out.println(String.format("%-30s\t%-20s\t%-20s", row.getString("title"),
                    row.getString("album"), row.getString("artist")));
        }
        System.out.println();

session.execute("CREATE KEYSPACE simplex WITH replication " + "= {‘class‘:‘SimpleStrategy‘, ‘replication_factor‘:3};");


b.建立一个Table:

session.execute(
                "CREATE TABLE simplex.songs (" +
                        "id uuid PRIMARY KEY," +
                        "title text," +
                        "album text," +
                        "artist text," +
                        "tags set<text>," +
                        "data blob" +
                        ");");
c. 插入数据:

session.execute(
                "INSERT INTO simplex.songs (id, title, album, artist, tags) " +
                        "VALUES (" +
                        "756716f7-2e54-4715-9f00-91dcbea6cf50," +
                        "'La Petite Tonkinoise'," +
                        "'Bye Bye Blackbird'," +
                        "'Joséphine Baker'," +
                        "{'jazz', '2013'})" +
                        ";");
d. 查询数据:

ResultSet results = session.execute("SELECT * FROM simplex.playlists ");

        System.out.println(String.format("%-30s\t%-20s\t%-20s\n%s", "title", "album", "artist",
                "-------------------------------+-----------------------+--------------------"));
        for (Row row : results) {
            System.out.println(String.format("%-30s\t%-20s\t%-20s", row.getString("title"),
                    row.getString("album"), row.getString("artist")));
        }
        System.out.println();


Cassandra数据库Java访问

标签:style   blog   io   os   ar   使用   java   for   sp   

原文地址:http://blog.csdn.net/zhuyu_deng/article/details/40430713

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!