用Java编写基本的n1ql查询 [英] writing a basic n1ql query in java

查看:39
本文介绍了用Java编写基本的n1ql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 Couchbase .我正在尝试使用java sdk编写基本查询,但无法理解如何编写.下面是查询:

I have just started learning Couchbase. I am trying to write a basic query using java sdk but I am not able to understand how to write it. Below is the query:

 SELECT * 
        FROM users_with_orders usr 
                JOIN orders_with_users orders 
                    ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END

这是用于不使用数组的连接:

This is for joining without array:

LetPath path = select("*,META(usr).id as _ID,META(usr).cas as _CAS).from(bucketName +" usr").join(bucketname +" orders").onKeys("usr.order_id) 

我应该如何继续对键数组进行上述查询?

How should I proceed with the above query for on keys array?

谢谢!!!!

推荐答案

有关从SDK查询的文档,您可以在Java SDK中使用简单的字符串,也可以使用DSL.例如:

As described in the docs on Querying from the SDK, you can use either a simple string with the Java SDK or use the DSL. For example:

    // query with a simple string
    System.out.println("Simple string query:");
    N1qlQuery airlineQuery = N1qlQuery.simple("SELECT `travel-sample`.* FROM `travel-sample` WHERE name=\"United Airlines\" AND type=\"airline\"");
    N1qlQueryResult queryResult = bucket.query(airlineQuery);

    for (N1qlQueryRow result: queryResult) {
        System.out.println(result.value());
    }

    //query with a parameter using the DSL
    System.out.println("Parameterized query using the DSL:");
    Statement statement = select(path(i("travel-sample"), "*")).from(i("travel-sample")).where(x("name").eq(x("$airline_param")).and(x("type").eq(s("airline"))));
    JsonObject placeholderValues = JsonObject.create().put("airline_param", "United Airlines");
    N1qlQuery airlineQueryParameterized = N1qlQuery.parameterized(statement, placeholderValues);
    N1qlQueryResult queryResultParameterized = bucket.query(airlineQueryParameterized);

    for (N1qlQueryRow row : queryResultParameterized) {
        System.out.println(row);
    }

(我将此示例的完整内容贴在了进口商品上,等等)

(I posted a full gist of this example for the imports, etc.)

有关更多信息,请参阅文档,但是您可能希望使用DSL来允许IDE代码完成和Java编译时间检查.在开发交互式Web应用程序时,您可能还希望使用参数化的语句(出于安全性考虑),甚至可能需要准备好的语句(出于性能考虑).

See the docs for more info, but you may want to use the DSL to allow IDE code completion and Java compile time checking. When developing an interactive web application, you'll probably also want to use parameterized statements (for security) and may even want prepared statements (for performance).

这篇关于用Java编写基本的n1ql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