using multiple logical operations like OR,AND,NOT in Aerospike

人走茶凉 提交于 2020-12-12 11:32:46

问题


I have data as below :

+--------+-----------+---------+---------+
| COMPANY| COLOR     | OWNER   | MODEL   |
+--------+-----------+---------+---------+
|Benz    | Red       | p1      | ABC     |
+--------+-----------+---------+---------+
|BMW     | Blue      | P2      | XYZ     |
+--------+-----------+---------+---------+
|Ferrari | YelloW    | P3      | PQR     |
+--------+-----------+---------+---------+
|Audi    | Blue      + P4      | MNO     |
------------------------------------------

Now I want the records where either the company is Benz or Color is Blue or Owner is P2. I have gone through Aerospike Documentation but could not find any way by which we can perform such operations in a single query. I want to simulate this SQL query in aerospike with C# client:

select * from tablename where (company = Benz or Color = Blue or Owner = P1)

回答1:


You can use Expressions in Aerospike to do just that. Java example below, more available in Aerospike Java Client Library examples (public repo). This is from QueryExp.java. (For C# client see: QueryExp.cs in the Csharp client code / Framework/AerospikeDemo)

private void runQuery1(
    AerospikeClient client,
    Parameters params,
    String binName
) throws Exception {

    int begin = 10;
    int end = 40;

    console.info("Query Predicate: (bin2 > 126 && bin2 <= 140) || (bin2 = 360)");

    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);

    // Filter applied on query itself.  Filter can only reference an indexed bin.
    stmt.setFilter(Filter.range(binName, begin, end));

    // Predicates are applied on query results on server side.
    // Predicates can reference any bin.
    QueryPolicy policy = new QueryPolicy(client.queryPolicyDefault);
    policy.filterExp = Exp.build(
        Exp.or(
            Exp.and(
                Exp.gt(Exp.intBin("bin2"), Exp.val(126)),
                Exp.le(Exp.intBin("bin2"), Exp.val(140))),
            Exp.eq(Exp.intBin("bin2"), Exp.val(360))));

    RecordSet rs = client.query(policy, stmt);

    try {
        while (rs.next()) {
            Record record = rs.getRecord();
            console.info("Record: " + record.toString());
        }
    }
    finally {
        rs.close();
    }
}


来源:https://stackoverflow.com/questions/64953854/using-multiple-logical-operations-like-or-and-not-in-aerospike

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!