Couchbase java

Couchbase client

Couchbase prepares several programming language support.

Java is also supported.

In this entry, I will explain simple usage with sample data provided by couchbase

build.gradle

compile group: 'com.couchbase.client', name: 'java-client', version: '2.7.11'

Couchbase data

I used travel-sample data prepared by couchbase

The data is like this.

{"travel-sample":{"callsign":"MILE-AIR","country":"United States","iata":"Q5","icao":"MLA","id":10,"name":"40-Mile Air","type":"airline"}}
{"travel-sample":{"callsign":"TXW","country":"United States","iata":"TQ","icao":"TXW","id":10123,"name":"Texas Wings","type":"airline"}}
{"travel-sample":{"callsign":"atifly","country":"United States","iata":"A1","icao":"A1F","id":10226,"name":"Atifly","type":"airline"}}
{"travel-sample":{"callsign":null,"country":"United Kingdom","iata":null,"icao":"JRB","id":10642,"name":"Jc royal.britannica","type":"airline"}}
{"travel-sample":{"callsign":"LOCAIR","country":"United States","iata":"ZQ","icao":"LOC","id":10748,"name":"Locair","type":"airline"}}
{"travel-sample":{"callsign":"SASQUATCH","country":"United States","iata":"K5","icao":"SQH","id":10765,"name":"SeaPort Airlines","type":"airline"}}
{"travel-sample":{"callsign":"ACE AIR","country":"United States","iata":"KO","icao":"AER","id":109,"name":"Alaska Central Express","type":"airline"}}
{"travel-sample":{"callsign":"FLYSTAR","country":"United Kingdom","iata":"5W","icao":"AEU","id":112,"name":"Astraeus","type":"airline"}}
{"travel-sample":{"callsign":"REUNION","country":"France","iata":"UU","icao":"REU","id":1191,"name":"Air Austral","type":"airline"}}
{"travel-sample":{"callsign":"AIRLINAIR","country":"France","iata":"A5","icao":"RLA","id":1203,"name":"Airlinair","type":"airline"}}

key is “travel-sample”

Client and N1QL

In this time, I use simple N1QL to extract data.

This is Sample codes

public class Main {

    public static void main(String... args) throws Exception {
        Cluster cluster = CouchbaseCluster.create("localhost");
        cluster.authenticate("username", "password");
        Bucket bucket = cluster.openBucket("travel-sample");

        // Perform a N1QL Query
        N1qlQueryResult result = bucket.query(N1qlQuery.simple("SELECT * FROM `travel-sample` LIMIT 10"));
        // Print each found Row
        for (N1qlQueryRow row : result) {
            System.out.println(row);
        }
    }
}

The point is cluster name(server host name),

And username and password is your setting.

Run simple N1qlQuery and extract data one by one. Very simple.

Data未分類
スポンサーリンク
Professional Programmer2

コメント