Search in sources :

Example 1 with JsonIterator

use of org.apache.druid.data.input.impl.prefetch.JsonIterator in project druid by druid-io.

the class PrefetchSqlFirehoseFactory method connect.

@Override
public Firehose connect(InputRowParser<Map<String, Object>> firehoseParser, @Nullable File temporaryDirectory) {
    if (objects == null) {
        objects = ImmutableList.copyOf(Preconditions.checkNotNull(initObjects(), "objects"));
    }
    if (cacheManager.isEnabled() || fetchConfig.getMaxFetchCapacityBytes() > 0) {
        Preconditions.checkNotNull(temporaryDirectory, "temporaryDirectory");
        Preconditions.checkArgument(temporaryDirectory.exists(), "temporaryDirectory[%s] does not exist", temporaryDirectory);
        Preconditions.checkArgument(temporaryDirectory.isDirectory(), "temporaryDirectory[%s] is not a directory", temporaryDirectory);
    }
    LOG.info("Create a new firehose for [%d] queries", objects.size());
    // fetchExecutor is responsible for background data fetching
    final ExecutorService fetchExecutor = Execs.singleThreaded("firehose_fetch_%d");
    final Fetcher<T> fetcher = new SqlFetcher<>(cacheManager, objects, fetchExecutor, temporaryDirectory, fetchConfig, new ObjectOpenFunction<T>() {

        @Override
        public InputStream open(T object, File outFile) throws IOException {
            return openObjectStream(object, outFile);
        }

        @Override
        public InputStream open(T object) throws IOException {
            final File outFile = File.createTempFile("sqlresults_", null, temporaryDirectory);
            return openObjectStream(object, outFile);
        }
    });
    return new SqlFirehose(new Iterator<JsonIterator<Map<String, Object>>>() {

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

        @Override
        public JsonIterator<Map<String, Object>> next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            try {
                TypeReference<Map<String, Object>> type = new TypeReference<Map<String, Object>>() {
                };
                final OpenObject<T> openObject = fetcher.next();
                final InputStream stream = openObject.getObjectStream();
                return new JsonIterator<>(type, stream, openObject.getResourceCloser(), objectMapper);
            } catch (Exception ioe) {
                throw new RuntimeException(ioe);
            }
        }
    }, firehoseParser, () -> {
        fetchExecutor.shutdownNow();
        try {
            Preconditions.checkState(fetchExecutor.awaitTermination(fetchConfig.getFetchTimeout(), TimeUnit.MILLISECONDS));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ISE("Failed to shutdown fetch executor during close");
        }
    });
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) OpenObject(org.apache.druid.data.input.impl.prefetch.OpenObject) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) ExecutorService(java.util.concurrent.ExecutorService) JsonIterator(org.apache.druid.data.input.impl.prefetch.JsonIterator) OpenObject(org.apache.druid.data.input.impl.prefetch.OpenObject) ISE(org.apache.druid.java.util.common.ISE) TypeReference(com.fasterxml.jackson.core.type.TypeReference) File(java.io.File) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with JsonIterator

use of org.apache.druid.data.input.impl.prefetch.JsonIterator in project druid by druid-io.

the class SqlFirehoseTest method testFirehoseStringParser.

