Search in sources :

Example 1 with CloseableIterator

use of org.apache.druid.java.util.common.parsers.CloseableIterator in project druid by druid-io.

the class SqlSegmentsMetadataQuery method retrieveSegments.

private CloseableIterator<DataSegment> retrieveSegments(final String dataSource, final Collection<Interval> intervals, final IntervalMode matchMode, final boolean used) {
    // Check if the intervals all support comparing as strings. If so, bake them into the SQL.
    final boolean compareAsString = intervals.stream().allMatch(Intervals::canCompareEndpointsAsStrings);
    final StringBuilder sb = new StringBuilder();
    sb.append("SELECT payload FROM %s WHERE used = :used AND dataSource = :dataSource");
    if (compareAsString && !intervals.isEmpty()) {
        sb.append(" AND (");
        for (int i = 0; i < intervals.size(); i++) {
            sb.append(matchMode.makeSqlCondition(connector.getQuoteString(), StringUtils.format(":start%d", i), StringUtils.format(":end%d", i)));
            if (i == intervals.size() - 1) {
                sb.append(")");
            } else {
                sb.append(" OR ");
            }
        }
    }
    final Query<Map<String, Object>> sql = handle.createQuery(StringUtils.format(sb.toString(), dbTables.getSegmentsTable())).setFetchSize(connector.getStreamingFetchSize()).bind("used", used).bind("dataSource", dataSource);
    if (compareAsString) {
        final Iterator<Interval> iterator = intervals.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            Interval interval = iterator.next();
            sql.bind(StringUtils.format("start%d", i), interval.getStart().toString()).bind(StringUtils.format("end%d", i), interval.getEnd().toString());
        }
    }
    final ResultIterator<DataSegment> resultIterator = sql.map((index, r, ctx) -> JacksonUtils.readValue(jsonMapper, r.getBytes(1), DataSegment.class)).iterator();
    return CloseableIterators.wrap(Iterators.filter(resultIterator, dataSegment -> {
        if (intervals.isEmpty()) {
            return true;
        } else {
            // segment interval like "20010/20011".)
            for (Interval interval : intervals) {
                if (matchMode.apply(interval, dataSegment.getInterval())) {
                    return true;
                }
            }
            return false;
        }
    }), resultIterator);
}
Also used : Intervals(org.apache.druid.java.util.common.Intervals) Logger(org.apache.druid.java.util.common.logger.Logger) JacksonUtils(org.apache.druid.java.util.common.jackson.JacksonUtils) Iterator(java.util.Iterator) Intervals(org.apache.druid.java.util.common.Intervals) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StringUtils(org.apache.druid.java.util.common.StringUtils) Query(org.skife.jdbi.v2.Query) Collectors(java.util.stream.Collectors) Iterators(com.google.common.collect.Iterators) Interval(org.joda.time.Interval) List(java.util.List) CloseableIterators(org.apache.druid.java.util.common.CloseableIterators) Handle(org.skife.jdbi.v2.Handle) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) DataSegment(org.apache.druid.timeline.DataSegment) ResultIterator(org.skife.jdbi.v2.ResultIterator) PreparedBatch(org.skife.jdbi.v2.PreparedBatch) IAE(org.apache.druid.java.util.common.IAE) SegmentId(org.apache.druid.timeline.SegmentId) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) Collections(java.util.Collections) Map(java.util.Map) DataSegment(org.apache.druid.timeline.DataSegment) Interval(org.joda.time.Interval)

Example 2 with CloseableIterator

use of org.apache.druid.java.util.common.parsers.CloseableIterator in project druid by druid-io.

the class ParquetReader method intermediateRowIterator.

