use of javax.cache.Cache in project ignite by apache.
the class IgniteModelDistributedInferenceExample method main.
/**
* Run example.
*/
public static void main(String... args) throws IOException, ExecutionException, InterruptedException {
System.out.println();
System.out.println(">>> Linear regression model over cache based dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.MORTALITY_DATA);
System.out.println(">>> Create new linear regression trainer object.");
LinearRegressionLSQRTrainer trainer = new LinearRegressionLSQRTrainer();
System.out.println(">>> Perform the training to get the model.");
LinearRegressionModel mdl = trainer.fit(ignite, dataCache, new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
System.out.println(">>> Linear regression model: " + mdl);
System.out.println(">>> Preparing model reader and model parser.");
ModelReader reader = new InMemoryModelReader(mdl);
ModelParser<Vector, Double, ?> parser = new IgniteModelParser<>();
try (Model<Vector, Future<Double>> infMdl = new IgniteDistributedModelBuilder(ignite, 4, 4).build(reader, parser)) {
System.out.println(">>> Inference model is ready.");
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Ground Truth\t|");
System.out.println(">>> ---------------------------------");
try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(new ScanQuery<>())) {
for (Cache.Entry<Integer, Vector> observation : observations) {
Vector val = observation.getValue();
Vector inputs = val.copyOfRange(1, val.size());
double groundTruth = val.get(0);
double prediction = infMdl.predict(inputs).get();
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
}
}
System.out.println(">>> ---------------------------------");
System.out.println(">>> Linear regression model over cache based dataset usage example completed.");
} finally {
if (dataCache != null)
dataCache.destroy();
}
} finally {
System.out.flush();
}
}
use of javax.cache.Cache in project ignite by apache.
the class EncryptedCacheExample method main.
/**
* @param args Command line arguments.
*/
public static void main(String[] args) {
System.out.println(">>> Starting cluster.");
// You can use encryption feature only for deployment with Ignite persistence enabled.
try (Ignite ignite = Ignition.start("examples/config/encryption/example-encrypted-store.xml")) {
// Activate the cluster. Required to do if the persistent store is enabled because you might need
// to wait while all the nodes, that store a subset of data on disk, join the cluster.
ignite.cluster().state(ClusterState.ACTIVE);
CacheConfiguration<Long, BankAccount> ccfg = new CacheConfiguration<>(CACHE_NAME);
// Enabling encryption for newly created cache.
ccfg.setEncryptionEnabled(true);
System.out.println(">>> Creating encrypted cache.");
IgniteCache<Long, BankAccount> cache = ignite.createCache(ccfg);
System.out.println(">>> Populating cache with data.");
// Data in this cache will be encrypted on the disk.
cache.put(1L, new BankAccount("Rich account", 1_000_000L));
cache.put(2L, new BankAccount("Middle account", 1_000L));
cache.put(3L, new BankAccount("One dollar account", 1L));
}
// After cluster shutdown data persisted on the disk in encrypted form.
System.out.println(">>> Starting cluster again.");
// Starting cluster again.
try (Ignite ignite = Ignition.start("examples/config/encryption/example-encrypted-store.xml")) {
ignite.cluster().state(ClusterState.ACTIVE);
// We can obtain existing cache and load data from disk.
IgniteCache<Long, BankAccount> cache = ignite.getOrCreateCache(CACHE_NAME);
QueryCursor<Cache.Entry<Long, BankAccount>> cursor = cache.query(new ScanQuery<>());
System.out.println(">>> Saved data:");
// Iterating through existing data.
for (Cache.Entry<Long, BankAccount> entry : cursor) {
System.out.println(">>> ID = " + entry.getKey() + ", AccountName = " + entry.getValue().accountName + ", Balance = " + entry.getValue().balance);
}
}
}
use of javax.cache.Cache in project ignite by apache.
the class JavaThinCompatibilityTest method testContinuousQueries.
/**
*/
private void testContinuousQueries() throws Exception {
X.println(">>>> Testing continuous queries");
try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(ADDR))) {
ClientCache<Object, Object> cache = client.getOrCreateCache("testContinuousQueries");
List<CacheEntryEvent<?, ?>> allEvts = new ArrayList<>();
cache.query(new ContinuousQuery<>().setLocalListener(evts -> evts.forEach(allEvts::add)));
cache.put(0, 0);
cache.put(0, 1);
cache.remove(0);
assertTrue(GridTestUtils.waitForCondition(() -> allEvts.size() == 3, 1_000L));
}
}
use of javax.cache.Cache in project ignite by apache.
the class UsingContinuousQueries method remoteFilterExample.
public static void remoteFilterExample() {
try (Ignite ignite = Ignition.start()) {
IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
// tag::remoteFilter[]
ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
qry.setLocalListener(events -> events.forEach(event -> System.out.format("Entry: key=[%s] value=[%s]\n", event.getKey(), event.getValue())));
qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {
@Override
public CacheEntryEventFilter<Integer, String> create() {
return new CacheEntryEventFilter<Integer, String>() {
@Override
public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) {
System.out.format("the value for key [%s] was updated from [%s] to [%s]\n", e.getKey(), e.getOldValue(), e.getValue());
return true;
}
};
}
});
// end::remoteFilter[]
cache.query(qry);
cache.put(1, "1");
}
}
use of javax.cache.Cache in project ignite by apache.
the class CacheContinuousQueryExample 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 example started.");
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
int keyCnt = 20;
// These entries will be queried by initial predicate.
for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
// Create new continuous query.
ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Integer, String>() {
@Override
public boolean apply(Integer key, String val) {
return key > 10;
}
}));
// Callback that is called locally when update notifications are received.
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends String> e : evts) System.out.println("Updated entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
}
});
// This filter will be evaluated remotely on all nodes.
// Entry that pass this filter will be sent to the caller.
qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {
@Override
public CacheEntryEventFilter<Integer, String> create() {
return new CacheEntryEventFilter<Integer, String>() {
@Override
public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) {
return e.getKey() > 10;
}
};
}
});
// Execute query.
try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
// Iterate through existing data.
for (Cache.Entry<Integer, String> e : cur) System.out.println("Queried existing entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
// Add a few more keys and watch more query notifications.
for (int i = keyCnt; i < keyCnt + 10; i++) cache.put(i, Integer.toString(i));
// 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);
}
}
}
Aggregations