Search in sources :

Example 1 with QuerySqlField

use of org.apache.ignite.cache.query.annotations.QuerySqlField 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 2 with QuerySqlField

use of org.apache.ignite.cache.query.annotations.QuerySqlField in project ignite by apache.

the class CacheConfiguration method processAnnotationsInClass.

/**
     * Process annotations for class.
     *
     * @param key If given class relates to key.
     * @param cls Class.
     * @param type Type descriptor.
     * @param parent Parent in case of embeddable.
     */
private static void processAnnotationsInClass(boolean key, Class<?> cls, TypeDescriptor type, @Nullable ClassProperty parent) {
    if (U.isJdk(cls) || QueryUtils.isGeometryClass(cls)) {
        if (parent == null && !key && QueryUtils.isSqlType(cls)) {
            // We have to index primitive _val.
            String idxName = cls.getSimpleName() + "_" + QueryUtils.VAL_FIELD_NAME + "_idx";
            type.addIndex(idxName, QueryUtils.isGeometryClass(cls) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
            type.addFieldToIndex(idxName, QueryUtils.VAL_FIELD_NAME, 0, false);
        }
        return;
    }
    if (parent != null && parent.knowsClass(cls))
        throw new CacheException("Recursive reference found in type: " + cls.getName());
    if (parent == null) {
        // Check class annotation at top level only.
        QueryTextField txtAnnCls = cls.getAnnotation(QueryTextField.class);
        if (txtAnnCls != null)
            type.valueTextIndex(true);
        QueryGroupIndex grpIdx = cls.getAnnotation(QueryGroupIndex.class);
        if (grpIdx != null)
            type.addIndex(grpIdx.name(), QueryIndexType.SORTED);
        QueryGroupIndex.List grpIdxList = cls.getAnnotation(QueryGroupIndex.List.class);
        if (grpIdxList != null && !F.isEmpty(grpIdxList.value())) {
            for (QueryGroupIndex idx : grpIdxList.value()) type.addIndex(idx.name(), QueryIndexType.SORTED);
        }
    }
    for (Class<?> c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) {
        for (Field field : c.getDeclaredFields()) {
            QuerySqlField sqlAnn = field.getAnnotation(QuerySqlField.class);
            QueryTextField txtAnn = field.getAnnotation(QueryTextField.class);
            if (sqlAnn != null || txtAnn != null) {
                ClassProperty prop = new ClassProperty(field);
                prop.parent(parent);
                // Add parent property before its possible nested properties so that
                // resulting parent column comes before columns corresponding to those
                // nested properties in the resulting table - that way nested
                // properties override will happen properly (first parent, then children).
                type.addProperty(prop, key, true);
                processAnnotation(key, sqlAnn, txtAnn, cls, c, field.getType(), prop, type);
            }
        }
    }
}
Also used : QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) Field(java.lang.reflect.Field) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QueryGroupIndex(org.apache.ignite.cache.query.annotations.QueryGroupIndex) CacheException(javax.cache.CacheException)

Example 3 with QuerySqlField

use of org.apache.ignite.cache.query.annotations.QuerySqlField in project ignite by apache.

the class PersistenceSettings method detectPojoFields.

/**
 * Extracts POJO fields from a list of corresponding XML field nodes.
 *
 * @param fieldNodes Field nodes to process.
 * @return POJO fields list.
 */
protected List<F> detectPojoFields(NodeList fieldNodes) {
    List<F> detectedFields = new LinkedList<>();
    if (fieldNodes != null && fieldNodes.getLength() != 0) {
        int cnt = fieldNodes.getLength();
        for (int i = 0; i < cnt; i++) {
            F field = createPojoField((Element) fieldNodes.item(i), getJavaClass());
            // Just checking that such field exists in the class
            PropertyMappingHelper.getPojoFieldAccessor(getJavaClass(), field.getName());
            detectedFields.add(field);
        }
        return detectedFields;
    }
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(getJavaClass());
    // Collecting Java Beans property descriptors
    if (descriptors != null) {
        for (PropertyDescriptor desc : descriptors) {
            // Skip POJO field if it's read-only
            if (desc.getWriteMethod() != null) {
                Field field = null;
                try {
                    field = getJavaClass().getDeclaredField(desc.getName());
                } catch (Throwable ignore) {
                }
                detectedFields.add(createPojoField(new PojoFieldAccessor(desc, field)));
            }
        }
    }
    Field[] fields = getJavaClass().getDeclaredFields();
    // Collecting all fields annotated with @QuerySqlField
    if (fields != null) {
        for (Field field : fields) {
            if (field.getAnnotation(QuerySqlField.class) != null && !PojoField.containsField(detectedFields, field.getName()))
                detectedFields.add(createPojoField(new PojoFieldAccessor(field)));
        }
    }
    return detectedFields;
}
Also used : QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) Field(java.lang.reflect.Field) PropertyDescriptor(java.beans.PropertyDescriptor) LinkedList(java.util.LinkedList)

