use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class TcpDiscoveryMultiThreadedTest method _testCustomEventOnJoinCoordinatorStop.
/**
* @throws Exception If failed.
*/
public void _testCustomEventOnJoinCoordinatorStop() throws Exception {
for (int k = 0; k < 10; k++) {
log.info("Iteration: " + k);
clientFlagGlobal = false;
final int START_NODES = 5;
final int JOIN_NODES = 5;
startGrids(START_NODES);
final AtomicInteger startIdx = new AtomicInteger(START_NODES);
final AtomicBoolean stop = new AtomicBoolean();
IgniteInternalFuture<?> fut1 = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
Ignite ignite = ignite(START_NODES - 1);
while (!stop.get()) {
ignite.createCache(ccfg);
ignite.destroyCache(ccfg.getName());
}
return null;
}
});
try {
final CyclicBarrier barrier = new CyclicBarrier(JOIN_NODES + 1);
IgniteInternalFuture<?> fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
int idx = startIdx.getAndIncrement();
Thread.currentThread().setName("start-thread-" + idx);
barrier.await();
Ignite ignite = startGrid(idx);
assertFalse(ignite.configuration().isClientMode());
log.info("Started node: " + ignite.name());
IgniteCache<Object, Object> cache = ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
// No-op.
}
});
QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry);
cur.close();
return null;
}
}, JOIN_NODES, "start-thread");
barrier.await();
U.sleep(ThreadLocalRandom.current().nextInt(10, 100));
for (int i = 0; i < START_NODES - 1; i++) {
GridTestUtils.invoke(ignite(i).configuration().getDiscoverySpi(), "simulateNodeFailure");
stopGrid(i);
}
stop.set(true);
fut1.get();
fut2.get();
} finally {
stop.set(true);
fut1.get();
}
stopAllGrids();
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryWithTransformerExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws Exception If example execution failed.
*/
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Cache continuous query with transformer example started.");
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, Organization> cache = ignite.getOrCreateCache(CACHE_NAME).withKeepBinary()) {
// Create new continuous query with transformer.
ContinuousQueryWithTransformer<Integer, Organization, String> qry = new ContinuousQueryWithTransformer<>();
// Factory to create transformers.
qry.setRemoteTransformerFactory(() -> {
// Only this field will be sent over to a local node over the network.
return event -> ((BinaryObject) event.getValue()).field("name");
});
// Listener that will receive transformed data.
qry.setLocalListener(names -> {
for (String name : names) System.out.println("New organization name: " + name);
});
// Execute query.
try (QueryCursor<Cache.Entry<Integer, Organization>> cur = cache.query(qry)) {
populateCache(cache);
// Wait for a while while callback is notified about remaining puts.
Thread.sleep(2000);
}
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache(CACHE_NAME);
}
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class UsingContinuousQueries method initialQueryExample.
public static void initialQueryExample() {
try (Ignite ignite = Ignition.start()) {
// tag::initialQry[]
IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
// end::initialQry[]
cache.put(100, "100");
// tag::initialQry[]
ContinuousQuery<Integer, String> query = new ContinuousQuery<>();
// Setting an optional initial query.
// The query will return entries for the keys greater than 10.
query.setInitialQuery(new ScanQuery<>((k, v) -> k > 10));
// mandatory local listener
query.setLocalListener(events -> {
});
try (QueryCursor<Cache.Entry<Integer, String>> cursor = cache.query(query)) {
// Iterating over the entries returned by the initial query
for (Cache.Entry<Integer, String> e : cursor) System.out.println("key=" + e.getKey() + ", val=" + e.getValue());
}
// end::initialQry[]
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class UsingScanQueries method executingScanQueriesExample.
@Test
void executingScanQueriesExample() {
try (Ignite ignite = Ignition.start()) {
// tag::scanQry[]
// tag::predicate[]
// tag::transformer[]
IgniteCache<Integer, Person> cache = ignite.getOrCreateCache("myCache");
// end::scanQry[]
// end::predicate[]
// end::transformer[]
Person person = new Person(1, "Vasya Ivanov");
person.setSalary(2000);
cache.put(1, person);
// tag::scanQry[]
QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new ScanQuery<>());
// end::scanQry[]
System.out.println("Scan query output:" + cursor.getAll().get(0).getValue().getName());
// tag::predicate[]
// Find the persons who earn more than 1,000.
IgniteBiPredicate<Integer, Person> filter = (key, p) -> p.getSalary() > 1000;
try (QueryCursor<Cache.Entry<Integer, Person>> qryCursor = cache.query(new ScanQuery<>(filter))) {
qryCursor.forEach(entry -> System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()));
}
// end::predicate[]
// tag::transformer[]
// Get only keys for persons earning more than 1,000.
List<Integer> keys = cache.query(new ScanQuery<>(// Remote filter
(IgniteBiPredicate<Integer, Person>) (k, p) -> p.getSalary() > 1000), // Transformer
(IgniteClosure<Cache.Entry<Integer, Person>, Integer>) Cache.Entry::getKey).getAll();
// end::transformer[]
System.out.println("Transformer example output:" + keys.get(0));
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class UsingScanQueries method executingIndexQueriesExample.
@Test
void executingIndexQueriesExample() {
try (Ignite ignite = Ignition.start()) {
// tag::idxQry[]
// Create index by 2 fields (orgId, salary).
QueryEntity personEntity = new QueryEntity(Integer.class, Person.class).setFields(new LinkedHashMap<String, String>() {
{
put("orgId", Integer.class.getName());
put("salary", Integer.class.getName());
}
}).setIndexes(Collections.singletonList(new QueryIndex(Arrays.asList("orgId", "salary"), QueryIndexType.SORTED).setName("ORG_SALARY_IDX")));
CacheConfiguration<Integer, Person> ccfg = new CacheConfiguration<Integer, Person>("entityCache").setQueryEntities(Collections.singletonList(personEntity));
IgniteCache<Integer, Person> cache = ignite.getOrCreateCache(ccfg);
// end::idxQry[]
{
// tag::idxQry[]
// Find the persons who work in Organization 1.
QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new IndexQuery<Integer, Person>(Person.class, "ORG_SALARY_IDX").setCriteria(eq("orgId", 1)));
// end::idxQry[]
}
{
// tag::idxQryMultipleCriteria[]
// Find the persons who work in Organization 1 and have salary more than 1,000.
QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new IndexQuery<Integer, Person>(Person.class, "ORG_SALARY_IDX").setCriteria(eq("orgId", 1), gt("salary", 1000)));
// end::idxQryMultipleCriteria[]
}
{
// tag::idxQryNoIdxName[]
// Ignite finds suitable index "ORG_SALARY_IDX" by specified criterion field "orgId".
QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new IndexQuery<Integer, Person>(Person.class).setCriteria(eq("orgId", 1)));
// end::idxQryNoIdxName[]
}
{
// tag::idxQryFilter[]
// Find the persons who work in Organization 1 and whose name contains 'Vasya'.
QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new IndexQuery<Integer, Person>(Person.class).setCriteria(eq("orgId", 1)).setFilter((k, v) -> v.getName().contains("Vasya")));
// end::idxQryFilter[]
}
}
}
Aggregations