Search in sources :

Example 6 with CloseableIterator

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

the class BufferArrayGrouper method iterator.

@Override
public CloseableIterator<Entry<Memory>> iterator() {
    final CloseableIterator<Entry<Integer>> iterator = iterator(false);
    final WritableMemory keyMemory = WritableMemory.allocate(Integer.BYTES);
    return new CloseableIterator<Entry<Memory>>() {

        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public Entry<Memory> next() {
            final Entry<Integer> integerEntry = iterator.next();
            keyMemory.putInt(0, integerEntry.getKey());
            return new Entry<>(keyMemory, integerEntry.getValues());
        }

        @Override
        public void close() throws IOException {
            iterator.close();
        }
    };
}
Also used : CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 7 with CloseableIterator

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

the class ConcurrentGrouper method parallelSortAndGetGroupersIterator.

private List<CloseableIterator<Entry<KeyType>>> parallelSortAndGetGroupersIterator() {
    // The number of groupers is same with the number of processing threads in the executor
    final List<ListenableFuture<CloseableIterator<Entry<KeyType>>>> futures = groupers.stream().map(grouper -> executor.submit(new AbstractPrioritizedCallable<CloseableIterator<Entry<KeyType>>>(priority) {

        @Override
        public CloseableIterator<Entry<KeyType>> call() {
            return grouper.iterator(true);
        }
    })).collect(Collectors.toList());
    ListenableFuture<List<CloseableIterator<Entry<KeyType>>>> future = Futures.allAsList(futures);
    try {
        final long timeout = queryTimeoutAt - System.currentTimeMillis();
        return hasQueryTimeout ? future.get(timeout, TimeUnit.MILLISECONDS) : future.get();
    } catch (InterruptedException | CancellationException e) {
        GuavaUtils.cancelAll(true, future, futures);
        throw new QueryInterruptedException(e);
    } catch (TimeoutException e) {
        GuavaUtils.cancelAll(true, future, futures);
        throw new QueryTimeoutException();
    } catch (ExecutionException e) {
        GuavaUtils.cancelAll(true, future, futures);
        throw new RuntimeException(e.getCause());
    }
}
Also used : Arrays(java.util.Arrays) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Supplier(com.google.common.base.Supplier) TimeoutException(java.util.concurrent.TimeoutException) ByteBuffer(java.nio.ByteBuffer) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AbstractPrioritizedCallable(org.apache.druid.query.AbstractPrioritizedCallable) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Suppliers(com.google.common.base.Suppliers) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) Nullable(javax.annotation.Nullable) CancellationException(java.util.concurrent.CancellationException) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GroupByQueryConfig(org.apache.druid.query.groupby.GroupByQueryConfig) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) CloseableIterators(org.apache.druid.java.util.common.CloseableIterators) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) GuavaUtils(org.apache.druid.common.guava.GuavaUtils) Preconditions(com.google.common.base.Preconditions) Comparator(java.util.Comparator) ReferenceCountingResourceHolder(org.apache.druid.collections.ReferenceCountingResourceHolder) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) CancellationException(java.util.concurrent.CancellationException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) TimeoutException(java.util.concurrent.TimeoutException) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException)

Example 8 with CloseableIterator

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

the class SpillingGrouper method iterator.

@Override
public CloseableIterator<Entry<KeyType>> iterator(final boolean sorted) {
    final List<CloseableIterator<Entry<KeyType>>> iterators = new ArrayList<>(1 + files.size());
    iterators.add(grouper.iterator(sorted));
    final Closer closer = Closer.create();
    for (final File file : files) {
        final MappingIterator<Entry<KeyType>> fileIterator = read(file, keySerde.keyClazz());
        iterators.add(CloseableIterators.withEmptyBaggage(Iterators.transform(fileIterator, new Function<Entry<KeyType>, Entry<KeyType>>() {

            @Override
            public Entry<KeyType> apply(Entry<KeyType> entry) {
                final Object[] deserializedValues = new Object[entry.getValues().length];
                for (int i = 0; i < deserializedValues.length; i++) {
                    deserializedValues[i] = aggregatorFactories[i].deserialize(entry.getValues()[i]);
                    if (deserializedValues[i] instanceof Integer) {
                        // Hack to satisfy the groupBy unit tests; perhaps we could do better by adjusting Jackson config.
                        deserializedValues[i] = ((Integer) deserializedValues[i]).longValue();
                    }
                }
                return new Entry<>(entry.getKey(), deserializedValues);
            }
        })));
        closer.register(fileIterator);
    }
    final Iterator<Entry<KeyType>> baseIterator;
    if (sortHasNonGroupingFields) {
        baseIterator = CloseableIterators.mergeSorted(iterators, defaultOrderKeyObjComparator);
    } else {
        baseIterator = sorted ? CloseableIterators.mergeSorted(iterators, keyObjComparator) : CloseableIterators.concat(iterators);
    }
    return CloseableIterators.wrap(baseIterator, closer);
}
Also used : Closer(org.apache.druid.java.util.common.io.Closer) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) ArrayList(java.util.ArrayList) File(java.io.File)

