Java Code Examples for com.couchbase.client.java.query.N1qlQueryResult

The following examples show how to use com.couchbase.client.java.query.N1qlQueryResult. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenSelectStatementWithDSL2_whenQuery_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    Statement st2 = select(
            x("t.city, t.airportname")
                    .concat(s(" (")).concat(x("t.faa")).concat(s(")")).as("portname_faa"))
            .from(i("travel-sample").as("t"))
            .where( x("t.type").eq(s("airport"))
                    .and(x("t.country").like(s("%States")))
                    .and(x("t.geo.lat").gte(70)))
            .limit(2);
    N1qlQueryResult r5 = bucket.query(N1qlQuery.simple(st2));
    List<JsonNode> list5 = extractJsonResult(r5);
    System.out.println("First Doc : " + list5.get(0));
    System.out.println("Query from Statement2: " + st2.toString());
}
 
Example 2
Source Project: nosql4idea   Source File: CouchbaseClient.java    License: Apache License 2.0 5 votes vote down vote up
public CouchbaseResult loadRecords(ServerConfiguration configuration, CouchbaseDatabase database, CouchbaseQuery couchbaseQuery) {
        Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment
                .builder()
                .queryEnabled(true)
                .build(),
                configuration.getServerUrl());
//        AuthenticationSettings authenticationSettings = configuration.getAuthenticationSettings();
//        ClusterManager clusterManager = cluster.clusterManager(authenticationSettings.getUsername(), authenticationSettings.getPassword());

        Bucket beerBucket = cluster.openBucket(database.getName(), 10, TimeUnit.SECONDS);
        N1qlQueryResult queryResult = beerBucket.query(N1qlQuery.simple(select("*").from(i(database.getName())).limit(couchbaseQuery.getLimit())));

//TODO dirty zone :(
        CouchbaseResult result = new CouchbaseResult(database.getName());
        List<JsonObject> errors = queryResult.errors();
        if (!errors.isEmpty()) {
            cluster.disconnect();
            result.addErrors(errors);
            return result;
        }

        for (N1qlQueryRow row : queryResult.allRows()) {
            result.add(row.value());
        }
        cluster.disconnect();
        return result;
    }
 
Example 3
Source Project: nosql4idea   Source File: CouchbaseClientTest.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment
            .builder()
            .queryEnabled(true)
            .build());

    Bucket defaultBucket = cluster.openBucket("default");
    defaultBucket.remove("user:walter");

    JsonArray friends = JsonArray.empty()
            .add(JsonObject.empty().put("name", "Mike Ehrmantraut"))
            .add(JsonObject.empty().put("name", "Jesse Pinkman"));

    JsonObject content = JsonObject.empty()
            .put("firstname", "Walter")
            .put("lastname", "White")
            .put("age", 52)
            .put("aliases", JsonArray.from("Walt Jackson", "Mr. Mayhew", "David Lynn"))
            .put("friends", friends);
    JsonDocument walter = JsonDocument.create("user:walter", content);
    JsonDocument inserted = defaultBucket.insert(walter);

    JsonDocument foundGuy = defaultBucket.get("user:walter");
    System.out.println(foundGuy.content().toMap());


    Bucket beerBucket = cluster.openBucket("beer-sample");
    N1qlQueryResult result = beerBucket.query(N1qlQuery.simple(select("*").from(i("beer-sample")).limit(10)));

    System.out.println("Errors found: " + result.errors());

    for (N1qlQueryRow row : result.allRows()) {
        JsonObject jsonObject = row.value();
        System.out.println(jsonObject.toMap());
    }

    cluster.disconnect();
}
 
Example 4
Source Project: nifi   Source File: CouchbaseMapCacheClient.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public long removeByPattern(String regex) throws IOException {
    Statement statement = Delete.deleteFromCurrentBucket().where(regexpContains("meta().id", regex));
    final N1qlQueryResult result = bucket.query(N1qlQuery.simple(statement));
    if (logger.isDebugEnabled()) {
        logger.debug("Deleted documents using regex {}, result={}", regex, result);
    }
    return result.info().mutationCount();
}
 
Example 5
Source Project: tutorials   Source File: CodeSnippets.java    License: MIT License 5 votes vote down vote up
public static List<JsonNode> extractJsonResult(N1qlQueryResult result) {
  return result.allRows().stream()
    .map(row -> {
        try {
          return objectMapper.readTree(row.value().toString());
        }catch (IOException e) {
           logger.log(Level.WARNING, e.getLocalizedMessage());
           return null;
        }
    })
    .filter(Objects::nonNull)
    .collect(Collectors.toList());
}
 
