Search in sources :

Example 11 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration in project ignite by apache.

the class CacheQueryDmlExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws Exception If example execution failed.
     */
@SuppressWarnings({ "unused", "ThrowFromFinallyBlock" })
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        print("Cache query DML example started.");
        CacheConfiguration<Long, Organization> orgCacheCfg = new CacheConfiguration<>(ORG_CACHE);
        orgCacheCfg.setIndexedTypes(Long.class, Organization.class);
        CacheConfiguration<Long, Person> personCacheCfg = new CacheConfiguration<>(PERSON_CACHE);
        personCacheCfg.setIndexedTypes(Long.class, Person.class);
        // Auto-close cache at the end of the example.
        try (IgniteCache<Long, Organization> orgCache = ignite.getOrCreateCache(orgCacheCfg);
            IgniteCache<Long, Person> personCache = ignite.getOrCreateCache(personCacheCfg)) {
            insert(orgCache, personCache);
            select(personCache, "Insert data");
            update(personCache);
            select(personCache, "Update salary for Master degrees");
            delete(personCache);
            select(personCache, "Delete non-Apache employees");
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(PERSON_CACHE);
            ignite.destroyCache(ORG_CACHE);
        }
        print("Cache query DML example finished.");
    }
}
Also used : Organization(org.apache.ignite.examples.model.Organization) Ignite(org.apache.ignite.Ignite) Person(org.apache.ignite.examples.model.Person) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 12 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration in project ignite by apache.

the class StreamTransformerExample method main.

public static void main(String[] args) throws Exception {
    // Mark this cluster member as client.
    Ignition.setClientMode(true);
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.hasServerNodes(ignite))
            return;
        CacheConfiguration<Integer, Long> cfg = new CacheConfiguration<>(CACHE_NAME);
        // Index key and value.
        cfg.setIndexedTypes(Integer.class, Long.class);
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, Long> stmCache = ignite.getOrCreateCache(cfg)) {
            try (IgniteDataStreamer<Integer, Long> stmr = ignite.dataStreamer(stmCache.getName())) {
                // Allow data updates.
                stmr.allowOverwrite(true);
                // Configure data transformation to count random numbers added to the stream.
                stmr.receiver(StreamTransformer.from((e, arg) -> {
                    // Get current count.
                    Long val = e.getValue();
                    // Increment count by 1.
                    e.setValue(val == null ? 1L : val + 1);
                    return null;
                }));
                // Stream 10 million of random numbers into the streamer cache.
                for (int i = 1; i <= 10_000_000; i++) {
                    stmr.addData(RAND.nextInt(RANGE), 1L);
                    if (i % 500_000 == 0)
                        System.out.println("Number of tuples streamed into Ignite: " + i);
                }
            }
            // Query top 10 most popular numbers every.
            SqlFieldsQuery top10Qry = new SqlFieldsQuery("select _key, _val from Long order by _val desc limit 10");
            // Execute queries.
            List<List<?>> top10 = stmCache.query(top10Qry).getAll();
            System.out.println("Top 10 most popular numbers:");
            // Print top 10 words.
            ExamplesUtils.printQueryResults(top10);
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : List(java.util.List) Ignition(org.apache.ignite.Ignition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) ExamplesUtils(org.apache.ignite.examples.ExamplesUtils) Random(java.util.Random) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) StreamTransformer(org.apache.ignite.stream.StreamTransformer) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 13 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration in project ignite by apache.

the class StreamVisitorExample method main.

