Search in sources :

Example 6 with SegmentMetadataQuery

use of io.druid.query.metadata.metadata.SegmentMetadataQuery in project druid by druid-io.

the class SegmentMetadataQueryRunnerFactory method createRunner.

@Override
public QueryRunner<SegmentAnalysis> createRunner(final Segment segment) {
    return new QueryRunner<SegmentAnalysis>() {

        @Override
        public Sequence<SegmentAnalysis> run(Query<SegmentAnalysis> inQ, Map<String, Object> responseContext) {
            SegmentMetadataQuery query = (SegmentMetadataQuery) inQ;
            final SegmentAnalyzer analyzer = new SegmentAnalyzer(query.getAnalysisTypes());
            final Map<String, ColumnAnalysis> analyzedColumns = analyzer.analyze(segment);
            final long numRows = analyzer.numRows(segment);
            long totalSize = 0;
            if (analyzer.analyzingSize()) {
                // Initialize with the size of the whitespace, 1 byte per
                totalSize = analyzedColumns.size() * numRows;
            }
            Map<String, ColumnAnalysis> columns = Maps.newTreeMap();
            ColumnIncluderator includerator = query.getToInclude();
            for (Map.Entry<String, ColumnAnalysis> entry : analyzedColumns.entrySet()) {
                final String columnName = entry.getKey();
                final ColumnAnalysis column = entry.getValue();
                if (!column.isError()) {
                    totalSize += column.getSize();
                }
                if (includerator.include(columnName)) {
                    columns.put(columnName, column);
                }
            }
            List<Interval> retIntervals = query.analyzingInterval() ? Arrays.asList(segment.getDataInterval()) : null;
            final Map<String, AggregatorFactory> aggregators;
            Metadata metadata = null;
            if (query.hasAggregators()) {
                metadata = segment.asStorageAdapter().getMetadata();
                if (metadata != null && metadata.getAggregators() != null) {
                    aggregators = Maps.newHashMap();
                    for (AggregatorFactory aggregator : metadata.getAggregators()) {
                        aggregators.put(aggregator.getName(), aggregator);
                    }
                } else {
                    aggregators = null;
                }
            } else {
                aggregators = null;
            }
            final TimestampSpec timestampSpec;
            if (query.hasTimestampSpec()) {
                if (metadata == null) {
                    metadata = segment.asStorageAdapter().getMetadata();
                }
                timestampSpec = metadata != null ? metadata.getTimestampSpec() : null;
            } else {
                timestampSpec = null;
            }
            final Granularity queryGranularity;
            if (query.hasQueryGranularity()) {
                if (metadata == null) {
                    metadata = segment.asStorageAdapter().getMetadata();
                }
                queryGranularity = metadata != null ? metadata.getQueryGranularity() : null;
            } else {
                queryGranularity = null;
            }
            Boolean rollup = null;
            if (query.hasRollup()) {
                if (metadata == null) {
                    metadata = segment.asStorageAdapter().getMetadata();
                }
                rollup = metadata != null ? metadata.isRollup() : null;
                if (rollup == null) {
                    // in this case, this segment is built before no-rollup function is coded,
                    // thus it is built with rollup
                    rollup = Boolean.TRUE;
                }
            }
            return Sequences.simple(Arrays.asList(new SegmentAnalysis(segment.getIdentifier(), retIntervals, columns, totalSize, numRows, aggregators, timestampSpec, queryGranularity, rollup)));
        }
    };
}
Also used : BaseQuery(io.druid.query.BaseQuery) SegmentMetadataQuery(io.druid.query.metadata.metadata.SegmentMetadataQuery) Query(io.druid.query.Query) Metadata(io.druid.segment.Metadata) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) Granularity(io.druid.java.util.common.granularity.Granularity) ColumnIncluderator(io.druid.query.metadata.metadata.ColumnIncluderator) QueryRunner(io.druid.query.QueryRunner) ConcatQueryRunner(io.druid.query.ConcatQueryRunner) SegmentMetadataQuery(io.druid.query.metadata.metadata.SegmentMetadataQuery) ColumnAnalysis(io.druid.query.metadata.metadata.ColumnAnalysis) TimestampSpec(io.druid.data.input.impl.TimestampSpec) SegmentAnalysis(io.druid.query.metadata.metadata.SegmentAnalysis) Map(java.util.Map) Interval(org.joda.time.Interval)

