Search in sources :

Example 1 with BinaryObjectBuilder

use of org.apache.ignite.binary.BinaryObjectBuilder 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;
        // Financial instrument cache configuration.
        CacheConfiguration<String, Instrument> instCfg = new CacheConfiguration<>(INSTRUMENTS_CACHE_NAME);
        // 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 (// Default config.
        IgniteCache<String, Double> mktCache = ignite.getOrCreateCache(MARKET_TICKS_CACHE_NAME);
            IgniteCache<String, Instrument> instCache = ignite.getOrCreateCache(instCfg)) {
            try (IgniteDataStreamer<String, Double> mktStmr = ignite.dataStreamer(mktCache.getName())) {
                // To achieve proper indexing we should use fully-qualified name
                // of the class as a type name when binary object is created.
                final String instTypeName = Instrument.class.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(new StreamVisitor<String, Double>() {

                    @Override
                    public void apply(IgniteCache<String, Double> cache, Map.Entry<String, Double> e) {
                        String symbol = e.getKey();
                        Double tick = e.getValue();
                        IgniteCache<String, BinaryObject> binInstCache = ignite.cache(INSTRUMENTS_CACHE_NAME).withKeepBinary();
                        BinaryObject inst = binInstCache.get(symbol);
                        BinaryObjectBuilder instBuilder;
                        if (inst == null) {
                            instBuilder = ignite.binary().builder(instTypeName);
                            // Constructor logic.
                            instBuilder.setField("symbol", symbol);
                        } else
                            instBuilder = inst.toBuilder();
                        // Instrument.update() logic.
                        Double open = instBuilder.<Double>getField("open");
                        if (open == null || open == 0)
                            instBuilder.setField("open", tick);
                        instBuilder.setField("latest", tick);
                        // Build instrument object.
                        inst = instBuilder.build();
                        binInstCache.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(INSTRUMENTS_CACHE_NAME);
            ignite.destroyCache(MARKET_TICKS_CACHE_NAME);
        }
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite) List(java.util.List) Map(java.util.Map) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 2 with BinaryObjectBuilder

use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.

the class AbstractJdbcPojoQuerySelfTest method beforeTestsStarted.

/** {@inheritDoc} */
@Override
protected void beforeTestsStarted() throws Exception {
    Ignite ignite = startGrid(0);
    BinaryObjectBuilder builder = ignite.binary().builder(TEST_OBJECT);
    BinaryObjectBuilder builder2 = ignite.binary().builder(TEST_OBJECT_2);
    builder2.setField("id", 1);
    builder2.setField("boolVal", true);
    BinaryObject testObject = builder2.build();
    builder.setField("id", 1);
    builder.setField("testObject", testObject);
    BinaryObject binObj = builder.build();
    IgniteCache<String, BinaryObject> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    cache.put("0", binObj);
    Class.forName("org.apache.ignite.IgniteJdbcDriver");
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 3 with BinaryObjectBuilder

use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.

the class CacheJdbcPojoStore method buildBinaryObject.

/**
     * Construct binary object from query result.
     *
     * @param typeName Type name.
     * @param fields Fields descriptors.
     * @param loadColIdxs Select query columns index.
     * @param rs ResultSet.
     * @return Constructed binary object.
     * @throws CacheLoaderException If failed to construct binary object.
     */
protected Object buildBinaryObject(String typeName, JdbcTypeField[] fields, Map<String, Integer> loadColIdxs, ResultSet rs) throws CacheLoaderException {
    try {
        BinaryObjectBuilder builder = ignite.binary().builder(typeName);
        for (JdbcTypeField field : fields) {
            Integer colIdx = columnIndex(loadColIdxs, field.getDatabaseFieldName());
            Object colVal = transformer.getColumnValue(rs, colIdx, field.getJavaFieldType());
            builder.setField(field.getJavaFieldName(), colVal, (Class<Object>) field.getJavaFieldType());
        }
        return builder.build();
    } catch (SQLException e) {
        throw new CacheException("Failed to read binary object: " + typeName, e);
    }
}
Also used : SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 4 with BinaryObjectBuilder

use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.

the class BinarySerialiedFieldComparatorSelfTest method build.

/**
     * Build object.
     *
     * @param parts Parts.
     * @return Result.
     */
private BinaryObjectImpl build(Object... parts) {
    String typeName = "Type" + TYPE_CTR.get();
    BinaryObjectBuilder builder = grid().binary().builder(typeName);
    if (!F.isEmpty(parts)) {
        for (int i = 0; i < parts.length; ) builder.setField((String) parts[i++], parts[i++]);
    }
    return (BinaryObjectImpl) builder.build();
}
Also used : BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 5 with BinaryObjectBuilder

use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.

the class BinaryObjectBuilderDefaultMappersSelfTest method testStringField.

/**
     * @throws Exception If failed.
     */
public void testStringField() throws Exception {
    BinaryObjectBuilder builder = builder("Class");
    builder.setField("stringField", "str");
    BinaryObject po = builder.build();
    assertEquals(expectedHashCode("Class"), po.type().typeId());
    assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode());
    assertEquals("str", po.<String>field("stringField"));
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Aggregations

BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)77 BinaryObject (org.apache.ignite.binary.BinaryObject)60 Ignite (org.apache.ignite.Ignite)11 IgniteBinary (org.apache.ignite.IgniteBinary)7 GridBinaryTestClasses (org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses)7 Date (java.util.Date)4 BinaryType (org.apache.ignite.binary.BinaryType)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Field (java.lang.reflect.Field)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 IgniteCache (org.apache.ignite.IgniteCache)2 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2