Nasıl Çalışır
Açıklaması şöyle. Yani Predicate tüm üyelere gönderilir, sonuçları ise istekte bulunan istemci birleştirir
The requested predicate is sent to each member in the cluster. Each member looks at its own local entries and filters them according to the predicate. At this stage, key/value pairs of the entries are deserialized and then passed to the predicate. The predicate requester merges all the results coming from each member into a single set.
Sonuçları QueryableEntry#extractAttributeValue() metodu alır.
Eğer aranılan attribute string "list[0].attribute1" gibi karmaşık bir şeyse Extractors sınıfı ile getter bulunur.
Extractor sınıfı ReflectionHelper sınıfını kullanarak belirtilen attribute string'ini "." karakterine göre ayırır ve en alttaki getter metodu bulur
Nasıl Kullanılır
Predicate nesneleri şu metodlar ile kullanılır
IMap.entrySet(Predicate)
IMap.values(Predicate)
equals metodu
com.hazelcast.query.impl.predicates.EqualPredicate döndürür
Örnek
Şöyle yaparız
class TestData implements Serializable {
private static final long serialVersionUID = 1L;
private String attr1;
private String attr2;
...
}
IMap<String, TestData> map = instance1.getMap(...);
map.executeOnEntries(new TestLoggingEntryProcessor(), Predicates.equal("attr1", "foo"));
Örnek - JSON
Şöyle yaparız
Collection<HazelcastJsonValue> departmentWithMatchSalary = departments.values(
Predicates.equal("people[any].salary", 50000));
in metodu
com.hazelcast.query.impl.predicates.InPredicate döndürür
Örnek
Şöyle yaparız
IMap<String, String> map = ...
// Add some entries to the map
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
// Create a predicate to search for entries with specific keys
Predicate<String, String> keyPredicate = Predicates.in("key1", "key2");
// Get the entries from the map that match the predicate
Collection<String> values = map.values(keyPredicate);
sql metodu
SQL'in çoğu şeyi destekleniyor
Predicates.sql() metodu veya new SqlPredicate() nesnesi kullanılabilir. SqlPredicate Sınıfını kullacaksak şu satırı dahil ederiz
import com.hazelcast.query.impl.predicates.SqlPredicate;
Örnek - Field Value
Şöyle yaparız
IMap<Integer, Employee> employees = ...;Predicate sqlPredicate = new SqlPredicate( String.format("lastName = '%s'",emp.getLastName())); Collection<Employee> matching = employees.values(sqlPredicate); // wildcards are supported, too - look for last names starting // with 'D' using the same 'values' call. sqlPredicate = new SqlPredicate("lastName like 'D%'"); matching = employees.values(sqlPredicate); // compound predicates work, as well sqlPredicate = new SqlPredicate("lastName like 'D%' and age between 21 and 30"); matching = employees.values(sqlPredicate); // this could go on, but it's a pretty robust subset of sql functionality. sqlPredicate = new SqlPredicate("deptId not in (13, 23, 33)"); matching = employees.values(sqlPredicate);
Örnek - Enum Value
Elimizde şöyle bir kod olsun
public enum MyState { STATE1 { @Override public boolean isTest() { return false; } }, STATE2 { @Override public boolean isTest() { return true; } }; public abstract boolean isTest(); } public static class MyEmployee implements Serializable { MyState state; public MyEmployee(MyState state) { this.state = state; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MyEmployee that = (MyEmployee) o; return state == that.state; } @Override public int hashCode() { return Objects.hash(state); } }
Şöyle yaparız
HazelcastInstance instance = ...; IMap<Integer, MyEmployee> map = instance.getMap("map"); MyEmployee employee1 = new MyEmployee(State.STATE1); map.put(1, employee1); MyEmployee employee2 = new MyEmployee(State.STATE2); map.put(2, employee2); Collection<MyEmployee> values = map.values(Predicates.sql("state.test = true")); assertThat(values).containsExactly(employee2);
Örnek
Şöyle yaparız
import com.hazelcast.query.Predicates; IMap<String, Employee> map = instance.getMap("employees"); map.addIndex(IndexType.SORTED, "age"); map.addIndex(IndexType.HASH, "active"); Set<Map.Entry<String, Employee>> entries = map .entrySet(Predicates.sql("active=true and age>44"));
Hiç yorum yok:
Yorum Gönder