问题
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