Search in sources :

Example 1 with TransformIterator

use of org.apache.commons.collections4.iterators.TransformIterator in project datawave by NationalSecurityAgency.

the class DashboardQueryLogic method getTransformIterator.

@Override
@SuppressWarnings("unchecked")
public TransformIterator getTransformIterator(Query settings) {
    TransformIterator origIter = super.getTransformIterator(settings);
    QueryLogicTransformer transformer = super.getTransformer(settings);
    DashboardSummary summary = new DashboardSummary(settings.getEndDate());
    while (origIter.hasNext()) {
        EventBase event = (EventBase) transformer.transform(origIter.next());
        DashboardFields.addEvent(summary, event);
    }
    return new TransformIterator(Arrays.asList(summary).iterator(), this);
}
Also used : EventBase(datawave.webservice.query.result.event.EventBase) TransformIterator(org.apache.commons.collections4.iterators.TransformIterator) DashboardSummary(datawave.webservice.query.dashboard.DashboardSummary) QueryLogicTransformer(datawave.webservice.query.logic.QueryLogicTransformer)

Example 2 with TransformIterator

use of org.apache.commons.collections4.iterators.TransformIterator in project datawave by NationalSecurityAgency.

the class GroupingTest method runTestQueryWithGrouping.

protected BaseQueryResponse runTestQueryWithGrouping(Map<String, Integer> expected, String querystr, Date startDate, Date endDate, Map<String, String> extraParms, Connector connector) throws Exception {
    log.debug("runTestQueryWithGrouping");
    QueryImpl settings = new QueryImpl();
    settings.setBeginDate(startDate);
    settings.setEndDate(endDate);
    settings.setPagesize(Integer.MAX_VALUE);
    settings.setQueryAuthorizations(auths.serialize());
    settings.setQuery(querystr);
    settings.setParameters(extraParms);
    settings.setId(UUID.randomUUID());
    log.debug("query: " + settings.getQuery());
    log.debug("logic: " + settings.getQueryLogicName());
    GenericQueryConfiguration config = logic.initialize(connector, settings, authSet);
    logic.setupQuery(config);
    DocumentTransformer transformer = (DocumentTransformer) (logic.getTransformer(settings));
    TransformIterator iter = new DatawaveTransformIterator(logic.iterator(), transformer);
    List<Object> eventList = new ArrayList<>();
    while (iter.hasNext()) {
        eventList.add(iter.next());
    }
    BaseQueryResponse response = transformer.createResponse(eventList);
    // un-comment to look at the json output
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
    mapper.writeValue(temporaryFolder.newFile(), response);
    Assert.assertTrue(response instanceof DefaultEventQueryResponse);
    DefaultEventQueryResponse eventQueryResponse = (DefaultEventQueryResponse) response;
    Assert.assertEquals("Got the wrong number of events", expected.size(), (long) eventQueryResponse.getReturnedEvents());
    for (EventBase event : eventQueryResponse.getEvents()) {
        String firstKey = "";
        String secondKey = "";
        Integer value = null;
        for (Object field : event.getFields()) {
            FieldBase fieldBase = (FieldBase) field;
            switch(fieldBase.getName()) {
                case "COUNT":
                    value = Integer.valueOf(fieldBase.getValueString());
                    break;
                case "GENDER":
                case "GEN":
                case "BIRTHDAY":
                    firstKey = fieldBase.getValueString();
                    break;
                case "AGE":
                case "AG":
                case "RECORD":
                    secondKey = fieldBase.getValueString();
                    break;
            }
        }
        log.debug("mapping is " + firstKey + "-" + secondKey + " count:" + value);
        String key;
        if (!firstKey.isEmpty() && !secondKey.isEmpty()) {
            key = firstKey + "-" + secondKey;
        } else if (!firstKey.isEmpty()) {
            key = firstKey;
        } else {
            key = secondKey;
        }
        Assert.assertEquals(expected.get(key), value);
    }
    return response;
}
Also used : EventBase(datawave.webservice.query.result.event.EventBase) DefaultEventQueryResponse(datawave.webservice.result.DefaultEventQueryResponse) TransformIterator(org.apache.commons.collections4.iterators.TransformIterator) DatawaveTransformIterator(datawave.webservice.query.iterator.DatawaveTransformIterator) DatawaveTransformIterator(datawave.webservice.query.iterator.DatawaveTransformIterator) ArrayList(java.util.ArrayList) GenericQueryConfiguration(datawave.webservice.query.configuration.GenericQueryConfiguration) QueryImpl(datawave.webservice.query.QueryImpl) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) FieldBase(datawave.webservice.query.result.event.FieldBase) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with TransformIterator