Example 4 with QuerySqlField

use of org.apache.ignite.cache.query.annotations.QuerySqlField in project ignite by apache.

the class StreamVisitorExample method main.

/**
 * @param args Command line arguments.
 */
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 5 with QuerySqlField

use of org.apache.ignite.cache.query.annotations.QuerySqlField in project ignite by apache.

the class QueryEntity method processAnnotationsInClass.

/**
 * Process annotations for class.
 *
 * @param key If given class relates to key.
 * @param cls Class.
 * @param type Type descriptor.
 * @param parent Parent in case of embeddable.
 */
private static void processAnnotationsInClass(boolean key, Class<?> cls, QueryEntityTypeDescriptor type, @Nullable QueryEntityClassProperty parent) {
    if (U.isJdk(cls) || QueryUtils.isGeometryClass(cls)) {
        if (parent == null && !key && QueryUtils.isSqlType(cls)) {
            // We have to index primitive _val.
            String idxName = cls.getSimpleName() + "_" + QueryUtils.VAL_FIELD_NAME + "_idx";
            type.addIndex(idxName, QueryUtils.isGeometryClass(cls) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED, QueryIndex.DFLT_INLINE_SIZE);
            type.addFieldToIndex(idxName, QueryUtils.VAL_FIELD_NAME, 0, false);
        }
        return;
    }
    if (parent != null && parent.knowsClass(cls))
        throw new CacheException("Recursive reference found in type: " + cls.getName());
    if (parent == null) {
        // Check class annotation at top level only.
        QueryTextField txtAnnCls = cls.getAnnotation(QueryTextField.class);
        if (txtAnnCls != null)
            type.valueTextIndex(true);
        QueryGroupIndex grpIdx = cls.getAnnotation(QueryGroupIndex.class);
        if (grpIdx != null)
            type.addIndex(grpIdx.name(), QueryIndexType.SORTED, grpIdx.inlineSize());
        QueryGroupIndex.List grpIdxList = cls.getAnnotation(QueryGroupIndex.List.class);
        if (grpIdxList != null && !F.isEmpty(grpIdxList.value())) {
            for (QueryGroupIndex idx : grpIdxList.value()) type.addIndex(idx.name(), QueryIndexType.SORTED, idx.inlineSize());
        }
    }
    for (Class<?> c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) {
        for (Field field : c.getDeclaredFields()) {
            QuerySqlField sqlAnn = field.getAnnotation(QuerySqlField.class);
            QueryTextField txtAnn = field.getAnnotation(QueryTextField.class);
            if (sqlAnn != null || txtAnn != null) {
                QueryEntityClassProperty prop = new QueryEntityClassProperty(field);
                prop.parent(parent);
                // Add parent property before its possible nested properties so that
                // resulting parent column comes before columns corresponding to those
                // nested properties in the resulting table - that way nested
                // properties override will happen properly (first parent, then children).
                type.addProperty(prop, sqlAnn, key, true);
                processAnnotation(key, sqlAnn, txtAnn, cls, c, field.getType(), prop, type);
            }
        }
    }
}
Also used : QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) QueryField(org.apache.ignite.internal.processors.query.QueryField) Field(java.lang.reflect.Field) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QueryGroupIndex(org.apache.ignite.cache.query.annotations.QueryGroupIndex) CacheException(javax.cache.CacheException) QueryEntityClassProperty(org.apache.ignite.internal.processors.cache.query.QueryEntityClassProperty)

Aggregations

QuerySqlField (org.apache.ignite.cache.query.annotations.QuerySqlField)5 Field (java.lang.reflect.Field)3 Serializable (java.io.Serializable)2 List (java.util.List)2 Random (java.util.Random)2 CacheException (javax.cache.CacheException)2 Ignite (org.apache.ignite.Ignite)2 IgniteCache (org.apache.ignite.IgniteCache)2 IgniteDataStreamer (org.apache.ignite.IgniteDataStreamer)2 Ignition (org.apache.ignite.Ignition)2 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)2 QueryGroupIndex (org.apache.ignite.cache.query.annotations.QueryGroupIndex)2 QueryTextField (org.apache.ignite.cache.query.annotations.QueryTextField)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 ExampleNodeStartup (org.apache.ignite.examples.ExampleNodeStartup)2 ExamplesUtils (org.apache.ignite.examples.ExamplesUtils)2 StreamVisitor (org.apache.ignite.stream.StreamVisitor)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 LinkedList (java.util.LinkedList)1 QueryEntityClassProperty (org.apache.ignite.internal.processors.cache.query.QueryEntityClassProperty)1