@Test
public void testFirehoseStringParser() throws Exception {
    final TestCloseable closeable = new TestCloseable();
    List<Object> expectedResults = new ArrayList<>();
    for (Map<String, Object> map : inputs) {
        expectedResults.add(map.get("x"));
    }
    final List<JsonIterator<Map<String, Object>>> lineIterators = fileList.stream().map(s -> new JsonIterator<Map<String, Object>>(TYPE_REF, s, closeable, objectMapper)).collect(Collectors.toList());
    final InputRowParser stringParser = TransformSpec.NONE.decorate(new StringInputRowParser(new TimeAndDimsParseSpec(new TimestampSpec("timestamp", "auto", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("x")))), Charset.defaultCharset().name()));
    try (final SqlFirehose firehose = new SqlFirehose(lineIterators.iterator(), stringParser, closeable)) {
        final List<Object> results = new ArrayList<>();
        while (firehose.hasMore()) {
            final InputRow inputRow = firehose.nextRow();
            if (inputRow == null) {
                results.add(null);
            } else {
                results.add(inputRow.getDimension("x").get(0));
            }
        }
        Assert.assertEquals(expectedResults, results);
    }
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) TimeAndDimsParseSpec(org.apache.druid.data.input.impl.TimeAndDimsParseSpec) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Charset(java.nio.charset.Charset) StringInputRowParser(org.apache.druid.data.input.impl.StringInputRowParser) After(org.junit.After) Map(java.util.Map) ExpressionTransform(org.apache.druid.segment.transform.ExpressionTransform) TypeReference(com.fasterxml.jackson.core.type.TypeReference) FileUtils(org.apache.druid.java.util.common.FileUtils) Before(org.junit.Before) JsonIterator(org.apache.druid.data.input.impl.prefetch.JsonIterator) ImmutableMap(com.google.common.collect.ImmutableMap) MapInputRowParser(org.apache.druid.data.input.impl.MapInputRowParser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileOutputStream(java.io.FileOutputStream) DimensionsSpec(org.apache.druid.data.input.impl.DimensionsSpec) Test(org.junit.Test) IOException(java.io.IOException) InputRowParser(org.apache.druid.data.input.impl.InputRowParser) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) File(java.io.File) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) InputRow(org.apache.druid.data.input.InputRow) List(java.util.List) TransformingStringInputRowParser(org.apache.druid.segment.transform.TransformingStringInputRowParser) Closeable(java.io.Closeable) Assert(org.junit.Assert) TransformSpec(org.apache.druid.segment.transform.TransformSpec) ArrayList(java.util.ArrayList) TimeAndDimsParseSpec(org.apache.druid.data.input.impl.TimeAndDimsParseSpec) StringInputRowParser(org.apache.druid.data.input.impl.StringInputRowParser) TransformingStringInputRowParser(org.apache.druid.segment.transform.TransformingStringInputRowParser) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) InputRow(org.apache.druid.data.input.InputRow) DimensionsSpec(org.apache.druid.data.input.impl.DimensionsSpec) JsonIterator(org.apache.druid.data.input.impl.prefetch.JsonIterator) StringInputRowParser(org.apache.druid.data.input.impl.StringInputRowParser) MapInputRowParser(org.apache.druid.data.input.impl.MapInputRowParser) InputRowParser(org.apache.druid.data.input.impl.InputRowParser) TransformingStringInputRowParser(org.apache.druid.segment.transform.TransformingStringInputRowParser) Test(org.junit.Test)

Example 3 with JsonIterator

use of org.apache.druid.data.input.impl.prefetch.JsonIterator in project druid by druid-io.

the class SqlFirehoseTest method testFirehose.