use of org.apache.commons.collections4.iterators.TransformIterator in project datawave by NationalSecurityAgency.

the class CompositeQueryLogicTest method testQueryLogicNoData.

@Test
public void testQueryLogicNoData() throws Exception {
    List<BaseQueryLogic<?>> logics = new ArrayList<>();
    TestQueryLogic logic1 = new TestQueryLogic();
    TestQueryLogic2 logic2 = new TestQueryLogic2();
    logics.add(logic1);
    logics.add(logic2);
    QueryImpl settings = new QueryImpl();
    settings.setPagesize(100);
    settings.setQueryAuthorizations(auths.serialize());
    settings.setQuery("FOO == 'BAR'");
    settings.setParameters(new HashSet<>());
    settings.setId(UUID.randomUUID());
    CompositeQueryLogic c = new CompositeQueryLogic();
    /**
     * RunningQuery.setupConnection()
     */
    c.setQueryLogics(logics);
    c.initialize((Connector) null, (Query) settings, Collections.singleton(auths));
    c.setupQuery(null);
    TransformIterator iter = c.getTransformIterator((Query) settings);
    /**
     * RunningQuery.next() - iterate over results coming from tablet server through the TransformIterator to turn them into the objects.
     */
    List<Object> results = new ArrayList<>();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (null == o)
            break;
        Assert.assertTrue(o instanceof TestQueryResponse);
        results.add((TestQueryResponse) o);
    }
    Assert.assertEquals(0, results.size());
    c.close();
}
Also used : QueryImpl(datawave.webservice.query.QueryImpl) TransformIterator(org.apache.commons.collections4.iterators.TransformIterator) ArrayList(java.util.ArrayList) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) Test(org.junit.Test)

Example 4 with TransformIterator

use of org.apache.commons.collections4.iterators.TransformIterator in project datawave by NationalSecurityAgency.

the class CompositeQueryLogicTest method testQueryLogic.

@Test
public // testQueryLogic with max.results.override not set
void testQueryLogic() throws Exception {
    Logger.getLogger(CompositeQueryLogic.class).setLevel(Level.TRACE);
    Logger.getLogger(CompositeQueryLogicResults.class).setLevel(Level.TRACE);
    Logger.getLogger(CompositeQueryLogicTransformer.class).setLevel(Level.TRACE);
    List<BaseQueryLogic<?>> logics = new ArrayList<>();
    TestQueryLogic logic1 = new TestQueryLogic();
    TestQueryLogic2 logic2 = new TestQueryLogic2();
    logics.add(logic1);
    logics.add(logic2);
    logic1.getData().put(key1, value1);
    logic1.getData().put(key2, value2);
    logic2.getData().put(key3, value3);
    logic2.getData().put(key4, value4);
    logic1.getData().put(key5, value5);
    logic1.getData().put(key6, value6);
    logic2.getData().put(key7, value7);
    logic2.getData().put(key8, value8);
    QueryImpl settings = new QueryImpl();
    settings.setPagesize(100);
    settings.setQueryAuthorizations(auths.toString());
    settings.setQuery("FOO == 'BAR'");
    settings.setParameters(new HashSet<>());
    settings.setId(UUID.randomUUID());
    CompositeQueryLogic c = new CompositeQueryLogic();
    // max.results.override is set to -1 when it is not passed in as it is an optional paramter
    logic1.setMaxResults(-1);
    logic2.setMaxResults(-1);
    /**
     * RunningQuery.setupConnection()
     */
    c.setQueryLogics(logics);
    c.initialize((Connector) null, (Query) settings, Collections.singleton(auths));
    c.setupQuery(null);
    TransformIterator iter = c.getTransformIterator((Query) settings);
    /**
     * RunningQuery.next() - iterate over results coming from tablet server through the TransformIterator to turn them into the objects.
     */
    List<Object> results = new ArrayList<>();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (null == o)
            break;
        Assert.assertTrue(o instanceof TestQueryResponse);
        results.add((TestQueryResponse) o);
    }
    Assert.assertEquals(8, results.size());
    ResultsPage page = new ResultsPage(results, Status.COMPLETE);
    /**
     * QueryExecutorBean.next() - transform list of objects into JAXB response
     */
    TestQueryResponseList response = (TestQueryResponseList) c.getTransformer((Query) settings).createResponse(page);
    Assert.assertEquals(8, response.getResponses().size());
    for (TestQueryResponse r : response.getResponses()) {
        Assert.assertNotNull(r);
    }
    c.close();
}
Also used : TransformIterator(org.apache.commons.collections4.iterators.TransformIterator) ArrayList(java.util.ArrayList) ResultsPage(datawave.webservice.query.cache.ResultsPage) QueryImpl(datawave.webservice.query.QueryImpl) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) Test(org.junit.Test)

