OR, AND, NOT Aerospike와 같은 여러 논리 연산 사용
Nov 22 2020
다음과 같은 데이터가 있습니다.
+--------+-----------+---------+---------+
| COMPANY| COLOR | OWNER | MODEL |
+--------+-----------+---------+---------+
|Benz | Red | p1 | ABC |
+--------+-----------+---------+---------+
|BMW | Blue | P2 | XYZ |
+--------+-----------+---------+---------+
|Ferrari | YelloW | P3 | PQR |
+--------+-----------+---------+---------+
|Audi | Blue + P4 | MNO |
------------------------------------------
이제 회사가 Benz이거나 Color가 Blue이거나 Owner가 P2 인 레코드를 원합니다. Aerospike 문서를 살펴 보았지만 단일 쿼리에서 이러한 작업을 수행 할 수있는 방법을 찾지 못했습니다. C # 클라이언트를 사용하여 aerospike에서이 SQL 쿼리를 시뮬레이션하고 싶습니다.
select * from tablename where (company = Benz or Color = Blue or Owner = P1)
답변
1 pgupta Nov 23 2020 at 22:36
Aerospike에서 Expressions를 사용하여이를 수행 할 수 있습니다. 아래의 Java 예제는 Aerospike Java 클라이언트 라이브러리 예제 (공용 저장소)에서 더 많이 사용할 수 있습니다. 이것은 QueryExp.java에서 가져온 것입니다. (C # 클라이언트의 경우 Csharp 클라이언트 코드 / Framework / AerospikeDemo의 QueryExp.cs 참조)
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();
}
}