Search in sources :

Example 11 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class CacheAffinityExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache affinity example started.");
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            for (int i = 0; i < KEY_CNT; i++) cache.put(i, Integer.toString(i));
            // Co-locates jobs with data using IgniteCompute.affinityRun(...) method.
            visitUsingAffinityRun();
            // Co-locates jobs with data using IgniteCluster.mapKeysToNodes(...) method.
            visitUsingMapKeysToNodes();
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : Ignite(org.apache.ignite.Ignite)

Example 12 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class ComputeFibonacciContinuationExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println("Compute Fibonacci continuation example started.");
        long N = 100;
        final UUID exampleNodeId = ignite.cluster().localNode().id();
        // Filter to exclude this node from execution.
        final IgnitePredicate<ClusterNode> nodeFilter = new IgnitePredicate<ClusterNode>() {

            @Override
            public boolean apply(ClusterNode n) {
                // Give preference to remote nodes.
                return ignite.cluster().forRemotes().nodes().isEmpty() || !n.id().equals(exampleNodeId);
            }
        };
        long start = System.currentTimeMillis();
        BigInteger fib = ignite.compute(ignite.cluster().forPredicate(nodeFilter)).apply(new ContinuationFibonacciClosure(nodeFilter), N);
        long duration = System.currentTimeMillis() - start;
        System.out.println();
        System.out.println(">>> Finished executing Fibonacci for '" + N + "' in " + duration + " ms.");
        System.out.println(">>> Fibonacci sequence for input number '" + N + "' is '" + fib + "'.");
        System.out.println(">>> If you re-run this example w/o stopping remote nodes - the performance will");
        System.out.println(">>> increase since intermediate results are pre-cache on remote nodes.");
        System.out.println(">>> You should see prints out every recursive Fibonacci execution on cluster nodes.");
        System.out.println(">>> Check remote nodes for output.");
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) BigInteger(java.math.BigInteger) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 13 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class ComputeTaskMapExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println("Compute task map example started.");
        // Execute task on the cluster and wait for its completion.
        int cnt = ignite.compute().execute(MapExampleCharacterCountTask.class, "Hello Ignite Enabled World!");
        System.out.println();
        System.out.println(">>> Total number of characters in the phrase is '" + cnt + "'.");
        System.out.println(">>> Check all nodes for output (this node is also part of the cluster).");
    }
}
Also used : Ignite(org.apache.ignite.Ignite)

Example 14 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class ComputeTaskSplitExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println("Compute task split example started.");
        // Execute task on the cluster and wait for its completion.
        int cnt = ignite.compute().execute(SplitExampleCharacterCountTask.class, "Hello Ignite Enabled World!");
        System.out.println();
        System.out.println(">>> Total number of characters in the phrase is '" + cnt + "'.");
        System.out.println(">>> Check all nodes for output (this node is also part of the cluster).");
    }
}
Also used : Ignite(org.apache.ignite.Ignite)

Example 15 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class IgniteAtomicReferenceExample 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(">>> Atomic reference example started.");
        // Make name of atomic reference.
        final String refName = UUID.randomUUID().toString();
        // Make value of atomic reference.
        String val = UUID.randomUUID().toString();
        // Initialize atomic reference.
        IgniteAtomicReference<String> ref = ignite.atomicReference(refName, val, true);
        System.out.println("Atomic reference initial value : " + ref.get() + '.');
        // Make closure for checking atomic reference value on cluster.
        IgniteRunnable c = new ReferenceClosure(refName);
        // Check atomic reference on all cluster nodes.
        ignite.compute().run(c);
        // Make new value of atomic reference.
        String newVal = UUID.randomUUID().toString();
        System.out.println("Try to change value of atomic reference with wrong expected value.");
        // Won't change.
        ref.compareAndSet("WRONG EXPECTED VALUE", newVal);
        // Check atomic reference on all cluster nodes.
        // Atomic reference value shouldn't be changed.
        ignite.compute().run(c);
        System.out.println("Try to change value of atomic reference with correct expected value.");
        ref.compareAndSet(val, newVal);
        // Check atomic reference on all cluster nodes.
        // Atomic reference value should be changed.
        ignite.compute().run(c);
    }
    System.out.println();
    System.out.println("Finished atomic reference example...");
    System.out.println("Check all nodes for output (this node is also part of the cluster).");
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Aggregations

Ignite (org.apache.ignite.Ignite)1560 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)275 CountDownLatch (java.util.concurrent.CountDownLatch)188 IgniteException (org.apache.ignite.IgniteException)187 Transaction (org.apache.ignite.transactions.Transaction)166 IgniteCache (org.apache.ignite.IgniteCache)161 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)161 ArrayList (java.util.ArrayList)135 ClusterNode (org.apache.ignite.cluster.ClusterNode)121 UUID (java.util.UUID)104 Event (org.apache.ignite.events.Event)104 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)98 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)97 CacheException (javax.cache.CacheException)94 HashMap (java.util.HashMap)78 IgniteKernal (org.apache.ignite.internal.IgniteKernal)71 Map (java.util.Map)61 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)61 Callable (java.util.concurrent.Callable)60 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)60