@Override
protected CloseableIterator<Group> intermediateRowIterator() throws IOException {
    final Closer closer = Closer.create();
    byte[] buffer = new byte[InputEntity.DEFAULT_FETCH_BUFFER_SIZE];
    final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
    final org.apache.parquet.hadoop.ParquetReader<Group> reader;
    try {
        final CleanableFile file = closer.register(source.fetch(temporaryDirectory, buffer));
        final Path path = new Path(file.file().toURI());
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        reader = closer.register(org.apache.parquet.hadoop.ParquetReader.builder(new GroupReadSupport(), path).withConf(conf).build());
    } catch (Exception e) {
        // We don't expect to see any exceptions thrown in the above try clause,
        // but we catch it just in case to avoid any potential resource leak.
        closer.close();
        throw new RuntimeException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
    }
    return new CloseableIterator<Group>() {

        Group value = null;

        @Override
        public boolean hasNext() {
            if (value == null) {
                try {
                    value = reader.read();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return value != null;
        }

        @Override
        public Group next() {
            if (value == null) {
                throw new NoSuchElementException();
            }
            Group currentValue = value;
            value = null;
            return currentValue;
        }

        @Override
        public void close() throws IOException {
            closer.close();
        }
    };
}
Also used : Closer(org.apache.druid.java.util.common.io.Closer) Path(org.apache.hadoop.fs.Path) Group(org.apache.parquet.example.data.Group) GroupReadSupport(org.apache.parquet.hadoop.example.GroupReadSupport) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) IOException(java.io.IOException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) CleanableFile(org.apache.druid.data.input.InputEntity.CleanableFile) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with CloseableIterator

use of org.apache.druid.java.util.common.parsers.CloseableIterator in project druid by druid-io.

the class DruidSegmentReaderTest method testMakeCloseableIteratorFromSequenceAndSegmentFileCloseYielderOnClose.

@Test
public void testMakeCloseableIteratorFromSequenceAndSegmentFileCloseYielderOnClose() throws IOException {
    MutableBoolean isSequenceClosed = new MutableBoolean(false);
    MutableBoolean isFileClosed = new MutableBoolean(false);
    Sequence<Map<String, Object>> sequence = new BaseSequence<>(new IteratorMaker<Map<String, Object>, Iterator<Map<String, Object>>>() {

        @Override
        public Iterator<Map<String, Object>> make() {
            return Collections.emptyIterator();
        }

        @Override
        public void cleanup(Iterator<Map<String, Object>> iterFromMake) {
            isSequenceClosed.setValue(true);
        }
    });
    CleanableFile cleanableFile = new CleanableFile() {

        @Override
        public File file() {
            return null;
        }

        @Override
        public void close() {
            isFileClosed.setValue(true);
        }
    };
    try (CloseableIterator<Map<String, Object>> iterator = DruidSegmentReader.makeCloseableIteratorFromSequenceAndSegmentFile(sequence, cleanableFile)) {
        while (iterator.hasNext()) {
            iterator.next();
        }
    }
    Assert.assertTrue("File is not closed", isFileClosed.booleanValue());
    Assert.assertTrue("Sequence is not closed", isSequenceClosed.booleanValue());
}
Also used : MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) Iterator(java.util.Iterator) CleanableFile(org.apache.druid.data.input.InputEntity.CleanableFile) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BaseSequence(org.apache.druid.java.util.common.guava.BaseSequence) NullHandlingTest(org.apache.druid.common.config.NullHandlingTest) Test(org.junit.Test)

Example 4 with CloseableIterator

use of org.apache.druid.java.util.common.parsers.CloseableIterator in project druid by druid-io.

the class RecordSupplierInputSourceTest method testRead.

@Test
public void testRead() throws IOException {
    final RandomCsvSupplier supplier = new RandomCsvSupplier();
    final InputSource inputSource = new RecordSupplierInputSource<>("topic", supplier, false);
    final List<String> colNames = IntStream.range(0, NUM_COLS).mapToObj(i -> StringUtils.format("col_%d", i)).collect(Collectors.toList());
    final InputFormat inputFormat = new CsvInputFormat(colNames, null, null, false, 0);
    final InputSourceReader reader = inputSource.reader(new InputRowSchema(new TimestampSpec("col_0", "auto", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(colNames.subList(1, colNames.size()))), ColumnsFilter.all()), inputFormat, temporaryFolder.newFolder());
    int read = 0;
    try (CloseableIterator<InputRow> iterator = reader.read()) {
        for (; read < NUM_ROWS && iterator.hasNext(); read++) {
            final InputRow inputRow = iterator.next();
            Assert.assertEquals(DateTimes.of(TIMESTAMP_STRING), inputRow.getTimestamp());
            Assert.assertEquals(NUM_COLS - 1, inputRow.getDimensions().size());
        }
    }
    Assert.assertEquals(NUM_ROWS, read);
    Assert.assertTrue(supplier.isClosed());
}
Also used : IntStream(java.util.stream.IntStream) StreamPartition(org.apache.druid.indexing.seekablestream.common.StreamPartition) RecordSupplier(org.apache.druid.indexing.seekablestream.common.RecordSupplier) OrderedPartitionableRecord(org.apache.druid.indexing.seekablestream.common.OrderedPartitionableRecord) Random(java.util.Random) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) InputRowSchema(org.apache.druid.data.input.InputRowSchema) ArrayList(java.util.ArrayList) InputSource(org.apache.druid.data.input.InputSource) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) InputSourceReader(org.apache.druid.data.input.InputSourceReader) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) Nullable(javax.annotation.Nullable) DateTimes(org.apache.druid.java.util.common.DateTimes) ColumnsFilter(org.apache.druid.data.input.ColumnsFilter) InputFormat(org.apache.druid.data.input.InputFormat) Collection(java.util.Collection) StringUtils(org.apache.druid.java.util.common.StringUtils) Set(java.util.Set) CsvInputFormat(org.apache.druid.data.input.impl.CsvInputFormat) DimensionsSpec(org.apache.druid.data.input.impl.DimensionsSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) IOException(java.io.IOException) NotNull(javax.validation.constraints.NotNull) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) InputRow(org.apache.druid.data.input.InputRow) List(java.util.List) Rule(org.junit.Rule) ByteEntity(org.apache.druid.data.input.impl.ByteEntity) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) InputSource(org.apache.druid.data.input.InputSource) InputSourceReader(org.apache.druid.data.input.InputSourceReader) InputFormat(org.apache.druid.data.input.InputFormat) CsvInputFormat(org.apache.druid.data.input.impl.CsvInputFormat) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) InputRow(org.apache.druid.data.input.InputRow) DimensionsSpec(org.apache.druid.data.input.impl.DimensionsSpec) CsvInputFormat(org.apache.druid.data.input.impl.CsvInputFormat) InputRowSchema(org.apache.druid.data.input.InputRowSchema) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 5 with CloseableIterator