public static void main(String[] args) throws Exception {
    // Mark this cluster member as client.
    Ignition.setClientMode(true);
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.hasServerNodes(ignite))
            return;
        // Market data cache with default configuration.
        CacheConfiguration<String, Double> mktDataCfg = new CacheConfiguration<>("marketTicks");
        // Financial instrument cache configuration.
        CacheConfiguration<String, Instrument> instCfg = new CacheConfiguration<>("instCache");
        // Index key and value for querying financial instruments.
        // Note that Instrument class has @QuerySqlField annotation for secondary field indexing.
        instCfg.setIndexedTypes(String.class, Instrument.class);
        // Auto-close caches at the end of the example.
        try (IgniteCache<String, Double> mktCache = ignite.getOrCreateCache(mktDataCfg);
            IgniteCache<String, Instrument> instCache = ignite.getOrCreateCache(instCfg)) {
            try (IgniteDataStreamer<String, Double> mktStmr = ignite.dataStreamer(mktCache.getName())) {
                // Note that we receive market data, but do not populate 'mktCache' (it remains empty).
                // Instead we update the instruments in the 'instCache'.
                // Since both, 'instCache' and 'mktCache' use the same key, updates are collocated.
                mktStmr.receiver(StreamVisitor.from((cache, e) -> {
                    String symbol = e.getKey();
                    Double tick = e.getValue();
                    Instrument inst = instCache.get(symbol);
                    if (inst == null)
                        inst = new Instrument(symbol);
                    // Don't populate market cache, as we don't use it for querying.
                    // Update cached instrument based on the latest market tick.
                    inst.update(tick);
                    instCache.put(symbol, inst);
                }));
                // Stream 10 million market data ticks into the system.
                for (int i = 1; i <= 10_000_000; i++) {
                    int idx = RAND.nextInt(INSTRUMENTS.length);
                    // Use gaussian distribution to ensure that
                    // numbers closer to 0 have higher probability.
                    double price = round2(INITIAL_PRICES[idx] + RAND.nextGaussian());
                    mktStmr.addData(INSTRUMENTS[idx], price);
                    if (i % 500_000 == 0)
                        System.out.println("Number of tuples streamed into Ignite: " + i);
                }
            }
            // Select top 3 best performing instruments.
            SqlFieldsQuery top3qry = new SqlFieldsQuery("select symbol, (latest - open) from Instrument order by (latest - open) desc limit 3");
            // Execute queries.
            List<List<?>> top3 = instCache.query(top3qry).getAll();
            System.out.println("Top performing financial instruments: ");
            // Print top 10 words.
            ExamplesUtils.printQueryResults(top3);
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(mktDataCfg.getName());
            ignite.destroyCache(instCfg.getName());
        }
    }
}
Also used : QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) ExamplesUtils(org.apache.ignite.examples.ExamplesUtils) Random(java.util.Random) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) StreamVisitor(org.apache.ignite.stream.StreamVisitor) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) List(java.util.List) Ignition(org.apache.ignite.Ignition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 14 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration 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.");
        CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>();
        cfg.setCacheMode(CacheMode.PARTITIONED);
        cfg.setName(CACHE_NAME);
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(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();
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 15 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration in project ignite by apache.

the class CacheAsyncApiExample 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 asynchronous API example started.");
        CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>();
        cfg.setCacheMode(CacheMode.PARTITIONED);
        cfg.setName(CACHE_NAME);
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cfg)) {
            // Enable asynchronous mode.
            IgniteCache<Integer, String> asyncCache = cache.withAsync();
            Collection<IgniteFuture<?>> futs = new ArrayList<>();
            // Execute several puts asynchronously.
            for (int i = 0; i < 10; i++) {
                asyncCache.put(i, String.valueOf(i));
                futs.add(asyncCache.future());
            }
            // Wait for completion of all futures.
            futs.forEach(IgniteFuture::get);
            // Execute get operation asynchronously.
            asyncCache.get(1);
            // Asynchronously wait for result.
            asyncCache.<String>future().listen(fut -> System.out.println("Get operation completed [value=" + fut.get() + ']'));
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1234 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)474 TcpDiscoverySpi (org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)330 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)292 Ignite (org.apache.ignite.Ignite)202 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)127 ArrayList (java.util.ArrayList)95 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)82 IgniteEx (org.apache.ignite.internal.IgniteEx)60 QueryEntity (org.apache.ignite.cache.QueryEntity)59 IgniteCache (org.apache.ignite.IgniteCache)55 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)54 CacheException (javax.cache.CacheException)51 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)51 IgniteException (org.apache.ignite.IgniteException)50 DataRegionConfiguration (org.apache.ignite.configuration.DataRegionConfiguration)45 LinkedHashMap (java.util.LinkedHashMap)41 IgfsGroupDataBlocksKeyMapper (org.apache.ignite.igfs.IgfsGroupDataBlocksKeyMapper)41 TcpDiscoveryVmIpFinder (org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder)38 Transaction (org.apache.ignite.transactions.Transaction)35