use of org.apache.ignite.cache.affinity.AffinityUuid in project ignite by apache.
the class CacheConfig method wordCache.
/**
* Configure streaming cache.
*/
public static CacheConfiguration<AffinityUuid, String> wordCache() {
CacheConfiguration<AffinityUuid, String> cfg = new CacheConfiguration<>("words");
// Index all words streamed into cache.
cfg.setIndexedTypes(AffinityUuid.class, String.class);
// Sliding window of 1 seconds.
cfg.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new CreatedExpiryPolicy(new Duration(SECONDS, 1))));
return cfg;
}
use of org.apache.ignite.cache.affinity.AffinityUuid in project ignite by apache.
the class IgniteCacheCollocatedQuerySelfTest method testColocatedQueryRight.
/**
* Correct affinity.
*/
public void testColocatedQueryRight() {
IgniteCache<AffinityUuid, Purchase> c = ignite(0).cache(DEFAULT_CACHE_NAME);
Random rnd = new GridRandom(SEED);
for (int i = 0; i < PURCHASES; i++) {
Purchase p = new Purchase();
p.productId = rnd.nextInt(PRODUCTS);
p.price = rnd.nextInt(MAX_PRICE);
// Correct affinity.
c.put(new AffinityUuid(p.productId), p);
}
List<List<?>> res1 = query(c, false);
List<List<?>> res2 = query(c, true);
X.println("res1: " + res1);
X.println("res2: " + res2);
assertFalse(res1.isEmpty());
// TODO fix type conversion issue
assertEquals(res1.toString(), res2.toString());
}
use of org.apache.ignite.cache.affinity.AffinityUuid in project ignite by apache.
the class QueryWords method main.
/**
* Schedules words query execution.
*
* @param args Command line arguments (none required).
* @throws Exception If failed.
*/
public static void main(String[] args) throws Exception {
// Mark this cluster member as client.
Ignition.setClientMode(true);
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
if (!ExamplesUtils.hasServerNodes(ignite))
return;
CacheConfiguration<AffinityUuid, String> cfg = CacheConfig.wordCache();
// The cache is configured with sliding window holding 1 second of the streaming data.
try (IgniteCache<AffinityUuid, String> stmCache = ignite.getOrCreateCache(cfg)) {
// Select top 10 words.
SqlFieldsQuery top10Qry = new SqlFieldsQuery("select _val, count(_val) as cnt from String group by _val order by cnt desc limit 10", true);
// Select average, min, and max counts among all the words.
SqlFieldsQuery statsQry = new SqlFieldsQuery("select avg(cnt), min(cnt), max(cnt) from (select count(_val) as cnt from String group by _val)");
// Query top 10 popular numbers every 5 seconds.
while (true) {
// Execute queries.
List<List<?>> top10 = stmCache.query(top10Qry).getAll();
List<List<?>> stats = stmCache.query(statsQry).getAll();
// Print average count.
List<?> row = stats.get(0);
if (row.get(0) != null)
System.out.printf("Query results [avg=%d, min=%d, max=%d]%n", row.get(0), row.get(1), row.get(2));
// Print top 10 words.
ExamplesUtils.printQueryResults(top10);
Thread.sleep(5000);
}
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache(cfg.getName());
}
}
}
use of org.apache.ignite.cache.affinity.AffinityUuid in project ignite by apache.
the class WordsSocketStreamerServer method main.
/**
* Starts socket streaming server.
*
* @param args Command line arguments (none required).
* @throws Exception If failed.
*/
public static void main(String[] args) throws Exception {
// Mark this cluster member as client.
Ignition.setClientMode(true);
CacheConfiguration<AffinityUuid, String> cfg = CacheConfig.wordCache();
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
if (!ExamplesUtils.hasServerNodes(ignite))
return;
// The cache is configured with sliding window holding 1 second of the streaming data.
try (IgniteCache<AffinityUuid, String> stmCache = ignite.getOrCreateCache(cfg)) {
IgniteDataStreamer<AffinityUuid, String> stmr = ignite.dataStreamer(stmCache.getName());
InetAddress addr = InetAddress.getLocalHost();
// Configure socket streamer
SocketStreamer<String, AffinityUuid, String> sockStmr = new SocketStreamer<>();
sockStmr.setAddr(addr);
sockStmr.setPort(PORT);
sockStmr.setDelimiter(DELIM);
sockStmr.setIgnite(ignite);
sockStmr.setStreamer(stmr);
// Converter from zero-terminated string to Java strings.
sockStmr.setConverter(new SocketMessageConverter<String>() {
@Override
public String convert(byte[] msg) {
try {
return new String(msg, "ASCII");
} catch (UnsupportedEncodingException e) {
throw new IgniteException(e);
}
}
});
sockStmr.setSingleTupleExtractor(new StreamSingleTupleExtractor<String, AffinityUuid, String>() {
@Override
public Map.Entry<AffinityUuid, String> extract(String word) {
// words are processed on the same cluster node.
return new IgniteBiTuple<>(new AffinityUuid(word), word);
}
});
sockStmr.start();
} catch (IgniteException e) {
System.err.println("Streaming server didn't start due to an error: ");
e.printStackTrace();
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache(cfg.getName());
}
}
}
use of org.apache.ignite.cache.affinity.AffinityUuid in project ignite by apache.
the class IgniteCacheCollocatedQuerySelfTest method testColocatedQueryWrong.
/**
* Correct affinity.
*/
public void testColocatedQueryWrong() {
IgniteCache<AffinityUuid, Purchase> c = ignite(0).cache(DEFAULT_CACHE_NAME);
Random rnd = new GridRandom(SEED);
for (int i = 0; i < PURCHASES; i++) {
Purchase p = new Purchase();
p.productId = rnd.nextInt(PRODUCTS);
p.price = rnd.nextInt(MAX_PRICE);
// Random affinity.
c.put(new AffinityUuid(rnd.nextInt(PRODUCTS)), p);
}
List<List<?>> res1 = query(c, false);
List<List<?>> res2 = query(c, true);
X.println("res1: " + res1);
X.println("res2: " + res2);
assertFalse(res1.isEmpty());
assertFalse(res1.equals(res2));
}
Aggregations