Search in sources :

Example 96 with Cache

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();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) IgniteModelParser(org.apache.ignite.ml.inference.parser.IgniteModelParser) DummyVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer) InMemoryModelReader(org.apache.ignite.ml.inference.reader.InMemoryModelReader) LinearRegressionLSQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer) InMemoryModelReader(org.apache.ignite.ml.inference.reader.InMemoryModelReader) ModelReader(org.apache.ignite.ml.inference.reader.ModelReader) Future(java.util.concurrent.Future) Ignite(org.apache.ignite.Ignite) IgniteDistributedModelBuilder(org.apache.ignite.ml.inference.builder.IgniteDistributedModelBuilder) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) IgniteCache(org.apache.ignite.IgniteCache) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) Cache(javax.cache.Cache)

Example 97 with Cache

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);
        }
    }
}
Also used : Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 98 with Cache

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));
    }
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) BinaryObject(org.apache.ignite.binary.BinaryObject) ClientFeatureNotSupportedByServerException(org.apache.ignite.client.ClientFeatureNotSupportedByServerException) ClientCache(org.apache.ignite.client.ClientCache) ServiceContext(org.apache.ignite.services.ServiceContext) Map(java.util.Map) X(org.apache.ignite.internal.util.typedef.X) Cache(javax.cache.Cache) IgniteBinary(org.apache.ignite.IgniteBinary) QueryEntity(org.apache.ignite.cache.QueryEntity) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) Parameterized(org.junit.runners.Parameterized) SERVICE_INVOKE_CALLCTX(org.apache.ignite.internal.client.thin.ProtocolBitmaskFeature.SERVICE_INVOKE_CALLCTX) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) GET_SERVICE_DESCRIPTORS(org.apache.ignite.internal.client.thin.ProtocolBitmaskFeature.GET_SERVICE_DESCRIPTORS) IgniteException(org.apache.ignite.IgniteException) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) IgniteProductVersion(org.apache.ignite.lang.IgniteProductVersion) NotNull(org.jetbrains.annotations.NotNull) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Person(org.apache.ignite.client.Person) ComputeJob(org.apache.ignite.compute.ComputeJob) ComputeTaskAdapter(org.apache.ignite.compute.ComputeTaskAdapter) RunWith(org.junit.runner.RunWith) PlatformType(org.apache.ignite.platform.PlatformType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult) ClientTransaction(org.apache.ignite.client.ClientTransaction) IgniteClient(org.apache.ignite.client.IgniteClient) PlatformExpiryPolicy(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy) Assume(org.junit.Assume) Duration(javax.cache.expiry.Duration) F(org.apache.ignite.internal.util.typedef.F) ServiceCallContext(org.apache.ignite.services.ServiceCallContext) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ClientServiceDescriptor(org.apache.ignite.client.ClientServiceDescriptor) TimeUnit(java.util.concurrent.TimeUnit) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignition(org.apache.ignite.Ignition) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) ServiceContextResource(org.apache.ignite.resources.ServiceContextResource) Service(org.apache.ignite.services.Service) CacheMode(org.apache.ignite.cache.CacheMode) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteClient(org.apache.ignite.client.IgniteClient) ArrayList(java.util.ArrayList) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration)

Example 99 with Cache

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");
    }
}
Also used : Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) Ignition(org.apache.ignite.Ignition) IgniteClosure(org.apache.ignite.lang.IgniteClosure) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter)

Example 100 with Cache

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);
        }
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Aggregations

Cache (javax.cache.Cache)271 IgniteCache (org.apache.ignite.IgniteCache)157 Test (org.junit.Test)130 Ignite (org.apache.ignite.Ignite)101 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 List (java.util.List)62 Map (java.util.Map)56 ScanQuery (org.apache.ignite.cache.query.ScanQuery)54 ArrayList (java.util.ArrayList)51 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)45 QueryCursor (org.apache.ignite.cache.query.QueryCursor)43 HashMap (java.util.HashMap)41 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)41 Collection (java.util.Collection)38 HashSet (java.util.HashSet)38 CacheManager (javax.cache.CacheManager)38 CacheException (javax.cache.CacheException)35 IgniteEx (org.apache.ignite.internal.IgniteEx)35 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)32