Search in sources :

Example 1 with Query

use of org.apache.gora.query.Query in project gora by apache.

the class TestCassandraStore method testDeleteByQueryFields.

@Test
public void testDeleteByQueryFields() throws Exception {
    preConfiguration();
    log.info("test method: testQueryByQueryFields");
    // test 5 - delete all with some fields
    WebPageDataCreator.createWebPageData(webPageStore);
    Query query = webPageStore.newQuery();
    query.setFields("outlinks", "parsedContent", "content");
    for (String SORTED_URL : SORTED_URLS) {
        query.setKey(SORTED_URL);
        webPageStore.deleteByQuery(query);
        WebPage page = webPageStore.get(SORTED_URL);
        assertNotNull(page);
        assertNotNull(page.getUrl());
        assertEquals(page.getUrl().toString(), SORTED_URL);
        assertEquals("Map of Outlinks should have a size of '0' as the deleteByQuery " + "not only removes the data but also the data structure.", 0, page.getOutlinks().size());
        assertEquals(0, page.getParsedContent().size());
        if (page.getContent() != null) {
            LOG.info("url:" + page.getUrl().toString());
            LOG.info("limit:" + page.getContent().limit());
        } else {
            assertNull(page.getContent());
        }
    }
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Query(org.apache.gora.query.Query) Test(org.junit.Test)

Example 2 with Query

use of org.apache.gora.query.Query in project gora by apache.

the class JetTest method testNewJetSource.

@Test
public void testNewJetSource() throws Exception {
    DataStore<Long, Pageview> dataStoreIn;
    dataStoreIn = DataStoreFactory.getDataStore(Long.class, Pageview.class, utility.getConfiguration());
    dataStoreOut = DataStoreFactory.getDataStore(Long.class, ResultPageView.class, utility.getConfiguration());
    query = dataStoreIn.newQuery();
    query.setStartKey(0L);
    query.setEndKey(55L);
    JetEngine<Long, Pageview, Long, ResultPageView> jetEngine = new JetEngine<>();
    BatchSource<JetInputOutputFormat<Long, Pageview>> fileSource = jetEngine.createDataSource(dataStoreIn, query);
    Pipeline p = Pipeline.create();
    p.drawFrom(fileSource).filter(item -> item.getValue().getIp().toString().equals("88.240.129.183")).map(e -> {
        ResultPageView resultPageView = new ResultPageView();
        resultPageView.setIp(e.getValue().getIp());
        resultPageView.setTimestamp(e.getValue().getTimestamp());
        resultPageView.setUrl(e.getValue().getUrl());
        return new JetInputOutputFormat<Long, ResultPageView>(e.getValue().getTimestamp(), resultPageView);
    }).drainTo(jetEngine.createDataSink(dataStoreOut));
    JetInstance jet = Jet.newJetInstance();
    Jet.newJetInstance();
    try {
        jet.newJob(p).join();
    } finally {
        Jet.shutdownAll();
    }
    Query<Long, ResultPageView> query = dataStoreOut.newQuery();
    Result<Long, ResultPageView> result = query.execute();
    int noOfOutputRecords = 0;
    String ip = "";
    while (result.next()) {
        noOfOutputRecords++;
        ip = result.get().getIp().toString();
        assertEquals("88.240.129.183", ip);
    }
    assertEquals(2, noOfOutputRecords);
}
Also used : AggregateOperations.counting(com.hazelcast.jet.aggregate.AggregateOperations.counting) JetInstance(com.hazelcast.jet.JetInstance) BatchSource(com.hazelcast.jet.pipeline.BatchSource) BeforeClass(org.junit.BeforeClass) Pipeline(com.hazelcast.jet.pipeline.Pipeline) DataStore(org.apache.gora.store.DataStore) Functions.wholeItem(com.hazelcast.jet.function.Functions.wholeItem) Sinks(com.hazelcast.jet.pipeline.Sinks) Test(org.junit.Test) Pageview(org.apache.gora.jet.generated.Pageview) ResultPageView(org.apache.gora.jet.generated.ResultPageView) Result(org.apache.gora.query.Result) DataStoreFactory(org.apache.gora.store.DataStoreFactory) IMap(com.hazelcast.core.IMap) HBaseTestingUtility(org.apache.hadoop.hbase.HBaseTestingUtility) Query(org.apache.gora.query.Query) Traversers.traverseArray(com.hazelcast.jet.Traversers.traverseArray) Jet(com.hazelcast.jet.Jet) Pattern(java.util.regex.Pattern) GoraException(org.apache.gora.util.GoraException) Assert.assertEquals(org.junit.Assert.assertEquals) JetInstance(com.hazelcast.jet.JetInstance) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Pageview(org.apache.gora.jet.generated.Pageview) ResultPageView(org.apache.gora.jet.generated.ResultPageView) Test(org.junit.Test)

Example 3 with Query

use of org.apache.gora.query.Query in project camel by apache.

the class GoraConsumerTest method consumerInstantiationWithMocksShouldSucceed.

@Test
public void consumerInstantiationWithMocksShouldSucceed() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    final Query mockQuery = mock(Query.class);
    when(mockDatastore.newQuery()).thenReturn(mockQuery);
    GoraConsumer goraConsumer = new GoraConsumer(mockGoraEndpoint, mockGoraProcessor, mockGoraConfiguration, mockDatastore);
}
Also used : Query(org.apache.gora.query.Query) Test(org.junit.Test)