Example 6
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void whenBasicSelectQuery_thenGetQueryResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    N1qlQueryResult result
            = bucket.query(N1qlQuery.simple("SELECT * FROM test"));

    result.forEach(System.out::println);

    System.out.println("result count: " + result.info().resultCount());
    System.out.println("error count: " + result.info().errorCount());
}
 
Example 7
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenSelectStatement_whenQuery_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    String query = "SELECT name FROM `travel-sample` " +
            "WHERE type = 'airport' LIMIT 100";
    N1qlQueryResult result1 = bucket.query(N1qlQuery.simple(query));

    System.out.println("Result Count " + result1.info().resultCount());

    N1qlQueryRow row = result1.allRows().get(0);
    JsonObject rowJson = row.value();
    System.out.println("Name in First Row " + rowJson.get("name"));

}
 
Example 8
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenSelectStatement2_whenQuery_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    JsonObject pVal = JsonObject.create().put("type", "airport");
    String query = "SELECT * FROM `travel-sample` " +
            "WHERE type = $type LIMIT 100";
    N1qlQueryResult r2 = bucket.query(N1qlQuery.parameterized(query, pVal));

    System.out.println(r2.allRows());

    List<JsonNode> list = extractJsonResult(r2);
    System.out.println(
            list.get(0).get("travel-sample").get("airportname").asText());
}
 
Example 9
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenSelectDSL_whenQuery_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    Statement statement = select("*")
            .from(i("travel-sample"))
            .where(x("type").eq(s("airport")))
            .limit(100);
    N1qlQueryResult r3 = bucket.query(N1qlQuery.simple(statement));

    List<JsonNode> list2 = extractJsonResult(r3);
    System.out.println("First Airport Name: " + list2.get(0).get("travel-sample").get("airportname").asText());

}
 
Example 10
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenSelectStatementWithOperators_whenQuery_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    String query2 = "SELECT t.city, " +
            "t.airportname || \" (\" || t.faa || \")\" AS portname_faa " +
            "FROM `travel-sample` t " +
            "WHERE t.type=\"airport\"" +
            "AND t.country LIKE '%States'" +
            "AND t.geo.lat >= 70 " +
            "LIMIT 2";
    N1qlQueryResult r4 = bucket.query(N1qlQuery.simple(query2));
    List<JsonNode> list3 = extractJsonResult(r4);
    System.out.println("First Doc : " + list3.get(0));
}
 
Example 11
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenInsertStatement_whenQuery_thenUpdate() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    String query = "INSERT INTO `travel-sample` (KEY, VALUE) " +
            " VALUES(" +
            "\"cust1293\", " +
            "{\"id\":\"1293\",\"name\":\"Sample Airline\", \"type\":\"airline\"})" +
            " RETURNING META().id as docid, *";
    N1qlQueryResult r1 = bucket.query(N1qlQuery.simple(query));
    r1.forEach(System.out::println);
}
 
Example 12
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenUpdateStatement_whenQuery_thenUpdate() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    String query2 = "UPDATE `travel-sample` USE KEYS \"cust_1\" " +
            "SET name=\"Sample Airline Updated\" RETURNING name";
    N1qlQueryResult result = bucket.query(N1qlQuery.simple(query2));
    result.forEach(System.out::println);
}
 
Example 13
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenUnestUpdateStatement_whenQuery_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    String query3 = "UPDATE `travel-sample` USE KEYS \"cust_2\" " +
            "UNSET name RETURNING *";
    N1qlQueryResult result1 = bucket.query(N1qlQuery.simple(query3));
    result1.forEach(System.out::println);
}
 
Example 14
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 4 votes vote down vote up
@Test
public void givenDeleteStatement_whenQuery_thenDelete() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    String query4 = "DELETE FROM `travel-sample` USE KEYS \"cust_50\"";
    N1qlQueryResult result4 = bucket.query(N1qlQuery.simple(query4));
}
 
Example 15
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 4 votes vote down vote up
@Test
public void givenDeleteStatement2_whenQuery_thenDelete() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    String query5 = "DELETE FROM `travel-sample` WHERE id = 0 RETURNING *";
    N1qlQueryResult result5 = bucket.query(N1qlQuery.simple(query5));
}