Example 7 with SegmentMetadataQuery

use of io.druid.query.metadata.metadata.SegmentMetadataQuery in project druid by druid-io.

the class SegmentAnalyzerTest method getSegmentAnalysises.

/**
   * *Awesome* method name auto-generated by IntelliJ!  I love IntelliJ!
   *
   * @param index
   *
   * @return
   */
private List<SegmentAnalysis> getSegmentAnalysises(Segment index, EnumSet<SegmentMetadataQuery.AnalysisType> analyses) {
    final QueryRunner runner = QueryRunnerTestHelper.makeQueryRunner((QueryRunnerFactory) new SegmentMetadataQueryRunnerFactory(new SegmentMetadataQueryQueryToolChest(new SegmentMetadataQueryConfig()), QueryRunnerTestHelper.NOOP_QUERYWATCHER), index, null);
    final SegmentMetadataQuery query = new SegmentMetadataQuery(new LegacyDataSource("test"), QuerySegmentSpecs.create("2011/2012"), null, null, null, analyses, false, false);
    HashMap<String, Object> context = new HashMap<String, Object>();
    return Sequences.toList(query.run(runner, context), Lists.<SegmentAnalysis>newArrayList());
}
Also used : HashMap(java.util.HashMap) SegmentMetadataQuery(io.druid.query.metadata.metadata.SegmentMetadataQuery) LegacyDataSource(io.druid.query.LegacyDataSource) QueryRunner(io.druid.query.QueryRunner)

Example 8 with SegmentMetadataQuery

use of io.druid.query.metadata.metadata.SegmentMetadataQuery in project druid by druid-io.

the class SegmentMetadataQueryTest method testSerde.

@Test
public void testSerde() throws Exception {
    String queryStr = "{\n" + "  \"queryType\":\"segmentMetadata\",\n" + "  \"dataSource\":\"test_ds\",\n" + "  \"intervals\":[\"2013-12-04T00:00:00.000Z/2013-12-05T00:00:00.000Z\"],\n" + "  \"analysisTypes\":[\"cardinality\",\"size\"]\n" + "}";
    EnumSet<SegmentMetadataQuery.AnalysisType> expectedAnalysisTypes = EnumSet.of(SegmentMetadataQuery.AnalysisType.CARDINALITY, SegmentMetadataQuery.AnalysisType.SIZE);
    Query query = MAPPER.readValue(queryStr, Query.class);
    Assert.assertTrue(query instanceof SegmentMetadataQuery);
    Assert.assertEquals("test_ds", Iterables.getOnlyElement(query.getDataSource().getNames()));
    Assert.assertEquals(new Interval("2013-12-04T00:00:00.000Z/2013-12-05T00:00:00.000Z"), query.getIntervals().get(0));
    Assert.assertEquals(expectedAnalysisTypes, ((SegmentMetadataQuery) query).getAnalysisTypes());
    // test serialize and deserialize
    Assert.assertEquals(query, MAPPER.readValue(MAPPER.writeValueAsString(query), Query.class));
}
Also used : SegmentMetadataQuery(io.druid.query.metadata.metadata.SegmentMetadataQuery) Query(io.druid.query.Query) SegmentMetadataQuery(io.druid.query.metadata.metadata.SegmentMetadataQuery) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 9 with SegmentMetadataQuery

use of io.druid.query.metadata.metadata.SegmentMetadataQuery in project druid by druid-io.

the class SegmentMetadataQueryTest method testSerdeWithDefaultInterval.

@Test
public void testSerdeWithDefaultInterval() throws Exception {
    String queryStr = "{\n" + "  \"queryType\":\"segmentMetadata\",\n" + "  \"dataSource\":\"test_ds\"\n" + "}";
    Query query = MAPPER.readValue(queryStr, Query.class);
    Assert.assertTrue(query instanceof SegmentMetadataQuery);
    Assert.assertEquals("test_ds", Iterables.getOnlyElement(query.getDataSource().getNames()));
    Assert.assertEquals(new Interval(JodaUtils.MIN_INSTANT, JodaUtils.MAX_INSTANT), query.getIntervals().get(0));
    Assert.assertTrue(((SegmentMetadataQuery) query).isUsingDefaultInterval());
    // test serialize and deserialize
    Assert.assertEquals(query, MAPPER.readValue(MAPPER.writeValueAsString(query), Query.class));
}
Also used : SegmentMetadataQuery(io.druid.query.metadata.metadata.SegmentMetadataQuery) Query(io.druid.query.Query) SegmentMetadataQuery(io.druid.query.metadata.metadata.SegmentMetadataQuery) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 10 with SegmentMetadataQuery

