Cassandra資料庫Java訪問

來源:互聯網
上載者:User

標籤: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訪問

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.