http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/Credit.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/Credit.java b/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/Credit.java deleted file mode 100644 index 81ebbee..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/Credit.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.computegrid.montecarlo; - -import java.io.*; - -/** - * This class provides a simple model for a credit contract (or a loan). It is basically - * defines as remaining crediting amount to date, credit remaining term, APR and annual - * probability on default. Although this model is simplified for the purpose - * of this example, it is close enough to emulate the real-life credit - * risk assessment application. - */ -public class Credit implements Serializable { - /** Remaining crediting amount. */ - private final double remAmnt; - - /** Remaining crediting remTerm. */ - private final int remTerm; - - /** Annual percentage rate (APR). */ - private final double apr; - - /** Expected annual probability of default (EaDF). */ - private final double edf; - - /** - * Creates new credit instance with given information. - * - * @param remAmnt Remained crediting amount. - * @param remTerm Remained crediting remTerm. - * @param apr Annual percentage rate (APR). - * @param edf Expected annual probability of default (EaDF). - */ - public Credit(double remAmnt, int remTerm, double apr, double edf) { - this.remAmnt = remAmnt; - this.remTerm = remTerm; - this.apr = apr; - this.edf = edf; - } - - /** - * Gets remained crediting amount. - * - * @return Remained amount of credit. - */ - double getRemainingAmount() { - return remAmnt; - } - - /** - * Gets remained crediting remTerm. - * - * @return Remained crediting remTerm in days. - */ - int getRemainingTerm() { - return remTerm; - } - - /** - * Gets annual percentage rate. - * - * @return Annual percentage rate in relative percents (percentage / 100). - */ - double getAnnualRate() { - return apr; - } - - /** - * Gets either credit probability of default for the given period of time - * if remaining term is less than crediting time or probability of default - * for whole remained crediting time. - * - * @param term Default term. - * @return Credit probability of default in relative percents - * (percentage / 100). - */ - double getDefaultProbability(int term) { - return 1 - Math.exp(Math.log(1 - edf) * Math.min(remTerm, term) / 365.0); - } - - /** {@inheritDoc} */ - @Override public String toString() { - StringBuilder buf = new StringBuilder(); - - buf.append(getClass().getName()); - buf.append(" [remAmnt=").append(remAmnt); - buf.append(", remTerm=").append(remTerm); - buf.append(", apr=").append(apr); - buf.append(", edf=").append(edf); - buf.append(']'); - - return buf.toString(); - } -}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/CreditRiskExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/CreditRiskExample.java b/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/CreditRiskExample.java deleted file mode 100644 index 5ab69e7..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/CreditRiskExample.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.computegrid.montecarlo; - -import org.apache.ignite.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -import java.util.*; - -/** - * Monte-Carlo example. Demonstrates distributed credit risk calculation. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public final class CreditRiskExample { - /** - * 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("Credit risk example started."); - - // Create portfolio. - Credit[] portfolio = new Credit[5000]; - - Random rnd = new Random(); - - // Generate some test portfolio items. - for (int i = 0; i < portfolio.length; i++) { - portfolio[i] = new Credit( - 50000 * rnd.nextDouble(), // Credit amount. - rnd.nextInt(1000), // Credit term in days. - rnd.nextDouble() / 10, // APR. - rnd.nextDouble() / 20 + 0.02 // EDF. - ); - } - - // Forecast horizon in days. - int horizon = 365; - - // Number of Monte-Carlo iterations. - int iter = 10000; - - // Percentile. - double percentile = 0.95; - - // Mark the stopwatch. - long start = System.currentTimeMillis(); - - // Calculate credit risk and print it out. - // As you can see the ignite enabling is completely hidden from the caller - // and it is fully transparent to him. In fact, the caller is never directly - // aware if method was executed just locally or on the 100s of cluster nodes. - // Credit risk crdRisk is the minimal amount that creditor has to have - // available to cover possible defaults. - - double crdRisk = ignite.compute().call(jobs(ignite.cluster().nodes().size(), portfolio, horizon, iter, percentile), - new IgniteReducer<Double, Double>() { - /** Collected values sum. */ - private double sum; - - /** Collected values count. */ - private int cnt; - - /** {@inheritDoc} */ - @Override public synchronized boolean collect(Double e) { - sum += e; - cnt++; - - return true; - } - - /** {@inheritDoc} */ - @Override public synchronized Double reduce() { - return sum / cnt; - } - }); - - System.out.println(); - System.out.println("Credit risk [crdRisk=" + crdRisk + ", duration=" + - (System.currentTimeMillis() - start) + "ms]"); - } - // We specifically don't do any error handling here to - // simplify the example. Real application may want to - // add error handling and application specific recovery. - } - - /** - * Creates closures for calculating credit risks. - * - * @param clusterSize Size of the cluster. - * @param portfolio Portfolio. - * @param horizon Forecast horizon in days. - * @param iter Number of Monte-Carlo iterations. - * @param percentile Percentile. - * @return Collection of closures. - */ - private static Collection<IgniteCallable<Double>> jobs(int clusterSize, final Credit[] portfolio, - final int horizon, int iter, final double percentile) { - // Number of iterations should be done by each node. - int iterPerNode = Math.round(iter / (float)clusterSize); - - // Number of iterations for the last/the only node. - int lastNodeIter = iter - (clusterSize - 1) * iterPerNode; - - Collection<IgniteCallable<Double>> clos = new ArrayList<>(clusterSize); - - // Note that for the purpose of this example we perform a simple homogeneous - // (non weighted) split assuming that all computing resources in this split - // will be identical. In real life scenarios when heterogeneous environment - // is used a split that is weighted by, for example, CPU benchmarks of each - // node in the split will be more efficient. It is fairly easy addition and - // Ignite comes with convenient Spring-compatible benchmark that can be - // used for weighted splits. - for (int i = 0; i < clusterSize; i++) { - final int nodeIter = i == clusterSize - 1 ? lastNodeIter : iterPerNode; - - clos.add(new IgniteCallable<Double>() { - /** {@inheritDoc} */ - @Override public Double call() { - return new CreditRiskManager().calculateCreditRiskMonteCarlo( - portfolio, horizon, nodeIter, percentile); - } - }); - } - - return clos; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/CreditRiskManager.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/CreditRiskManager.java b/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/CreditRiskManager.java deleted file mode 100644 index 051b782..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/CreditRiskManager.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.computegrid.montecarlo; - -import java.util.*; - -/** - * This class abstracts out the calculation of risk for a credit portfolio. - */ -@SuppressWarnings({"FloatingPointEquality"}) -public class CreditRiskManager { - /** - * Default randomizer with normal distribution. - * Note that since every JVM on the cluster will have its own random - * generator (independently initialized) the Monte-Carlo simulation - * will be slightly skewed when performed on the ignite cluster due to skewed - * normal distribution of the sub-jobs comparing to execution on the - * local node only with single random generator. Real-life applications - * may want to provide its own implementation of distributed random - * generator. - */ - private static Random rndGen = new Random(); - - /** - * Calculates credit risk for a given credit portfolio. This calculation uses - * Monte-Carlo Simulation to produce risk value. - * - * @param portfolio Credit portfolio. - * @param horizon Forecast horizon (in days). - * @param num Number of Monte-Carlo iterations. - * @param percentile Cutoff level. - * @return Credit risk value, i.e. the minimal amount that creditor has to - * have available to cover possible defaults. - */ - public double calculateCreditRiskMonteCarlo(Credit[] portfolio, int horizon, int num, double percentile) { - System.out.println(">>> Calculating credit risk for portfolio [size=" + portfolio.length + ", horizon=" + - horizon + ", percentile=" + percentile + ", iterations=" + num + "] <<<"); - - long start = System.currentTimeMillis(); - - double[] losses = calculateLosses(portfolio, horizon, num); - - Arrays.sort(losses); - - double[] lossProbs = new double[losses.length]; - - // Count variational numbers. - // Every next one either has the same value or previous one plus probability of loss. - for (int i = 0; i < losses.length; i++) - if (i == 0) - // First time it's just a probability of first value. - lossProbs[i] = getLossProbability(losses, 0); - else if (losses[i] != losses[i - 1]) - // Probability of this loss plus previous one. - lossProbs[i] = getLossProbability(losses, i) + lossProbs[i - 1]; - else - // The same loss the same probability. - lossProbs[i] = lossProbs[i - 1]; - - // Count percentile. - double crdRisk = 0; - - for (int i = 0; i < lossProbs.length; i++) - if (lossProbs[i] > percentile) { - crdRisk = losses[i - 1]; - - break; - } - - System.out.println(">>> Finished calculating portfolio risk [risk=" + crdRisk + - ", time=" + (System.currentTimeMillis() - start) + "ms]"); - - return crdRisk; - } - - /** - * Calculates losses for the given credit portfolio using Monte-Carlo Simulation. - * Simulates probability of default only. - * - * @param portfolio Credit portfolio. - * @param horizon Forecast horizon. - * @param num Number of Monte-Carlo iterations. - * @return Losses array simulated by Monte Carlo method. - */ - private double[] calculateLosses(Credit[] portfolio, int horizon, int num) { - double[] losses = new double[num]; - - // Count losses using Monte-Carlo method. We generate random probability of default, - // if it exceeds certain credit default value we count losses - otherwise count income. - for (int i = 0; i < num; i++) - for (Credit crd : portfolio) { - int remDays = Math.min(crd.getRemainingTerm(), horizon); - - if (rndGen.nextDouble() >= 1 - crd.getDefaultProbability(remDays)) - // (1 + 'r' * min(H, W) / 365) * S. - // Where W is a horizon, H is a remaining crediting term, 'r' is an annual credit rate, - // S is a remaining credit amount. - losses[i] += (1 + crd.getAnnualRate() * Math.min(horizon, crd.getRemainingTerm()) / 365) - * crd.getRemainingAmount(); - else - // - 'r' * min(H,W) / 365 * S - // Where W is a horizon, H is a remaining crediting term, 'r' is a annual credit rate, - // S is a remaining credit amount. - losses[i] -= crd.getAnnualRate() * Math.min(horizon, crd.getRemainingTerm()) / 365 * - crd.getRemainingAmount(); - } - - return losses; - } - - /** - * Calculates probability of certain loss in array of losses. - * - * @param losses Array of losses. - * @param i Index of certain loss in array. - * @return Probability of loss with given index. - */ - private double getLossProbability(double[] losses, int i) { - double cnt = 0; - double loss = losses[i]; - - for (double tmp : losses) - if (loss == tmp) - cnt++; - - return cnt / losses.length; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/package-info.java b/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/package-info.java deleted file mode 100644 index 2e4a018..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/computegrid/montecarlo/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * <!-- Package description. --> - * Monte-Carlo simulation example. - */ -package org.apache.ignite.examples.computegrid.montecarlo; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/computegrid/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/computegrid/package-info.java b/examples/src/main/java/org/apache/ignite/examples/computegrid/package-info.java deleted file mode 100644 index c072222..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/computegrid/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * <!-- Package description. --> - * Basic examples for computational ignite functionality. - */ -package org.apache.ignite.examples.computegrid; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheAffinityExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheAffinityExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheAffinityExample.java deleted file mode 100644 index 5c8d187..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheAffinityExample.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -import java.util.*; - -/** - * This example demonstrates the simplest code that populates the distributed cache - * and co-locates simple closure execution with each key. The goal of this particular - * example is to provide the simplest code example of this logic. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will - * start node with {@code examples/config/example-ignite.xml} configuration. - */ -public final class CacheAffinityExample { - /** Cache name. */ - private static final String CACHE_NAME = CacheAffinityExample.class.getSimpleName(); - - /** Number of keys. */ - private static final int KEY_CNT = 20; - - /** - * 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."); - - CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>(); - - cfg.setCacheMode(CacheMode.PARTITIONED); - cfg.setName(CACHE_NAME); - - try (IgniteCache<Integer, String> cache = ignite.createCache(cfg)) { - 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(); - } - } - } - - /** - * Collocates jobs with keys they need to work on using - * {@link IgniteCompute#affinityRun(String, Object, IgniteRunnable)} method. - */ - private static void visitUsingAffinityRun() { - Ignite ignite = Ignition.ignite(); - - final IgniteCache<Integer, String> cache = ignite.jcache(CACHE_NAME); - - for (int i = 0; i < KEY_CNT; i++) { - final int key = i; - - // This runnable will execute on the remote node where - // data with the given key is located. Since it will be co-located - // we can use local 'peek' operation safely. - ignite.compute().affinityRun(CACHE_NAME, key, new IgniteRunnable() { - @Override public void run() { - // Peek is a local memory lookup, however, value should never be 'null' - // as we are co-located with node that has a given key. - System.out.println("Co-located using affinityRun [key= " + key + ", value=" + cache.localPeek(key) + ']'); - } - }); - } - } - - /** - * Collocates jobs with keys they need to work on using {@link IgniteCluster#mapKeysToNodes(String, Collection)} - * method. The difference from {@code affinityRun(...)} method is that here we process multiple keys - * in a single job. - */ - private static void visitUsingMapKeysToNodes() { - final Ignite ignite = Ignition.ignite(); - - Collection<Integer> keys = new ArrayList<>(KEY_CNT); - - for (int i = 0; i < KEY_CNT; i++) - keys.add(i); - - // Map all keys to nodes. - Map<ClusterNode, Collection<Integer>> mappings = ignite.cluster().mapKeysToNodes(CACHE_NAME, keys); - - for (Map.Entry<ClusterNode, Collection<Integer>> mapping : mappings.entrySet()) { - ClusterNode node = mapping.getKey(); - - final Collection<Integer> mappedKeys = mapping.getValue(); - - if (node != null) { - // Bring computations to the nodes where the data resides (i.e. collocation). - ignite.compute(ignite.cluster().forNode(node)).run(new IgniteRunnable() { - @Override public void run() { - IgniteCache<Integer, String> cache = ignite.jcache(CACHE_NAME); - - // Peek is a local memory lookup, however, value should never be 'null' - // as we are co-located with node that has a given key. - for (Integer key : mappedKeys) - System.out.println("Co-located using mapKeysToNodes [key= " + key + - ", value=" + cache.localPeek(key) + ']'); - } - }); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheApiExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheApiExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheApiExample.java deleted file mode 100644 index c912d47..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheApiExample.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -import javax.cache.processor.*; -import java.util.concurrent.*; - -/** - * This example demonstrates some of the cache rich API capabilities. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will - * start node with {@code examples/config/example-ignite.xml} configuration. - */ -public class CacheApiExample { - /** Cache name. */ - private static final String CACHE_NAME = CacheApiExample.class.getSimpleName(); - - /** - * 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 API example started."); - - CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>(); - - cfg.setCacheMode(CacheMode.PARTITIONED); - cfg.setName(CACHE_NAME); - - try (IgniteCache<Integer, String> cache = ignite.createCache(cfg)) { - // Demonstrate atomic map operations. - atomicMapOperations(cache); - } - } - } - - /** - * Demonstrates cache operations similar to {@link ConcurrentMap} API. Note that - * cache API is a lot richer than the JDK {@link ConcurrentMap}. - * - * @throws IgniteException If failed. - */ - private static void atomicMapOperations(final IgniteCache<Integer, String> cache) throws IgniteException { - System.out.println(); - System.out.println(">>> Cache atomic map operation examples."); - - // Put and return previous value. - String v = cache.getAndPut(1, "1"); - assert v == null; - - // Put and do not return previous value (all methods ending with 'x' return boolean). - // Performs better when previous value is not needed. - cache.put(2, "2"); - - // Put asynchronously. - final IgniteCache<Integer, String> asyncCache = cache.withAsync(); - - asyncCache.put(3, "3"); - - asyncCache.get(3); - - IgniteFuture<String> fut = asyncCache.future(); - - //Asynchronously wait for result. - fut.listen(new IgniteInClosure<IgniteFuture<String>>() { - @Override - public void apply(IgniteFuture<String> fut) { - try { - System.out.println("Put operation completed [previous-value=" + fut.get() + ']'); - } - catch (IgniteException e) { - e.printStackTrace(); - } - } - }); - - // Put-if-absent. - boolean b1 = cache.putIfAbsent(4, "4"); - boolean b2 = cache.putIfAbsent(4, "44"); - assert b1 && !b2; - - // Invoke - assign new value based on previous value. - cache.put(6, "6"); - cache.invoke(6, new EntryProcessor<Integer, String, Object>() { - @Override public Object process(MutableEntry<Integer, String> entry, Object... args) { - String v = entry.getValue(); - - entry.setValue(v + "6"); // Set new value based on previous value. - - return null; - } - }); - - // Replace. - cache.put(7, "7"); - b1 = cache.replace(7, "7", "77"); - b2 = cache.replace(7, "7", "777"); - assert b1 & !b2; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheContinuousQueryExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheContinuousQueryExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheContinuousQueryExample.java deleted file mode 100644 index c12077e..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheContinuousQueryExample.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.query.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -import javax.cache.*; -import javax.cache.event.*; - -/** - * This examples demonstrates continuous query API. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will - * start node with {@code examples/config/example-ignite.xml} configuration. - */ -public class CacheContinuousQueryExample { - /** Cache name. */ - private static final String CACHE_NAME = CacheContinuousQueryExample.class.getSimpleName(); - - /** - * 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."); - - CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>(); - - cfg.setCacheMode(CacheMode.PARTITIONED); - cfg.setName(CACHE_NAME); - - try (IgniteCache<Integer, String> cache = ignite.createCache(cfg)) { - 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.setRemoteFilter(new IgniteCacheEntryEventFilter<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); - } - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheDataStreamerExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheDataStreamerExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheDataStreamerExample.java deleted file mode 100644 index 6de1402..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheDataStreamerExample.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; - -/** - * Demonstrates how cache can be populated with data utilizing {@link IgniteDataStreamer} API. - * {@link IgniteDataStreamer} is a lot more efficient to use than standard - * {@code put(...)} operation as it properly buffers cache requests - * together and properly manages load on remote nodes. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will - * start node with {@code examples/config/example-ignite.xml} configuration. - */ -public class CacheDataStreamerExample { - /** Cache name. */ - private static final String CACHE_NAME = CacheDataStreamerExample.class.getSimpleName(); - - /** Number of entries to load. */ - private static final int ENTRY_COUNT = 500000; - - /** Heap size required to run this example. */ - public static final int MIN_MEMORY = 512 * 1024 * 1024; - - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - ExamplesUtils.checkMinMemory(MIN_MEMORY); - - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println(">>> Cache data streamer example started."); - - CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>(); - - cfg.setCacheMode(CacheMode.PARTITIONED); - cfg.setName(CACHE_NAME); - - try (IgniteCache<Integer, String> cache = ignite.createCache(cfg)) { - System.out.println(); - System.out.println(">>> Cache clear finished."); - - try (IgniteDataStreamer<Integer, String> ldr = ignite.dataStreamer(CACHE_NAME)) { - long start = System.currentTimeMillis(); - - // Configure loader. - ldr.perNodeBufferSize(1024); - ldr.perNodeParallelOperations(8); - - for (int i = 0; i < ENTRY_COUNT; i++) { - ldr.addData(i, Integer.toString(i)); - - // Print out progress while loading cache. - if (i > 0 && i % 10000 == 0) - System.out.println("Loaded " + i + " keys."); - } - - long end = System.currentTimeMillis(); - - System.out.println(">>> Loaded " + ENTRY_COUNT + " keys in " + (end - start) + "ms."); - } - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheEventsExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheEventsExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheEventsExample.java deleted file mode 100644 index 1c9e29e..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheEventsExample.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.events.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -import java.util.*; - -import static org.apache.ignite.events.EventType.*; - -/** - * This examples demonstrates events API. Note that ignite events are disabled by default and - * must be specifically enabled, just like in {@code examples/config/example-ignite.xml} file. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will - * start node with {@code examples/config/example-ignite.xml} configuration. - */ -public class CacheEventsExample { - /** Cache name. */ - private static final String CACHE_NAME = CacheEventsExample.class.getSimpleName(); - - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException, InterruptedException { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println(">>> Cache events example started."); - - CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>(); - - cfg.setCacheMode(CacheMode.PARTITIONED); - cfg.setName(CACHE_NAME); - - try (IgniteCache<Integer, String> cache = ignite.createCache(cfg)) { - // This optional local callback is called for each event notification - // that passed remote predicate listener. - IgniteBiPredicate<UUID, CacheEvent> locLsnr = new IgniteBiPredicate<UUID, CacheEvent>() { - @Override public boolean apply(UUID uuid, CacheEvent evt) { - System.out.println("Received event [evt=" + evt.name() + ", key=" + evt.key() + - ", oldVal=" + evt.oldValue() + ", newVal=" + evt.newValue()); - - return true; // Continue listening. - } - }; - - // Remote listener which only accepts events for keys that are - // greater or equal than 10 and if event node is primary for this key. - IgnitePredicate<CacheEvent> rmtLsnr = new IgnitePredicate<CacheEvent>() { - @Override public boolean apply(CacheEvent evt) { - System.out.println("Cache event [name=" + evt.name() + ", key=" + evt.key() + ']'); - - int key = evt.key(); - - return key >= 10 && ignite.affinity(CACHE_NAME).isPrimary(ignite.cluster().localNode(), key); - } - }; - - // Subscribe to specified cache events on all nodes that have cache running. - // Cache events are explicitly enabled in examples/config/example-ignite.xml file. - ignite.events(ignite.cluster().forCacheNodes(CACHE_NAME)).remoteListen(locLsnr, rmtLsnr, - EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_REMOVED); - - // Generate cache events. - for (int i = 0; i < 20; i++) - cache.put(i, Integer.toString(i)); - - // Wait for a while while callback is notified about remaining puts. - Thread.sleep(2000); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/datagrid/CachePopularNumbersExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CachePopularNumbersExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CachePopularNumbersExample.java deleted file mode 100644 index b1cc6d8..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CachePopularNumbersExample.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.query.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.stream.*; - -import javax.cache.processor.*; -import java.util.*; - -/** - * Real time popular numbers counter. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will - * start node with {@code examples/config/example-ignite.xml} configuration. - */ -public class CachePopularNumbersExample { - /** Cache name. */ - private static final String CACHE_NAME = CachePopularNumbersExample.class.getSimpleName(); - - /** Count of most popular numbers to retrieve from cluster. */ - private static final int POPULAR_NUMBERS_CNT = 10; - - /** Random number generator. */ - private static final Random RAND = new Random(); - - /** Range within which to generate numbers. */ - private static final int RANGE = 1000; - - /** Count of total numbers to generate. */ - private static final int CNT = 1000000; - - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - Timer popularNumbersQryTimer = new Timer("numbers-query-worker"); - - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println(">>> Cache popular numbers example started."); - - CacheConfiguration<Integer, Long> cfg = new CacheConfiguration<>(); - - cfg.setCacheMode(CacheMode.PARTITIONED); - cfg.setName(CACHE_NAME); - cfg.setIndexedTypes( - Integer.class, Long.class - ); - - try (IgniteCache<Integer, Long> cache = ignite.createCache(cfg)) { - ClusterGroup prj = ignite.cluster().forCacheNodes(CACHE_NAME); - - if (prj.nodes().isEmpty()) { - System.out.println("Ignite does not have cache configured: " + CACHE_NAME); - - return; - } - - TimerTask task = scheduleQuery(ignite, popularNumbersQryTimer, POPULAR_NUMBERS_CNT); - - streamData(ignite); - - // Force one more run to get final counts. - task.run(); - - popularNumbersQryTimer.cancel(); - } - } - } - - /** - * Populates cache in real time with numbers and keeps count for every number. - * - * @param ignite Ignite. - * @throws IgniteException If failed. - */ - private static void streamData(final Ignite ignite) throws IgniteException { - try (IgniteDataStreamer<Integer, Long> stmr = ignite.dataStreamer(CACHE_NAME)) { - // Set larger per-node buffer size since our state is relatively small. - stmr.perNodeBufferSize(2048); - - stmr.receiver(new IncrementingUpdater()); - - for (int i = 0; i < CNT; i++) - stmr.addData(RAND.nextInt(RANGE), 1L); - } - } - - /** - * Schedules our popular numbers query to run every 3 seconds. - * - * @param ignite Ignite. - * @param timer Timer. - * @param cnt Number of popular numbers to return. - * @return Scheduled task. - */ - private static TimerTask scheduleQuery(final Ignite ignite, Timer timer, final int cnt) { - TimerTask task = new TimerTask() { - @Override public void run() { - // Get reference to cache. - IgniteCache<Integer, Long> cache = ignite.jcache(CACHE_NAME); - - try { - List<List<?>> results = cache.query( - new SqlFieldsQuery("select _key, _val from Long order by _val desc, _key limit ?").setArgs(cnt)) - .getAll(); - - for (List<?> res : results) - System.out.println(res.get(0) + "=" + res.get(1)); - - System.out.println("----------------"); - } - catch (Exception e) { - e.printStackTrace(); - } - } - }; - - timer.schedule(task, 3000, 3000); - - return task; - } - - /** - * Increments value for key. - */ - private static class IncrementingUpdater implements StreamReceiver<Integer, Long> { - /** Process entries to increase value by entry key. */ - private static final EntryProcessor<Integer, Long, ?> INC = new EntryProcessor<Integer, Long, Object>() { - @Override public Object process(MutableEntry<Integer, Long> e, Object... args) { - Long val = e.getValue(); - - e.setValue(val == null ? 1L : val + 1); - - return null; - } - }; - - /** {@inheritDoc} */ - @Override public void receive(IgniteCache<Integer, Long> cache, Collection<Map.Entry<Integer, Long>> entries) { - for (Map.Entry<Integer, Long> entry : entries) - cache.invoke(entry.getKey(), INC); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/datagrid/CachePutGetExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CachePutGetExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CachePutGetExample.java deleted file mode 100644 index acb46ed..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CachePutGetExample.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; - -import java.util.*; - -/** - * This example demonstrates very basic operations on cache, such as 'put' and 'get'. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will - * start node with {@code examples/config/example-ignite.xml} configuration. - */ -public class CachePutGetExample { - /** Cache name. */ - private static final String CACHE_NAME = CachePutGetExample.class.getSimpleName(); - - /** - * 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")) { - - CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>(); - - cfg.setCacheMode(CacheMode.PARTITIONED); - cfg.setName(CACHE_NAME); - - try (IgniteCache<Integer, String> cache = ignite.createCache(cfg)) { - // Individual puts and gets. - putGet(cache); - - // Bulk puts and gets. - putAllGetAll(cache); - } - } - } - - /** - * Execute individual puts and gets. - * - * @throws IgniteException If failed. - */ - private static void putGet(IgniteCache<Integer, String> cache) throws IgniteException { - System.out.println(); - System.out.println(">>> Cache put-get example started."); - - final int keyCnt = 20; - - // Store keys in cache. - for (int i = 0; i < keyCnt; i++) - cache.put(i, Integer.toString(i)); - - System.out.println(">>> Stored values in cache."); - - for (int i = 0; i < keyCnt; i++) - System.out.println("Got [key=" + i + ", val=" + cache.get(i) + ']'); - } - - /** - * Execute bulk {@code putAll(...)} and {@code getAll(...)} operations. - * - * @throws IgniteException If failed. - */ - private static void putAllGetAll(IgniteCache<Integer, String> cache) throws IgniteException { - System.out.println(); - System.out.println(">>> Starting putAll-getAll example."); - - final int keyCnt = 20; - - // Create batch. - Map<Integer, String> batch = new HashMap<>(); - - for (int i = 0; i < keyCnt; i++) - batch.put(i, "bulk-" + Integer.toString(i)); - - // Bulk-store entries in cache. - cache.putAll(batch); - - System.out.println(">>> Bulk-stored values in cache."); - - // Bulk-get values from cache. - Map<Integer, String> vals = cache.getAll(batch.keySet()); - - for (Map.Entry<Integer, String> e : vals.entrySet()) - System.out.println("Got entry [key=" + e.getKey() + ", val=" + e.getValue() + ']'); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryExample.java deleted file mode 100644 index 9d9a6fd..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryExample.java +++ /dev/null @@ -1,408 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.affinity.*; -import org.apache.ignite.cache.query.*; -import org.apache.ignite.cache.query.annotations.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; - -import javax.cache.*; -import java.io.*; -import java.util.*; - -/** - * Cache queries example. This example demonstrates SQL, TEXT, and FULL SCAN - * queries over cache. - * <p> - * Example also demonstrates usage of fields queries that return only required - * fields instead of whole key-value pairs. When fields queries are distributed - * across several nodes, they may not work as expected. Keep in mind following - * limitations (not applied if data is queried from one node only): - * <ul> - * <li> - * {@code Group by} and {@code sort by} statements are applied separately - * on each node, so result set will likely be incorrectly grouped or sorted - * after results from multiple remote nodes are grouped together. - * </li> - * <li> - * Aggregation functions like {@code sum}, {@code max}, {@code avg}, etc. - * are also applied on each node. Therefore you will get several results - * containing aggregated values, one for each node. - * </li> - * <li> - * Joins will work correctly only if joined objects are stored in - * collocated mode. Refer to {@link CacheAffinityKey} javadoc for more details. - * </li> - * <li> - * Note that if you created query on to replicated cache, all data will - * be queried only on one node, not depending on what caches participate in - * the query (some data from partitioned cache can be lost). And visa versa, - * if you created it on partitioned cache, data from replicated caches - * will be duplicated. - * </li> - * </ul> - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will - * start node with {@code examples/config/example-ignite.xml} configuration. - */ -public class CacheQueryExample { - /** Cache name. */ - private static final String CACHE_NAME = CacheQueryExample.class.getSimpleName(); - - /** - * 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 query example started."); - - CacheConfiguration<?, ?> cfg = new CacheConfiguration<>(); - - cfg.setCacheMode(CacheMode.PARTITIONED); - cfg.setName(CACHE_NAME); - cfg.setIndexedTypes( - UUID.class, Organization.class, - CacheAffinityKey.class, Person.class - ); - - try (IgniteCache<?, ?> cache = ignite.createCache(cfg)) { - // Populate cache. - initialize(); - - // Example for SQL-based querying employees based on salary ranges. - sqlQuery(); - - // Example for SQL-based querying employees for a given organization (includes SQL join). - sqlQueryWithJoin(); - - // Example for TEXT-based querying for a given string in peoples resumes. - textQuery(); - - // Example for SQL-based querying to calculate average salary among all employees within a company. - sqlQueryWithAggregation(); - - // Example for SQL-based fields queries that return only required - // fields instead of whole key-value pairs. - sqlFieldsQuery(); - - // Example for SQL-based fields queries that return only required - // fields instead of whole key-value pairs. - sqlFieldsQuery(); - - // Example for SQL-based fields queries that uses joins. - sqlFieldsQueryWithJoin(); - } - - print("Cache query example finished."); - } - } - - /** - * Example for SQL queries based on salary ranges. - */ - private static void sqlQuery() { - IgniteCache<CacheAffinityKey<UUID>, Person> cache = Ignition.ignite().jcache(CACHE_NAME); - - // SQL clause which selects salaries based on range. - String sql = "salary > ? and salary <= ?"; - - // Execute queries for salary ranges. - print("People with salaries between 0 and 1000: ", - cache.query(new SqlQuery<CacheAffinityKey<UUID>, Person>(Person.class, sql). - setArgs(0, 1000)).getAll()); - - print("People with salaries between 1000 and 2000: ", - cache.query(new SqlQuery<CacheAffinityKey<UUID>, Person>(Person.class, sql). - setArgs(1000, 2000)).getAll()); - - print("People with salaries greater than 2000: ", - cache.query(new SqlQuery<CacheAffinityKey<UUID>, Person>(Person.class, sql). - setArgs(2000, Integer.MAX_VALUE)).getAll()); - } - - /** - * Example for SQL queries based on all employees working for a specific organization. - */ - private static void sqlQueryWithJoin() { - IgniteCache<CacheAffinityKey<UUID>, Person> cache = Ignition.ignite().jcache(CACHE_NAME); - - // SQL clause query which joins on 2 types to select people for a specific organization. - String joinSql = - "from Person, Organization " - + "where Person.orgId = Organization.id " - + "and lower(Organization.name) = lower(?)"; - - // Execute queries for find employees for different organizations. - print("Following people are 'GridGain' employees: ", - cache.query(new SqlQuery<CacheAffinityKey<UUID>, Person>(Person.class, joinSql). - setArgs("GridGain")).getAll()); - print("Following people are 'Other' employees: ", - cache.query(new SqlQuery<CacheAffinityKey<UUID>, Person>(Person.class, joinSql). - setArgs("Other")).getAll()); - } - - /** - * Example for TEXT queries using LUCENE-based indexing of people's resumes. - */ - private static void textQuery() { - IgniteCache<CacheAffinityKey<UUID>, Person> cache = Ignition.ignite().jcache(CACHE_NAME); - - // Query for all people with "Master Degree" in their resumes. - QueryCursor<Cache.Entry<CacheAffinityKey<UUID>, Person>> masters = - cache.query(new TextQuery<CacheAffinityKey<UUID>, Person>(Person.class, "Master")); - - // Query for all people with "Bachelor Degree" in their resumes. - QueryCursor<Cache.Entry<CacheAffinityKey<UUID>, Person>> bachelors = - cache.query(new TextQuery<CacheAffinityKey<UUID>, Person>(Person.class, "Bachelor")); - - print("Following people have 'Master Degree' in their resumes: ", masters.getAll()); - print("Following people have 'Bachelor Degree' in their resumes: ", bachelors.getAll()); - } - - /** - * Example for SQL queries to calculate average salary for a specific organization. - */ - private static void sqlQueryWithAggregation() { - IgniteCache<CacheAffinityKey<UUID>, Person> cache = Ignition.ignite().jcache(CACHE_NAME); - - // Calculate average of salary of all persons in GridGain. - QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery( - "select avg(salary) from Person, Organization where Person.orgId = Organization.id and " - + "lower(Organization.name) = lower(?)").setArgs("GridGain")); - - // Calculate average salary for a specific organization. - print("Average salary for 'GridGain' employees: " + cursor.getAll()); - } - - /** - * Example for SQL-based fields queries that return only required - * fields instead of whole key-value pairs. - */ - private static void sqlFieldsQuery() { - IgniteCache<?, ?> cache = Ignition.ignite().jcache(CACHE_NAME); - - // Create query to get names of all employees. - QueryCursor<List<?>> cursor = cache.query( - new SqlFieldsQuery("select concat(firstName, ' ', lastName) from Person")); - - // Execute query to get collection of rows. In this particular - // case each row will have one element with full name of an employees. - List<List<?>> res = cursor.getAll(); - - // Print names. - print("Names of all employees:", res); - } - - /** - * Example for SQL-based fields queries that return only required - * fields instead of whole key-value pairs. - */ - private static void sqlFieldsQueryWithJoin() { - IgniteCache<?, ?> cache = Ignition.ignite().jcache(CACHE_NAME); - - // Execute query to get names of all employees. - QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("select concat(firstName, ' ', lastName), " - + "Organization.name from Person, Organization where " - + "Person.orgId = Organization.id")); - - // In this particular case each row will have one element with full name of an employees. - List<List<?>> res = cursor.getAll(); - - // Print persons' names and organizations' names. - print("Names of all employees and organizations they belong to:", res); - } - - /** - * Populate cache with test data. - */ - private static void initialize() { - IgniteCache<Object, Object> cache = Ignition.ignite().jcache(CACHE_NAME); - - // Organizations. - Organization org1 = new Organization("GridGain"); - Organization org2 = new Organization("Other"); - - // People. - Person p1 = new Person(org1, "John", "Doe", 2000, "John Doe has Master Degree."); - Person p2 = new Person(org1, "Jane", "Doe", 1000, "Jane Doe has Bachelor Degree."); - Person p3 = new Person(org2, "John", "Smith", 1000, "John Smith has Bachelor Degree."); - Person p4 = new Person(org2, "Jane", "Smith", 2000, "Jane Smith has Master Degree."); - - cache.put(org1.id, org1); - cache.put(org2.id, org2); - - // Note that in this example we use custom affinity key for Person objects - // to ensure that all persons are collocated with their organizations. - cache.put(p1.key(), p1); - cache.put(p2.key(), p2); - cache.put(p3.key(), p3); - cache.put(p4.key(), p4); - } - - /** - * Prints collection of objects to standard out. - * - * @param msg Message to print before all objects are printed. - * @param col Query results. - */ - private static void print(String msg, Iterable<?> col) { - if (msg != null) - System.out.println(">>> " + msg); - - print(col); - } - - /** - * Prints collection items. - * - * @param col Collection. - */ - private static void print(Iterable<?> col) { - for (Object next : col) { - if (next instanceof Iterable) - print((Iterable<?>)next); - else - System.out.println(">>> " + next); - } - } - - /** - * Prints out given object to standard out. - * - * @param o Object to print. - */ - private static void print(Object o) { - System.out.println(">>> " + o); - } - - /** - * Person class. - */ - private static class Person implements Serializable { - /** Person ID (indexed). */ - @QuerySqlField(index = true) - private UUID id; - - /** Organization ID (indexed). */ - @QuerySqlField(index = true) - private UUID orgId; - - /** First name (not-indexed). */ - @QuerySqlField - private String firstName; - - /** Last name (not indexed). */ - @QuerySqlField - private String lastName; - - /** Resume text (create LUCENE-based TEXT index for this field). */ - @QueryTextField - private String resume; - - /** Salary (indexed). */ - @QuerySqlField(index = true) - private double salary; - - /** Custom cache key to guarantee that person is always collocated with its organization. */ - private transient CacheAffinityKey<UUID> key; - - /** - * Constructs person record. - * - * @param org Organization. - * @param firstName First name. - * @param lastName Last name. - * @param salary Salary. - * @param resume Resume text. - */ - Person(Organization org, String firstName, String lastName, double salary, String resume) { - // Generate unique ID for this person. - id = UUID.randomUUID(); - - orgId = org.id; - - this.firstName = firstName; - this.lastName = lastName; - this.resume = resume; - this.salary = salary; - } - - /** - * Gets cache affinity key. Since in some examples person needs to be collocated with organization, we create - * custom affinity key to guarantee this collocation. - * - * @return Custom affinity key to guarantee that person is always collocated with organization. - */ - public CacheAffinityKey<UUID> key() { - if (key == null) - key = new CacheAffinityKey<>(id, orgId); - - return key; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "Person [firstName=" + firstName + - ", lastName=" + lastName + - ", id=" + id + - ", orgId=" + orgId + - ", resume=" + resume + - ", salary=" + salary + ']'; - } - } - - /** - * Organization class. - */ - private static class Organization implements Serializable { - /** Organization ID (indexed). */ - @QuerySqlField(index = true) - private UUID id; - - /** Organization name (indexed). */ - @QuerySqlField(index = true) - private String name; - - /** - * Create organization. - * - * @param name Organization name. - */ - Organization(String name) { - id = UUID.randomUUID(); - - this.name = name; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "Organization [id=" + id + ", name=" + name + ']'; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheTransactionExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheTransactionExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheTransactionExample.java deleted file mode 100644 index 5feb0d9..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheTransactionExample.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.transactions.*; - -import java.io.*; - -import static org.apache.ignite.transactions.TransactionConcurrency.*; -import static org.apache.ignite.transactions.TransactionIsolation.*; - -/** - * Demonstrates how to use cache transactions. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will - * start node with {@code examples/config/example-ignite.xml} configuration. - */ -public class CacheTransactionExample { - /** Cache name. */ - private static final String CACHE_NAME = CacheTransactionExample.class.getSimpleName(); - - /** - * 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 transaction example started."); - - CacheConfiguration<Integer, Account> cfg = new CacheConfiguration<>(); - - cfg.setCacheMode(CacheMode.PARTITIONED); - cfg.setName(CACHE_NAME); - cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); - - NearCacheConfiguration<Integer, Account> nearCacheCfg = new NearCacheConfiguration<>(); - - try (IgniteCache<Integer, Account> cache = ignite.createCache(cfg, nearCacheCfg)) { - // Initialize. - cache.put(1, new Account(1, 100)); - cache.put(2, new Account(1, 200)); - - System.out.println(); - System.out.println(">>> Accounts before deposit: "); - System.out.println(">>> " + cache.get(1)); - System.out.println(">>> " + cache.get(2)); - - // Make transactional deposits. - deposit(cache, 1, 100); - deposit(cache, 2, 200); - - System.out.println(); - System.out.println(">>> Accounts after transfer: "); - System.out.println(">>> " + cache.get(1)); - System.out.println(">>> " + cache.get(2)); - - System.out.println(">>> Cache transaction example finished."); - } - } - } - - /** - * Make deposit into specified account. - * - * @param acctId Account ID. - * @param amount Amount to deposit. - * @throws IgniteException If failed. - */ - private static void deposit(IgniteCache<Integer, Account> cache, int acctId, double amount) throws IgniteException { - try (Transaction tx = Ignition.ignite().transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { - Account acct0 = cache.get(acctId); - - assert acct0 != null; - - Account acct = new Account(acct0.id, acct0.balance); - - // Deposit into account. - acct.update(amount); - - // Store updated account in cache. - cache.put(acctId, acct); - - tx.commit(); - } - - System.out.println(); - System.out.println(">>> Transferred amount: $" + amount); - } - - /** - * Account. - */ - private static class Account implements Serializable { - /** Account ID. */ - private int id; - - /** Account balance. */ - private double balance; - - /** - * @param id Account ID. - * @param balance Balance. - */ - Account(int id, double balance) { - this.id = id; - this.balance = balance; - } - - /** - * Change balance by specified amount. - * - * @param amount Amount to add to balance (may be negative). - */ - void update(double amount) { - balance += amount; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "Account [id=" + id + ", balance=$" + balance + ']'; - } - } -}