use of org.apache.druid.java.util.common.parsers.CloseableIterator in project druid by druid-io.

the class BufferHashGrouper method iterator.

@Override
public CloseableIterator<Entry<KeyType>> iterator(boolean sorted) {
    if (!initialized) {
        // a nested groupBy's subquery has an empty result set (see testEmptySubquery() in GroupByQueryRunnerTest)
        return CloseableIterators.withEmptyBaggage(Collections.emptyIterator());
    }
    if (sorted) {
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") final List<Integer> wrappedOffsets = new AbstractList<Integer>() {

            @Override
            public Integer get(int index) {
                return offsetList.get(index);
            }

            @Override
            public Integer set(int index, Integer element) {
                final Integer oldValue = get(index);
                offsetList.set(index, element);
                return oldValue;
            }

            @Override
            public int size() {
                return hashTable.getSize();
            }
        };
        final BufferComparator comparator;
        if (useDefaultSorting) {
            comparator = keySerde.bufferComparator();
        } else {
            comparator = keySerde.bufferComparatorWithAggregators(aggregators.factories().toArray(new AggregatorFactory[0]), aggregators.aggregatorPositions());
        }
        // Sort offsets in-place.
        Collections.sort(wrappedOffsets, (lhs, rhs) -> {
            final ByteBuffer tableBuffer = hashTable.getTableBuffer();
            return comparator.compare(tableBuffer, tableBuffer, lhs + HASH_SIZE, rhs + HASH_SIZE);
        });
        return new CloseableIterator<Entry<KeyType>>() {

            int curr = 0;

            final int size = getSize();

            @Override
            public boolean hasNext() {
                return curr < size;
            }

            @Override
            public Entry<KeyType> next() {
                if (curr >= size) {
                    throw new NoSuchElementException();
                }
                return bucketEntryForOffset(wrappedOffsets.get(curr++));
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }

            @Override
            public void close() {
            // do nothing
            }
        };
    } else {
        // Unsorted iterator
        return new CloseableIterator<Entry<KeyType>>() {

            int curr = 0;

            final int size = getSize();

            @Override
            public boolean hasNext() {
                return curr < size;
            }

            @Override
            public Entry<KeyType> next() {
                if (curr >= size) {
                    throw new NoSuchElementException();
                }
                final int offset = offsetList.get(curr);
                final Entry<KeyType> entry = bucketEntryForOffset(offset);
                curr++;
                return entry;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }

            @Override
            public void close() {
            // do nothing
            }
        };
    }
}
Also used : AbstractList(java.util.AbstractList) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) ByteBuffer(java.nio.ByteBuffer) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

CloseableIterator (org.apache.druid.java.util.common.parsers.CloseableIterator)16 IOException (java.io.IOException)9 Map (java.util.Map)6 NoSuchElementException (java.util.NoSuchElementException)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Collections (java.util.Collections)4 Iterator (java.util.Iterator)4 Set (java.util.Set)4 CleanableFile (org.apache.druid.data.input.InputEntity.CleanableFile)4 Closer (org.apache.druid.java.util.common.io.Closer)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Preconditions (com.google.common.base.Preconditions)3 ImmutableList (com.google.common.collect.ImmutableList)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 File (java.io.File)3 Collectors (java.util.stream.Collectors)3 ParseException (org.apache.druid.java.util.common.parsers.ParseException)3 Supplier (com.google.common.base.Supplier)2 ByteBuffer (java.nio.ByteBuffer)2