Search in sources :

Example 41 with QueryEntity

use of org.apache.ignite.cache.QueryEntity in project ignite by apache.

the class IgniteCacheAbstractFieldsQuerySelfTest method testExecuteWithMetaDataAndCustomKeyPrecision.

/**
 */
@Test
public void testExecuteWithMetaDataAndCustomKeyPrecision() throws Exception {
    QueryEntity qeWithPrecision = new QueryEntity().setKeyType("java.lang.String").setKeyFieldName("my_key").setValueType("CustomKeyType").addQueryField("my_key", "java.lang.String", "my_key").addQueryField("strField", "java.lang.String", "strField").setFieldsPrecision(ImmutableMap.of("strField", 999, "my_key", 777));
    grid(0).getOrCreateCache(cacheConfiguration().setName("cacheWithCustomKeyPrecision").setQueryEntities(Collections.singleton(qeWithPrecision)));
    GridQueryProcessor qryProc = grid(0).context().query();
    qryProc.querySqlFields(new SqlFieldsQuery("INSERT INTO CustomKeyType(my_key, strField) VALUES(?, ?)").setSchema("cacheWithCustomKeyPrecision").setArgs("1", "ABC"), true);
    qryProc.querySqlFields(new SqlFieldsQuery("INSERT INTO CustomKeyType(my_key, strField) VALUES(?, ?)").setSchema("cacheWithCustomKeyPrecision").setArgs("2", "DEF"), true);
    QueryCursorImpl<List<?>> cursor = (QueryCursorImpl<List<?>>) qryProc.querySqlFields(new SqlFieldsQuery("SELECT my_key, strField FROM CustomKeyType").setSchema("cacheWithCustomKeyPrecision"), true);
    List<GridQueryFieldMetadata> fieldsMeta = cursor.fieldsMeta();
    int fldCnt = 0;
    for (GridQueryFieldMetadata meta : fieldsMeta) {
        switch(meta.fieldName()) {
            case "STRFIELD":
                assertEquals(999, meta.precision());
                fldCnt++;
                break;
            case "MY_KEY":
                assertEquals(777, meta.precision());
                fldCnt++;
                break;
            default:
                fail("Unknown field - " + meta.fieldName());
        }
    }
    assertEquals("Metadata for all fields should be returned.", 2, fldCnt);
}
Also used : GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) ArrayList(java.util.ArrayList) List(java.util.List) QueryEntity(org.apache.ignite.cache.QueryEntity) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 42 with QueryEntity

use of org.apache.ignite.cache.QueryEntity in project ignite by apache.

the class IgniteCacheAbstractFieldsQuerySelfTest method testDecimalColumnScaleAndPrecision.

/**
 * Test that scale and precision returned correctly for Decimal column in result set:
 *
 * 1. Start node;
 * 2. Create table with Decimal(3,0) column;
 * 3. Insert a new row into the table;
 * 4. Execute select with decimal row;
 * 5. Check that selected decimal column has precision 3 and scale 0.
 *
 * @throws Exception If failed.
 */
@Test
public void testDecimalColumnScaleAndPrecision() throws Exception {
    QueryEntity qeWithPrecision = new QueryEntity().setKeyType("java.lang.Long").setValueType("TestType").addQueryField("age", "java.math.BigDecimal", "age").setFieldsPrecision(ImmutableMap.of("age", 3)).setFieldsScale(ImmutableMap.of("age", 0));
    grid(0).getOrCreateCache(cacheConfiguration().setName("cacheWithDecimalPrecisionAndScale").setQueryEntities(Collections.singleton(qeWithPrecision)));
    GridQueryProcessor qryProc = grid(0).context().query();
    qryProc.querySqlFields(new SqlFieldsQuery("INSERT INTO TestType(_key, age) VALUES(?, ?)").setSchema("cacheWithDecimalPrecisionAndScale").setArgs(1, new BigDecimal(160)), true);
    QueryCursorImpl<List<?>> cursor = (QueryCursorImpl<List<?>>) qryProc.querySqlFields(new SqlFieldsQuery("SELECT age FROM TestType").setSchema("cacheWithDecimalPrecisionAndScale"), true);
    List<GridQueryFieldMetadata> fieldsMeta = cursor.fieldsMeta();
    assertEquals(1, fieldsMeta.size());
    GridQueryFieldMetadata meta = fieldsMeta.get(0);
    assertEquals(3, meta.precision());
    assertEquals(0, meta.scale());
}
Also used : GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) ArrayList(java.util.ArrayList) List(java.util.List) QueryEntity(org.apache.ignite.cache.QueryEntity) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) BigDecimal(java.math.BigDecimal) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 43 with QueryEntity