Example 4 with Query

use of org.apache.gora.query.Query in project gora by apache.

the class RedisStore method deleteByQuery.

@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
    Collection<K> range = runQuery(query);
    RBatch batchInstance = redisInstance.createBatch();
    RLexSortedSetAsync secundaryIndex = batchInstance.getLexSortedSet(generateIndexKey());
    if (query.getFields() != null && query.getFields().length < mapping.getFields().size()) {
        List<String> dbFields = new ArrayList<>();
        List<RedisType> dbTypes = new ArrayList<>();
        for (String af : query.getFields()) {
            dbFields.add(mapping.getFields().get(af));
            dbTypes.add(mapping.getTypes().get(af));
        }
        for (K key : range) {
            if (mode == StorageMode.SINGLEKEY) {
                RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyHash(key));
                dbFields.forEach((field) -> {
                    map.removeAsync(field);
                });
            } else {
                for (int indexField = 0; indexField < dbFields.size(); indexField++) {
                    String field = dbFields.get(indexField);
                    RedisType type = dbTypes.get(indexField);
                    switch(type) {
                        case STRING:
                            RBucketAsync<Object> bucket = batchInstance.getBucket(generateKeyString(field, key));
                            bucket.deleteAsync();
                            break;
                        case LIST:
                            RListAsync<Object> rlist = batchInstance.getList(generateKeyString(field, key));
                            rlist.deleteAsync();
                            break;
                        case HASH:
                            RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyString(field, key));
                            map.deleteAsync();
                            break;
                        default:
                            throw new AssertionError(type.name());
                    }
                }
            }
        }
    } else {
        range.stream().map((key) -> {
            secundaryIndex.removeAsync(key);
            return key;
        }).forEachOrdered((key) -> {
            if (mode == StorageMode.SINGLEKEY) {
                RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyHash(key));
                map.deleteAsync();
            } else {
                redisInstance.getKeys().deleteByPattern(generateKeyStringBase(key) + WILDCARD);
            }
        });
    }
    batchInstance.execute();
    return range.size();
}
Also used : RLexSortedSetAsync(org.redisson.api.RLexSortedSetAsync) Config(org.redisson.config.Config) StorageMode(org.apache.gora.redis.util.StorageMode) LoggerFactory(org.slf4j.LoggerFactory) ReadMode(org.redisson.config.ReadMode) RedisResult(org.apache.gora.redis.query.RedisResult) Map(java.util.Map) RBatch(org.redisson.api.RBatch) GoraException(org.apache.gora.util.GoraException) Schema(org.apache.avro.Schema) RScoredSortedSetAsync(org.redisson.api.RScoredSortedSetAsync) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Result(org.apache.gora.query.Result) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) GORA_REDIS_READMODE(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_READMODE) List(java.util.List) RedisQuery(org.apache.gora.redis.query.RedisQuery) RListAsync(org.redisson.api.RListAsync) PartitionQueryImpl(org.apache.gora.query.impl.PartitionQueryImpl) RBucketAsync(org.redisson.api.RBucketAsync) RScoredSortedSet(org.redisson.api.RScoredSortedSet) RLexSortedSet(org.redisson.api.RLexSortedSet) PREFIX(org.apache.gora.redis.util.RedisStoreConstants.PREFIX) GORA_REDIS_STORAGE(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_STORAGE) RList(org.redisson.api.RList) DataStoreBase(org.apache.gora.store.impl.DataStoreBase) ArrayList(java.util.ArrayList) RFuture(org.redisson.api.RFuture) RMap(org.redisson.api.RMap) RBucket(org.redisson.api.RBucket) Charset(java.nio.charset.Charset) WILDCARD(org.apache.gora.redis.util.RedisStoreConstants.WILDCARD) PartitionQuery(org.apache.gora.query.PartitionQuery) START_TAG(org.apache.gora.redis.util.RedisStoreConstants.START_TAG) RMapAsync(org.redisson.api.RMapAsync) RedissonClient(org.redisson.api.RedissonClient) GORA_REDIS_MODE(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_MODE) Properties(java.util.Properties) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) END_TAG(org.apache.gora.redis.util.RedisStoreConstants.END_TAG) GORA_REDIS_MASTERNAME(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_MASTERNAME) IOException(java.io.IOException) INDEX(org.apache.gora.redis.util.RedisStoreConstants.INDEX) ServerMode(org.apache.gora.redis.util.ServerMode) ScoredEntry(org.redisson.client.protocol.ScoredEntry) Redisson(org.redisson.Redisson) FIELD_SEPARATOR(org.apache.gora.redis.util.RedisStoreConstants.FIELD_SEPARATOR) DatumHandler(org.apache.gora.redis.util.DatumHandler) GORA_REDIS_ADDRESS(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_ADDRESS) Query(org.apache.gora.query.Query) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) RLexSortedSetAsync(org.redisson.api.RLexSortedSetAsync) RBatch(org.redisson.api.RBatch)