use of io.druid.query.metadata.metadata.SegmentMetadataQuery in project druid by druid-io.

the class SegmentMetadataQueryTest method testDefaultIntervalAndFiltering.

@Test
public void testDefaultIntervalAndFiltering() throws Exception {
    SegmentMetadataQuery testQuery = Druids.newSegmentMetadataQueryBuilder().dataSource("testing").toInclude(new ListColumnIncluderator(Arrays.asList("placement"))).merge(true).build();
    Interval expectedInterval = new Interval(JodaUtils.MIN_INSTANT, JodaUtils.MAX_INSTANT);
    /* No interval specified, should use default interval */
    Assert.assertTrue(testQuery.isUsingDefaultInterval());
    Assert.assertEquals(testQuery.getIntervals().get(0), expectedInterval);
    Assert.assertEquals(testQuery.getIntervals().size(), 1);
    List<LogicalSegment> testSegments = Arrays.asList(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2012-01-01/P1D");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2012-01-01T01/PT1H");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2013-01-05/P1D");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2013-05-20/P1D");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2014-01-05/P1D");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2014-02-05/P1D");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2015-01-19T01/PT1H");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2015-01-20T02/PT1H");
        }
    });
    /* Test default period filter */
    List<LogicalSegment> filteredSegments = new SegmentMetadataQueryQueryToolChest(new SegmentMetadataQueryConfig()).filterSegments(testQuery, testSegments);
    List<LogicalSegment> expectedSegments = Arrays.asList(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2015-01-19T01/PT1H");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2015-01-20T02/PT1H");
        }
    });
    Assert.assertEquals(filteredSegments.size(), 2);
    for (int i = 0; i < filteredSegments.size(); i++) {
        Assert.assertEquals(expectedSegments.get(i).getInterval(), filteredSegments.get(i).getInterval());
    }
    /* Test 2 year period filtering */
    SegmentMetadataQueryConfig twoYearPeriodCfg = new SegmentMetadataQueryConfig("P2Y");
    List<LogicalSegment> filteredSegments2 = new SegmentMetadataQueryQueryToolChest(twoYearPeriodCfg).filterSegments(testQuery, testSegments);
    List<LogicalSegment> expectedSegments2 = Arrays.asList(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2013-05-20/P1D");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2014-01-05/P1D");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2014-02-05/P1D");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2015-01-19T01/PT1H");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return new Interval("2015-01-20T02/PT1H");
        }
    });
    Assert.assertEquals(filteredSegments2.size(), 5);
    for (int i = 0; i < filteredSegments2.size(); i++) {
        Assert.assertEquals(expectedSegments2.get(i).getInterval(), filteredSegments2.get(i).getInterval());
    }
}
Also used : LogicalSegment(io.druid.timeline.LogicalSegment) SegmentMetadataQuery(io.druid.query.metadata.metadata.SegmentMetadataQuery) ListColumnIncluderator(io.druid.query.metadata.metadata.ListColumnIncluderator) Interval(org.joda.time.Interval) Test(org.junit.Test)

Aggregations

SegmentMetadataQuery (io.druid.query.metadata.metadata.SegmentMetadataQuery)13 SegmentAnalysis (io.druid.query.metadata.metadata.SegmentAnalysis)8 Interval (org.joda.time.Interval)7 ColumnAnalysis (io.druid.query.metadata.metadata.ColumnAnalysis)5 Test (org.junit.Test)5 ListColumnIncluderator (io.druid.query.metadata.metadata.ListColumnIncluderator)4 IOException (java.io.IOException)4 SegmentMetadataQueryBuilder (io.druid.query.Druids.SegmentMetadataQueryBuilder)3 Query (io.druid.query.Query)3 TableDataSource (io.druid.query.TableDataSource)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 QueryRunner (io.druid.query.QueryRunner)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 SerDeException (org.apache.hadoop.hive.serde2.SerDeException)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ImmutableList (com.google.common.collect.ImmutableList)1