# ignite-45 - Example fixes.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/235da895 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/235da895 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/235da895 Branch: refs/heads/ignite-497-stick Commit: 235da8956721133e108f1b2c86c7d8c3338b6fec Parents: a628cb9 Author: Dmitiry Setrakyan <dsetrak...@gridgain.com> Authored: Sun Mar 22 22:03:55 2015 -0700 Committer: Dmitiry Setrakyan <dsetrak...@gridgain.com> Committed: Sun Mar 22 22:03:55 2015 -0700 ---------------------------------------------------------------------- .../datagrid/CacheDataStreamerExample.java | 16 +- .../datagrid/CachePopularNumbersExample.java | 172 ------------------- 2 files changed, 8 insertions(+), 180 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/235da895/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 index 6de1402..d4bbde9 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheDataStreamerExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheDataStreamerExample.java @@ -66,25 +66,25 @@ public class CacheDataStreamerExample { System.out.println(); System.out.println(">>> Cache clear finished."); - try (IgniteDataStreamer<Integer, String> ldr = ignite.dataStreamer(CACHE_NAME)) { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); + try (IgniteDataStreamer<Integer, String> stmr = ignite.dataStreamer(CACHE_NAME)) { // Configure loader. - ldr.perNodeBufferSize(1024); - ldr.perNodeParallelOperations(8); + stmr.perNodeBufferSize(1024); + stmr.perNodeParallelOperations(8); for (int i = 0; i < ENTRY_COUNT; i++) { - ldr.addData(i, Integer.toString(i)); + stmr.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(); + long end = System.currentTimeMillis(); - System.out.println(">>> Loaded " + ENTRY_COUNT + " keys in " + (end - start) + "ms."); - } + System.out.println(">>> Loaded " + ENTRY_COUNT + " keys in " + (end - start) + "ms."); } } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/235da895/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); - } - } -}