Example 5 with Query

use of org.apache.gora.query.Query in project gora by apache.

the class OrientDBStore method get.

/**
 * {@inheritDoc}
 */
@Override
public T get(K key, String[] fields) throws GoraException {
    String[] dbFields = getFieldsToQuery(fields);
    com.github.raymanrt.orientqb.query.Query selectQuery = new com.github.raymanrt.orientqb.query.Query();
    for (String k : dbFields) {
        String dbFieldName = orientDBMapping.getDocumentField(k);
        if (dbFieldName != null && dbFieldName.length() > 0) {
            selectQuery.select(dbFieldName);
        }
    }
    selectQuery.from(orientDBMapping.getDocumentClass()).where(projection("_id").eq(Parameter.parameter("key")));
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("key", key);
    OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>(selectQuery.toString());
    try (ODatabaseDocumentTx selectTx = connectionPool.acquire()) {
        selectTx.activateOnCurrentThread();
        List<ODocument> result = selectTx.command(query).execute(params);
        if (result.size() == 1) {
            return convertOrientDocToAvroBean(result.get(0), dbFields);
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) PartitionQuery(org.apache.gora.query.PartitionQuery) OrientDBQuery(org.apache.gora.orientdb.query.OrientDBQuery) Query(org.apache.gora.query.Query) HashMap(java.util.HashMap) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) GoraException(org.apache.gora.util.GoraException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

Query (org.apache.gora.query.Query)7 GoraException (org.apache.gora.util.GoraException)4 Test (org.junit.Test)4 Result (org.apache.gora.query.Result)3 IMap (com.hazelcast.core.IMap)2 Jet (com.hazelcast.jet.Jet)2 JetInstance (com.hazelcast.jet.JetInstance)2 Traversers.traverseArray (com.hazelcast.jet.Traversers.traverseArray)2 AggregateOperations.counting (com.hazelcast.jet.aggregate.AggregateOperations.counting)2 Functions.wholeItem (com.hazelcast.jet.function.Functions.wholeItem)2 BatchSource (com.hazelcast.jet.pipeline.BatchSource)2 Pipeline (com.hazelcast.jet.pipeline.Pipeline)2 Sinks (com.hazelcast.jet.pipeline.Sinks)2 IOException (java.io.IOException)2 Pattern (java.util.regex.Pattern)2 Pageview (org.apache.gora.jet.generated.Pageview)2 ResultPageView (org.apache.gora.jet.generated.ResultPageView)2 PartitionQuery (org.apache.gora.query.PartitionQuery)2 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1