Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1FC2218E7C for ; Thu, 24 Mar 2016 13:14:03 +0000 (UTC) Received: (qmail 31409 invoked by uid 500); 24 Mar 2016 13:14:03 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 31293 invoked by uid 500); 24 Mar 2016 13:14:02 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 31196 invoked by uid 99); 24 Mar 2016 13:14:02 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Mar 2016 13:14:02 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 98B1EE050B; Thu, 24 Mar 2016 13:14:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.apache.org Date: Thu, 24 Mar 2016 13:14:04 -0000 Message-Id: In-Reply-To: <3adbbc65c0f043949d6d968018dbcf1e@git.apache.org> References: <3adbbc65c0f043949d6d968018dbcf1e@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [03/11] ignite git commit: IGNITE-2415: CacheLoadOnlyStoreAdapter use example. - Fixes #569. IGNITE-2415: CacheLoadOnlyStoreAdapter use example. - Fixes #569. Signed-off-by: shtykh_roman Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/2f64ab0b Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/2f64ab0b Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/2f64ab0b Branch: refs/heads/ignite-2004 Commit: 2f64ab0b4739a646dfb2c3b1fb2ed5b8039b43b4 Parents: 66f9a34 Author: shtykh_roman Authored: Thu Mar 24 11:00:21 2016 +0900 Committer: shtykh_roman Committed: Thu Mar 24 11:00:21 2016 +0900 ---------------------------------------------------------------------- .../store/CacheLoadOnlyStoreExample.java | 166 +++++++++++++++++++ examples/src/main/resources/person.csv | 20 +++ .../ignite/examples/CacheExamplesSelfTest.java | 8 + 3 files changed, 194 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/2f64ab0b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheLoadOnlyStoreExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheLoadOnlyStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheLoadOnlyStoreExample.java new file mode 100644 index 0000000..4635c16 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheLoadOnlyStoreExample.java @@ -0,0 +1,166 @@ +/* + * 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.store; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.Serializable; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Scanner; +import javax.cache.configuration.FactoryBuilder; +import javax.cache.integration.CacheLoaderException; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteException; +import org.apache.ignite.Ignition; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CachePeekMode; +import org.apache.ignite.cache.store.CacheLoadOnlyStoreAdapter; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.examples.ExampleNodeStartup; +import org.apache.ignite.examples.model.Person; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.internal.util.typedef.T2; +import org.apache.ignite.lang.IgniteBiTuple; +import org.jetbrains.annotations.Nullable; + +/** + * Example of how to load data from CSV file using {@link CacheLoadOnlyStoreAdapter}. + *

+ * The adapter is intended to be used in cases when you need to pre-load a cache from text or file of any other format. + *