Example 9 with CloseableIterator

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

the class ParallelCombiner method runCombiner.

private Pair<CloseableIterator<Entry<KeyType>>, Future> runCombiner(List<? extends CloseableIterator<Entry<KeyType>>> iterators, ByteBuffer combineBuffer, AggregatorFactory[] combiningFactories, List<String> dictionary) {
    final SettableColumnSelectorFactory settableColumnSelectorFactory = new SettableColumnSelectorFactory(combiningFactories);
    final StreamingMergeSortedGrouper<KeyType> grouper = new StreamingMergeSortedGrouper<>(Suppliers.ofInstance(combineBuffer), combineKeySerdeFactory.factorizeWithDictionary(dictionary), settableColumnSelectorFactory, combiningFactories, queryTimeoutAt);
    // init() must be called before iterator(), so cannot be called inside the below callable.
    grouper.init();
    final ListenableFuture future = executor.submit(new AbstractPrioritizedCallable<Void>(priority) {

        @Override
        public Void call() {
            try (CloseableIterator<Entry<KeyType>> mergedIterator = CloseableIterators.mergeSorted(iterators, keyObjComparator);
                // This variable is used to close releaser automatically.
                @SuppressWarnings("unused") final Releaser releaser = combineBufferHolder.increment()) {
                while (mergedIterator.hasNext()) {
                    final Entry<KeyType> next = mergedIterator.next();
                    settableColumnSelectorFactory.set(next.values);
                    // grouper always returns ok or throws an exception
                    grouper.aggregate(next.key);
                    settableColumnSelectorFactory.set(null);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            grouper.finish();
            return null;
        }
    });
    return new Pair<>(grouper.iterator(), future);
}
Also used : CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) IOException(java.io.IOException) Entry(org.apache.druid.query.groupby.epinephelinae.Grouper.Entry) Releaser(org.apache.druid.collections.Releaser) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Pair(org.apache.druid.java.util.common.Pair)

Example 10 with CloseableIterator

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

the class AvroOCFReader method intermediateRowIterator.

@Override
protected CloseableIterator<GenericRecord> intermediateRowIterator() throws IOException {
    final Closer closer = Closer.create();
    final byte[] buffer = new byte[InputEntity.DEFAULT_FETCH_BUFFER_SIZE];
    try {
        final InputEntity.CleanableFile file = closer.register(source.fetch(temporaryDirectory, buffer));
        final GenericDatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
        final DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(file.file(), datumReader);
        final Schema writerSchema = dataFileReader.getSchema();
        if (readerSchema == null) {
            readerSchema = writerSchema;
        }
        datumReader.setSchema(writerSchema);
        datumReader.setExpected(readerSchema);
        closer.register(dataFileReader);
        return new CloseableIterator<GenericRecord>() {

            @Override
            public boolean hasNext() {
                return dataFileReader.hasNext();
            }

            @Override
            public GenericRecord next() {
                return dataFileReader.next();
            }

            @Override
            public void close() throws IOException {
                closer.close();
            }
        };
    } catch (Exception e) {
        closer.close();
        throw new RuntimeException(e);
    }
}
Also used : Closer(org.apache.druid.java.util.common.io.Closer) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) InputRowSchema(org.apache.druid.data.input.InputRowSchema) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) DataFileReader(org.apache.avro.file.DataFileReader) InputEntity(org.apache.druid.data.input.InputEntity) GenericRecord(org.apache.avro.generic.GenericRecord)

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