use of org.apache.ignite.cache.QueryEntity in project ignite by apache.

the class IgniteCacheAbstractQuerySelfTest method cacheConfiguration.

/**
 * @return cache configuration
 */
protected CacheConfiguration cacheConfiguration() {
    CacheConfiguration cc = defaultCacheConfiguration();
    cc.setCacheMode(cacheMode());
    cc.setAtomicityMode(atomicityMode());
    cc.setNearConfiguration(nearCacheConfiguration());
    cc.setWriteSynchronizationMode(FULL_SYNC);
    cc.setCacheStoreFactory(new StoreFactory());
    cc.setReadThrough(true);
    cc.setWriteThrough(true);
    cc.setLoadPreviousValue(true);
    cc.setRebalanceMode(SYNC);
    cc.setSqlFunctionClasses(SqlFunctions.class);
    List<QueryEntity> entityList = new ArrayList<>();
    QueryEntity qryEntity = new QueryEntity();
    qryEntity.setKeyType(Integer.class.getName());
    qryEntity.setValueType(Type1.class.getName());
    qryEntity.addQueryField("id", Integer.class.getName(), null);
    qryEntity.addQueryField("name", String.class.getName(), null);
    qryEntity.setTableName("Type2");
    qryEntity.setIndexes(Arrays.asList(new QueryIndex("id").setName("index~!@#$%^&*()_=-+;[]{}|?,.*`:nameWithNonLetterSymbols")));
    entityList.add(qryEntity);
    qryEntity = new QueryEntity();
    qryEntity.setKeyType(Integer.class.getName());
    qryEntity.setValueType(Type2.class.getName());
    qryEntity.addQueryField("id", Integer.class.getName(), null);
    qryEntity.addQueryField("name", String.class.getName(), null);
    qryEntity.setTableName("Type1");
    qryEntity.setIndexes(Arrays.asList(new QueryIndex("id")));
    entityList.add(qryEntity);
    qryEntity = new QueryEntity();
    qryEntity.setKeyType(Integer.class.getName());
    qryEntity.setValueType(ObjectValue2.class.getName());
    qryEntity.addQueryField("strVal", String.class.getName(), null);
    // Default index type
    final QueryIndex strIdx = new QueryIndex();
    strIdx.setFieldNames(Collections.singletonList("strVal"), true);
    qryEntity.setIndexes(Arrays.asList(strIdx));
    entityList.add(qryEntity);
    cc.setQueryEntities(entityList);
    if (cacheMode() != CacheMode.LOCAL)
        cc.setAffinity(new RendezvousAffinityFunction());
    // Explicitly set number of backups equal to number of grids.
    if (cacheMode() == CacheMode.PARTITIONED)
        cc.setBackups(gridCount());
    return cc;
}
Also used : ArrayList(java.util.ArrayList) QueryIndex(org.apache.ignite.cache.QueryIndex) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) QueryEntity(org.apache.ignite.cache.QueryEntity) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 44 with QueryEntity

use of org.apache.ignite.cache.QueryEntity in project ignite by apache.