+ * Remote nodes can be started with {@link ExampleNodeStartup} in another JVM which will + * start node with {@code examples/config/example-ignite.xml} configuration. + */ +public class CacheLoadOnlyStoreExample { + /** Cache name. */ + private static final String CACHE_NAME = CacheLoadOnlyStoreExample.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(">>> CacheLoadOnlyStoreExample started."); + + ProductLoader productLoader = new ProductLoader("examples/src/main/resources/person.csv"); + + productLoader.setThreadsCount(2); + productLoader.setBatchSize(10); + productLoader.setBatchQueueSize(1); + + try (IgniteCache cache = ignite.getOrCreateCache(cacheConfiguration(productLoader))) { + // load data. + cache.loadCache(null); + + System.out.println(">>> Loaded number of items: " + cache.size(CachePeekMode.PRIMARY)); + + System.out.println(">>> Data for the person by id1: " + cache.get(1L)); + } + finally { + // Distributed cache could be removed from cluster only by #destroyCache() call. + ignite.destroyCache(CACHE_NAME); + } + } + } + + /** + * Creates cache configurations for the loader. + * + * @return {@link CacheConfiguration}. + */ + private static CacheConfiguration cacheConfiguration(ProductLoader productLoader) { + CacheConfiguration cacheCfg = new CacheConfiguration(); + + cacheCfg.setCacheMode(CacheMode.PARTITIONED); + cacheCfg.setName(CACHE_NAME); + + // provide the loader. + cacheCfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory(productLoader)); + + return cacheCfg; + } + + /** + * Csv data loader for product data. + */ + private static class ProductLoader extends CacheLoadOnlyStoreAdapter implements Serializable { + /** Csv file name. */ + final String csvFileName; + + /** Constructor. */ + ProductLoader(String csvFileName) { + this.csvFileName = csvFileName; + } + + /** {@inheritDoc} */ + @Override protected Iterator inputIterator(@Nullable Object... args) throws CacheLoaderException { + final Scanner scanner; + + try { + File path = IgniteUtils.resolveIgnitePath(csvFileName); + + if (path == null) + throw new CacheLoaderException("Failed to open the source file: " + csvFileName); + + scanner = new Scanner(path); + + scanner.useDelimiter("\\n"); + } + catch (FileNotFoundException e) { + throw new CacheLoaderException("Failed to open the source file " + csvFileName, e); + } + + /** + * Iterator for text input. The scanner is implicitly closed when there's nothing to scan. + */ + return new Iterator() { + /** {@inheritDoc} */ + @Override public boolean hasNext() { + if (!scanner.hasNext()) { + scanner.close(); + + return false; + } + + return true; + } + + /** {@inheritDoc} */ + @Override public String next() { + if (!hasNext()) + throw new NoSuchElementException(); + + return scanner.next(); + } + }; + } + + /** {@inheritDoc} */ + @Nullable @Override protected IgniteBiTuple parse(String rec, @Nullable Object... args) { + String[] p = rec.split("\\s*,\\s*"); + return new T2<>(Long.valueOf(p[0]), new Person(Long.valueOf(p[0]), Long.valueOf(p[1]), + p[2], p[3], Double.valueOf(p[4]), p[5].trim())); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/2f64ab0b/examples/src/main/resources/person.csv ---------------------------------------------------------------------- diff --git a/examples/src/main/resources/person.csv b/examples/src/main/resources/person.csv new file mode 100644 index 0000000..6f45854 --- /dev/null +++ b/examples/src/main/resources/person.csv @@ -0,0 +1,20 @@ +1,201,name1,surname1,1000,r1 +2,202,name2,surname2,2000,r2 +3,203,name3,surname3,3000,r3 +4,204,name4,surname4,4000,r4 +5,205,name5,surname5,5000,r5 +6,206,name6,surname6,6000,r6 +7,207,name7,surname7,7000,r7 +8,208,name8,surname8,8000,r8 +9,209,name9,surname9,9000,r9 +10,210,name10,surname10,10000,r10 +11,211,name11,surname11,11000,r11 +12,212,name12,surname12,12000,r12 +13,213,name13,surname13,13000,r13 +14,214,name14,surname14,14000,r14 +15,215,name15,surname15,15000,r15 +16,216,name16,surname16,16000,r16 +17,217,name17,surname17,17000,r17 +18,218,name18,surname18,18000,r18 +19,219,name19,surname19,19000,r19 +20,220,name20,surname20,20000,r20 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/2f64ab0b/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java ---------------------------------------------------------------------- diff --git a/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java b/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java index 39c2ea6..541291b 100644 --- a/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java +++ b/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java @@ -26,6 +26,7 @@ import org.apache.ignite.examples.datagrid.CachePutGetExample; import org.apache.ignite.examples.datagrid.CacheQueryExample; import org.apache.ignite.examples.datagrid.CacheTransactionExample; import org.apache.ignite.examples.datagrid.starschema.CacheStarSchemaExample; +import org.apache.ignite.examples.datagrid.store.CacheLoadOnlyStoreExample; import org.apache.ignite.examples.datastructures.IgniteAtomicLongExample; import org.apache.ignite.examples.datastructures.IgniteAtomicReferenceExample; import org.apache.ignite.examples.datastructures.IgniteAtomicSequenceExample; @@ -158,4 +159,11 @@ public class CacheExamplesSelfTest extends GridAbstractExamplesTest { public void testCacheContinuousQueryExample() throws Exception { CacheContinuousQueryExample.main(EMPTY_ARGS); } + + /** + * @throws Exception If failed. + */ + public void testCacheLoadOnlyStoreExample() throws Exception { + CacheLoadOnlyStoreExample.main(EMPTY_ARGS); + } }