@Test
public void testFirehose() throws Exception {
    final TestCloseable closeable = new TestCloseable();
    List<Object> expectedResults = new ArrayList<>();
    for (Map<String, Object> map : inputs) {
        expectedResults.add(map.get("x"));
    }
    final List<JsonIterator<Map<String, Object>>> lineIterators = fileList.stream().map(s -> new JsonIterator<Map<String, Object>>(TYPE_REF, s, closeable, objectMapper)).collect(Collectors.toList());
    try (final SqlFirehose firehose = new SqlFirehose(lineIterators.iterator(), parser, closeable)) {
        final List<Object> results = new ArrayList<>();
        while (firehose.hasMore()) {
            final InputRow inputRow = firehose.nextRow();
            if (inputRow == null) {
                results.add(null);
            } else {
                results.add(inputRow.getDimension("x").get(0));
            }
        }
        Assert.assertEquals(expectedResults, results);
    }
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) TimeAndDimsParseSpec(org.apache.druid.data.input.impl.TimeAndDimsParseSpec) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Charset(java.nio.charset.Charset) StringInputRowParser(org.apache.druid.data.input.impl.StringInputRowParser) After(org.junit.After) Map(java.util.Map) ExpressionTransform(org.apache.druid.segment.transform.ExpressionTransform) TypeReference(com.fasterxml.jackson.core.type.TypeReference) FileUtils(org.apache.druid.java.util.common.FileUtils) Before(org.junit.Before) JsonIterator(org.apache.druid.data.input.impl.prefetch.JsonIterator) ImmutableMap(com.google.common.collect.ImmutableMap) MapInputRowParser(org.apache.druid.data.input.impl.MapInputRowParser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileOutputStream(java.io.FileOutputStream) DimensionsSpec(org.apache.druid.data.input.impl.DimensionsSpec) Test(org.junit.Test) IOException(java.io.IOException) InputRowParser(org.apache.druid.data.input.impl.InputRowParser) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) File(java.io.File) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) InputRow(org.apache.druid.data.input.InputRow) List(java.util.List) TransformingStringInputRowParser(org.apache.druid.segment.transform.TransformingStringInputRowParser) Closeable(java.io.Closeable) Assert(org.junit.Assert) TransformSpec(org.apache.druid.segment.transform.TransformSpec) ArrayList(java.util.ArrayList) InputRow(org.apache.druid.data.input.InputRow) JsonIterator(org.apache.druid.data.input.impl.prefetch.JsonIterator) Test(org.junit.Test)

Example 4 with JsonIterator

use of org.apache.druid.data.input.impl.prefetch.JsonIterator in project druid by druid-io.

the class SqlFirehoseTest method testClose.

@Test
public void testClose() throws IOException {
    File file = File.createTempFile("test", "", TEST_DIR);
    final TestCloseable closeable = new TestCloseable();
    try (FileOutputStream fos = new FileOutputStream(file)) {
        final JsonGenerator jg = objectMapper.getFactory().createGenerator(fos);
        jg.writeStartArray();
        jg.writeEndArray();
        jg.close();
    }
    final JsonIterator<Map<String, Object>> jsonIterator = new JsonIterator<>(TYPE_REF, new FileInputStream(file), closeable, objectMapper);
    final SqlFirehose firehose = new SqlFirehose(ImmutableList.of(jsonIterator).iterator(), parser, closeable);
    // initialize lineIterator
    firehose.hasMore();
    firehose.close();
    Assert.assertTrue(closeable.closed);
}
Also used : FileOutputStream(java.io.FileOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonIterator(org.apache.druid.data.input.impl.prefetch.JsonIterator) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 5 with JsonIterator

use of org.apache.druid.data.input.impl.prefetch.JsonIterator in project druid by druid-io.

the class SqlReader method intermediateRowIterator.

@Override
protected CloseableIterator<Map<String, Object>> intermediateRowIterator() throws IOException {
    final Closer closer = Closer.create();
    // The results are fetched into local storage as this avoids having to keep a persistent database connection for a long time
    final InputEntity.CleanableFile resultFile = closer.register(source.fetch(temporaryDirectory, null));
    FileInputStream inputStream = new FileInputStream(resultFile.file());
    JsonIterator<Map<String, Object>> jsonIterator = new JsonIterator<>(new TypeReference<Map<String, Object>>() {
    }, inputStream, closer, objectMapper);
    return jsonIterator;
}
Also used : Closer(org.apache.druid.java.util.common.io.Closer) InputEntity(org.apache.druid.data.input.InputEntity) JsonIterator(org.apache.druid.data.input.impl.prefetch.JsonIterator) Map(java.util.Map) FileInputStream(java.io.FileInputStream)

Aggregations

Map (java.util.Map)6 JsonIterator (org.apache.druid.data.input.impl.prefetch.JsonIterator)6 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 SmileFactory (com.fasterxml.jackson.dataformat.smile.SmileFactory)3 ImmutableList (com.google.common.collect.ImmutableList)3 Closeable (java.io.Closeable)3 Charset (java.nio.charset.Charset)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 InputRow (org.apache.druid.data.input.InputRow)3 DimensionsSpec (org.apache.druid.data.input.impl.DimensionsSpec)3