Example 5 with TransformIterator

use of org.apache.commons.collections4.iterators.TransformIterator in project datawave by NationalSecurityAgency.

the class CompositeQueryLogicTest method testQueryLogicWithMaxResultsOverride.

@Test
public // testQueryLogic with max.results.override is set
void testQueryLogicWithMaxResultsOverride() throws Exception {
    Logger.getLogger(CompositeQueryLogic.class).setLevel(Level.TRACE);
    Logger.getLogger(CompositeQueryLogicResults.class).setLevel(Level.TRACE);
    Logger.getLogger(CompositeQueryLogicTransformer.class).setLevel(Level.TRACE);
    List<BaseQueryLogic<?>> logics = new ArrayList<>();
    TestQueryLogic logic1 = new TestQueryLogic();
    TestQueryLogic2 logic2 = new TestQueryLogic2();
    logics.add(logic1);
    logics.add(logic2);
    logic1.getData().put(key1, value1);
    logic1.getData().put(key2, value2);
    logic2.getData().put(key3, value3);
    logic2.getData().put(key4, value4);
    logic1.getData().put(key5, value5);
    logic1.getData().put(key6, value6);
    logic2.getData().put(key7, value7);
    logic2.getData().put(key8, value8);
    QueryImpl settings = new QueryImpl();
    settings.setPagesize(100);
    settings.setQueryAuthorizations(auths.toString());
    settings.setQuery("FOO == 'BAR'");
    settings.setParameters(new HashSet<>());
    settings.setId(UUID.randomUUID());
    CompositeQueryLogic c = new CompositeQueryLogic();
    // max.results.override is set to -1 when it is not passed in as it is an optional parameter
    logic1.setMaxResults(0);
    logic2.setMaxResults(4);
    /**
     * RunningQuery.setupConnection()
     */
    c.setQueryLogics(logics);
    c.initialize((Connector) null, (Query) settings, Collections.singleton(auths));
    c.setupQuery(null);
    TransformIterator iter = c.getTransformIterator((Query) settings);
    /**
     * RunningQuery.next() - iterate over results coming from tablet server through the TransformIterator to turn them into the objects.
     */
    List<Object> results = new ArrayList<>();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (null == o)
            break;
        Assert.assertTrue(o instanceof TestQueryResponse);
        results.add((TestQueryResponse) o);
    }
    Assert.assertEquals(4, results.size());
    ResultsPage page = new ResultsPage(results, Status.COMPLETE);
    /**
     * QueryExecutorBean.next() - transform list of objects into JAXB response
     */
    TestQueryResponseList response = (TestQueryResponseList) c.getTransformer((Query) settings).createResponse(page);
    Assert.assertEquals(4, response.getResponses().size());
    for (TestQueryResponse r : response.getResponses()) {
        Assert.assertNotNull(r);
    }
    c.close();
}
Also used : TransformIterator(org.apache.commons.collections4.iterators.TransformIterator) ArrayList(java.util.ArrayList) ResultsPage(datawave.webservice.query.cache.ResultsPage) QueryImpl(datawave.webservice.query.QueryImpl) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) Test(org.junit.Test)

Aggregations

TransformIterator (org.apache.commons.collections4.iterators.TransformIterator)15 QueryImpl (datawave.webservice.query.QueryImpl)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 BaseQueryLogic (datawave.webservice.query.logic.BaseQueryLogic)6 EventBase (datawave.webservice.query.result.event.EventBase)5 ResultsPage (datawave.webservice.query.cache.ResultsPage)4 GenericQueryConfiguration (datawave.webservice.query.configuration.GenericQueryConfiguration)3 DatawaveTransformIterator (datawave.webservice.query.iterator.DatawaveTransformIterator)3 FieldBase (datawave.webservice.query.result.event.FieldBase)3 BaseQueryResponse (datawave.webservice.result.BaseQueryResponse)3 DefaultEventQueryResponse (datawave.webservice.result.DefaultEventQueryResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 QueryMetricFactoryImpl (datawave.microservice.querymetric.QueryMetricFactoryImpl)2 Connector (org.apache.accumulo.core.client.Connector)2 InMemoryInstance (datawave.accumulo.inmemory.InMemoryInstance)1 DocumentTransformer (datawave.query.transformer.DocumentTransformer)1 ShardQueryCountTableTransformer (datawave.query.transformer.ShardQueryCountTableTransformer)1 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)1 DatawaveUser (datawave.security.authorization.DatawaveUser)1