the class IgniteCacheDistributedJoinCustomAffinityMapper method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    List<CacheConfiguration> ccfgs = new ArrayList<>();
    {
        CacheConfiguration ccfg = configuration(PERSON_CACHE);
        QueryEntity entity = new QueryEntity();
        entity.setKeyType(Integer.class.getName());
        entity.setValueType(Person.class.getName());
        entity.addQueryField("orgId", Integer.class.getName(), null);
        entity.setIndexes(F.asList(new QueryIndex("orgId")));
        ccfg.setQueryEntities(F.asList(entity));
        ccfgs.add(ccfg);
    }
    {
        CacheConfiguration ccfg = configuration(PERSON_CACHE_CUSTOM_AFF);
        ccfg.setAffinityMapper(new TestMapper());
        QueryEntity entity = new QueryEntity();
        entity.setKeyType(Integer.class.getName());
        entity.setValueType(Person.class.getName());
        entity.addQueryField("orgId", Integer.class.getName(), null);
        entity.setIndexes(F.asList(new QueryIndex("orgId")));
        ccfg.setQueryEntities(F.asList(entity));
        ccfgs.add(ccfg);
    }
    {
        CacheConfiguration ccfg = configuration(ORG_CACHE);
        QueryEntity entity = new QueryEntity();
        entity.setKeyType(Integer.class.getName());
        entity.setValueType(Organization.class.getName());
        ccfg.setQueryEntities(F.asList(entity));
        ccfgs.add(ccfg);
    }
    {
        CacheConfiguration ccfg = configuration(ORG_CACHE_REPL_CUSTOM);
        ccfg.setCacheMode(REPLICATED);
        ccfg.setAffinityMapper(new TestMapper());
        QueryEntity entity = new QueryEntity();
        entity.setKeyType(Integer.class.getName());
        entity.setValueType(Organization.class.getName());
        ccfg.setQueryEntities(F.asList(entity));
        ccfgs.add(ccfg);
    }
    cfg.setCacheConfiguration(ccfgs.toArray(new CacheConfiguration[ccfgs.size()]));
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ArrayList(java.util.ArrayList) QueryIndex(org.apache.ignite.cache.QueryIndex) QueryEntity(org.apache.ignite.cache.QueryEntity) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 45 with QueryEntity

use of org.apache.ignite.cache.QueryEntity in project ignite by apache.

the class IgniteCacheFullTextQueryNodeJoiningSelfTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    CacheConfiguration cache = new CacheConfiguration(DEFAULT_CACHE_NAME);
    cache.setCacheMode(PARTITIONED);
    cache.setAtomicityMode(atomicityMode());
    cache.setWriteSynchronizationMode(FULL_SYNC);
    cache.setBackups(1);
    cache.setRebalanceMode(CacheRebalanceMode.SYNC);
    QueryEntity qryEntity = new QueryEntity();
    qryEntity.setKeyType(AffinityKey.class.getName());
    qryEntity.setValueType(IndexedEntity.class.getName());
    LinkedHashMap<String, String> fields = new LinkedHashMap<>();
    fields.put("val", String.class.getName());
    qryEntity.setFields(fields);
    qryEntity.setIndexes(Arrays.asList(new QueryIndex("val", QueryIndexType.FULLTEXT)));
    cache.setQueryEntities(Arrays.asList(qryEntity));
    cfg.setCacheConfiguration(cache);
    TcpCommunicationSpi commSpi = new TcpCommunicationSpi();
    commSpi.setSharedMemoryPort(-1);
    cfg.setCommunicationSpi(commSpi);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) QueryIndex(org.apache.ignite.cache.QueryIndex) QueryEntity(org.apache.ignite.cache.QueryEntity) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) TcpCommunicationSpi(org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

QueryEntity (org.apache.ignite.cache.QueryEntity)221 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)115 QueryIndex (org.apache.ignite.cache.QueryIndex)87 LinkedHashMap (java.util.LinkedHashMap)83 ArrayList (java.util.ArrayList)53 Test (org.junit.Test)42 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)41 HashMap (java.util.HashMap)27 HashSet (java.util.HashSet)22 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)21 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)21 CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)19 List (java.util.List)18 Ignite (org.apache.ignite.Ignite)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)13 Map (java.util.Map)12 BinaryObject (org.apache.ignite.binary.BinaryObject)12 DataRegionConfiguration (org.apache.ignite.configuration.DataRegionConfiguration)12 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)12