diff --git a/core/src/main/java/org/apache/druid/data/input/impl/InputEntityIteratingReader.java b/core/src/main/java/org/apache/druid/data/input/impl/InputEntityIteratingReader.java index 385bc5f1459..1869756eaa1 100644 --- a/core/src/main/java/org/apache/druid/data/input/impl/InputEntityIteratingReader.java +++ b/core/src/main/java/org/apache/druid/data/input/impl/InputEntityIteratingReader.java @@ -46,7 +46,7 @@ public class InputEntityIteratingReader implements InputSourceReader private final Iterator sourceIterator; private final File temporaryDirectory; - InputEntityIteratingReader( + public InputEntityIteratingReader( InputRowSchema inputRowSchema, InputFormat inputFormat, Stream sourceStream, diff --git a/docs/development/extensions-core/google.md b/docs/development/extensions-core/google.md index 90402d728dc..6dafa6ad046 100644 --- a/docs/development/extensions-core/google.md +++ b/docs/development/extensions-core/google.md @@ -38,6 +38,34 @@ Deep storage can be written to Google Cloud Storage either via this extension or |`druid.google.prefix`||GCS prefix.|No-prefix| + + +## Google cloud storage batch ingestion input source + +This extension also provides an input source for Druid native batch ingestion to support reading objects directly from Google Cloud Storage. Objects can be specified as list of Google Cloud Storage URI strings. The Google Cloud Storage input source is splittable and can be used by [native parallel index tasks](../../ingestion/native-batch.md#parallel-task), where each worker task of `index_parallel` will read a single object. + +```json +... + "ioConfig": { + "type": "index_parallel", + "inputSource": { + "type": "google", + "uris": ["gs://foo/bar/file.json", "gs://bar/foo/file2.json"] + }, + "inputFormat": { + "type": "json" + }, + ... + }, +... +``` + +|property|description|default|required?| +|--------|-----------|-------|---------| +|type|This should be `google`.|N/A|yes| +|uris|JSON array of URIs where Google Cloud Storage files to be ingested are located.|N/A|yes| + + ## Firehose diff --git a/extensions-core/google-extensions/pom.xml b/extensions-core/google-extensions/pom.xml index c94b1c76c84..d98910b2718 100644 --- a/extensions-core/google-extensions/pom.xml +++ b/extensions-core/google-extensions/pom.xml @@ -117,7 +117,12 @@ google-api-client provided - + + com.google.code.findbugs + jsr305 + 2.0.1 + provided + org.apache.druid diff --git a/extensions-core/google-extensions/src/main/java/org/apache/druid/data/input/google/GoogleCloudStorageEntity.java b/extensions-core/google-extensions/src/main/java/org/apache/druid/data/input/google/GoogleCloudStorageEntity.java new file mode 100644 index 00000000000..5a3256eb374 --- /dev/null +++ b/extensions-core/google-extensions/src/main/java/org/apache/druid/data/input/google/GoogleCloudStorageEntity.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.data.input.google; + +import com.google.common.base.Predicate; +import org.apache.druid.data.input.InputEntity; +import org.apache.druid.storage.google.GoogleByteSource; +import org.apache.druid.storage.google.GoogleStorage; +import org.apache.druid.storage.google.GoogleUtils; +import org.apache.druid.utils.CompressionUtils; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +public class GoogleCloudStorageEntity implements InputEntity +{ + private final GoogleStorage storage; + private final URI uri; + + GoogleCloudStorageEntity(GoogleStorage storage, URI uri) + { + this.storage = storage; + this.uri = uri; + } + + @Nullable + @Override + public URI getUri() + { + return uri; + } + + @Override + public InputStream open() throws IOException + { + // Get data of the given object and open an input stream + final String bucket = uri.getAuthority(); + final String key = GoogleUtils.extractGoogleCloudStorageObjectKey(uri); + final GoogleByteSource byteSource = new GoogleByteSource(storage, bucket, key); + return CompressionUtils.decompress(byteSource.openStream(), uri.getPath()); + } + + @Override + public Predicate getFetchRetryCondition() + { + return GoogleUtils.GOOGLE_RETRY; + } +} diff --git a/extensions-core/google-extensions/src/main/java/org/apache/druid/data/input/google/GoogleCloudStorageInputSource.java b/extensions-core/google-extensions/src/main/java/org/apache/druid/data/input/google/GoogleCloudStorageInputSource.java new file mode 100644 index 00000000000..1bc99ad8b4b --- /dev/null +++ b/extensions-core/google-extensions/src/main/java/org/apache/druid/data/input/google/GoogleCloudStorageInputSource.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.data.input.google; + +import com.fasterxml.jackson.annotation.JacksonInject; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.collect.ImmutableList; +import org.apache.druid.data.input.AbstractInputSource; +import org.apache.druid.data.input.InputFormat; +import org.apache.druid.data.input.InputRowSchema; +import org.apache.druid.data.input.InputSourceReader; +import org.apache.druid.data.input.InputSplit; +import org.apache.druid.data.input.SplitHintSpec; +import org.apache.druid.data.input.impl.InputEntityIteratingReader; +import org.apache.druid.data.input.impl.SplittableInputSource; +import org.apache.druid.storage.google.GoogleStorage; + +import javax.annotation.Nullable; +import java.io.File; +import java.net.URI; +import java.util.List; +import java.util.Objects; +import java.util.stream.Stream; + +public class GoogleCloudStorageInputSource extends AbstractInputSource implements SplittableInputSource +{ + private final GoogleStorage storage; + private final List uris; + + @JsonCreator + public GoogleCloudStorageInputSource( + @JacksonInject GoogleStorage storage, + @JsonProperty("uris") List uris + ) + { + this.storage = storage; + this.uris = uris; + } + + @JsonProperty("uris") + public List getUris() + { + return uris; + } + + + @Override + public Stream> createSplits(InputFormat inputFormat, @Nullable SplitHintSpec splitHintSpec) + { + return uris.stream().map(InputSplit::new); + } + + @Override + public int getNumSplits(InputFormat inputFormat, @Nullable SplitHintSpec splitHintSpec) + { + return uris.size(); + } + + @Override + public SplittableInputSource withSplit(InputSplit split) + { + return new GoogleCloudStorageInputSource(storage, ImmutableList.of(split.get())); + } + + @Override + public boolean needsFormat() + { + return true; + } + + @Override + protected InputSourceReader formattableReader( + InputRowSchema inputRowSchema, + InputFormat inputFormat, + @Nullable File temporaryDirectory + ) + { + return new InputEntityIteratingReader( + inputRowSchema, + inputFormat, + createSplits(inputFormat, null).map(split -> new GoogleCloudStorageEntity(storage, split.get())), + temporaryDirectory + ); + } + + @Override + public boolean equals(Object o) + { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + GoogleCloudStorageInputSource that = (GoogleCloudStorageInputSource) o; + return Objects.equals(uris, that.uris); + } + + @Override + public int hashCode() + { + return Objects.hash(uris); + } +} diff --git a/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleStorageDruidModule.java b/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleStorageDruidModule.java index 74a58c41919..c30ce073579 100644 --- a/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleStorageDruidModule.java +++ b/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleStorageDruidModule.java @@ -30,6 +30,7 @@ import com.google.api.services.storage.Storage; import com.google.common.collect.ImmutableList; import com.google.inject.Binder; import com.google.inject.Provides; +import org.apache.druid.data.input.google.GoogleCloudStorageInputSource; import org.apache.druid.firehose.google.StaticGoogleBlobStoreFirehoseFactory; import org.apache.druid.guice.Binders; import org.apache.druid.guice.JsonConfigProvider; @@ -41,7 +42,7 @@ import java.util.List; public class GoogleStorageDruidModule implements DruidModule { - public static final String SCHEME = "google"; + static final String SCHEME = "google"; private static final Logger LOG = new Logger(GoogleStorageDruidModule.class); private static final String APPLICATION_NAME = "druid-google-extensions"; @@ -72,7 +73,9 @@ public class GoogleStorageDruidModule implements DruidModule } }, new SimpleModule().registerSubtypes( - new NamedType(StaticGoogleBlobStoreFirehoseFactory.class, "static-google-blobstore")) + new NamedType(StaticGoogleBlobStoreFirehoseFactory.class, "static-google-blobstore"), + new NamedType(GoogleCloudStorageInputSource.class, SCHEME) + ) ); } diff --git a/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleUtils.java b/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleUtils.java index c3d3e4acb90..1dd12ef2388 100644 --- a/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleUtils.java +++ b/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleUtils.java @@ -20,12 +20,13 @@ package org.apache.druid.storage.google; import com.google.api.client.http.HttpResponseException; +import com.google.common.base.Predicate; import java.io.IOException; +import java.net.URI; public class GoogleUtils { - public static boolean isRetryable(Throwable t) { if (t instanceof HttpResponseException) { @@ -34,4 +35,11 @@ public class GoogleUtils } return t instanceof IOException; } + + public static String extractGoogleCloudStorageObjectKey(URI uri) + { + return uri.getPath().startsWith("/") ? uri.getPath().substring(1) : uri.getPath(); + } + + public static final Predicate GOOGLE_RETRY = e -> isRetryable(e); } diff --git a/extensions-core/google-extensions/src/test/java/org/apache/druid/data/input/google/GoogleCloudStorageInputSourceTest.java b/extensions-core/google-extensions/src/test/java/org/apache/druid/data/input/google/GoogleCloudStorageInputSourceTest.java new file mode 100644 index 00000000000..a3b77afd890 --- /dev/null +++ b/extensions-core/google-extensions/src/test/java/org/apache/druid/data/input/google/GoogleCloudStorageInputSourceTest.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.data.input.google; + +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.module.guice.ObjectMapperModule; +import com.google.common.collect.ImmutableList; +import com.google.inject.Binder; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Provides; +import org.apache.druid.data.input.InputSplit; +import org.apache.druid.data.input.impl.JsonInputFormat; +import org.apache.druid.initialization.DruidModule; +import org.apache.druid.jackson.DefaultObjectMapper; +import org.apache.druid.java.util.common.parsers.JSONPathSpec; +import org.apache.druid.storage.google.GoogleStorage; +import org.junit.Assert; +import org.junit.Test; + +import java.net.URI; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class GoogleCloudStorageInputSourceTest +{ + private static final GoogleStorage STORAGE = new GoogleStorage(null); + + @Test + public void testSerde() throws Exception + { + final ObjectMapper mapper = createGoogleObjectMapper(); + + final List uris = Arrays.asList( + new URI("gs://foo/bar/file.gz"), + new URI("gs://bar/foo/file2.gz") + ); + + final List prefixes = Arrays.asList( + new URI("gs://foo/bar"), + new URI("gs://bar/foo") + ); + + final GoogleCloudStorageInputSource withUris = new GoogleCloudStorageInputSource(STORAGE, uris); + final GoogleCloudStorageInputSource serdeWithUris = + mapper.readValue(mapper.writeValueAsString(withUris), GoogleCloudStorageInputSource.class); + Assert.assertEquals(withUris, serdeWithUris); + } + + @Test + public void testWithUrisSplit() + { + final List uris = Arrays.asList( + URI.create("gs://foo/bar/file.gz"), + URI.create("gs://bar/foo/file2.gz") + ); + + GoogleCloudStorageInputSource inputSource = new GoogleCloudStorageInputSource(STORAGE, uris); + + Stream> splits = inputSource.createSplits( + new JsonInputFormat(JSONPathSpec.DEFAULT, null), + null + ); + Assert.assertEquals(uris, splits.map(InputSplit::get).collect(Collectors.toList())); + } + + public static ObjectMapper createGoogleObjectMapper() + { + final DruidModule baseModule = new TestGoogleModule(); + final ObjectMapper baseMapper = new DefaultObjectMapper(); + baseModule.getJacksonModules().forEach(baseMapper::registerModule); + + final Injector injector = Guice.createInjector( + new ObjectMapperModule(), + baseModule + ); + return injector.getInstance(ObjectMapper.class); + } + + private static class TestGoogleModule implements DruidModule + { + @Override + public List getJacksonModules() + { + return ImmutableList.of(new SimpleModule()); + } + + @Override + public void configure(Binder binder) + { + + } + + @Provides + public GoogleStorage getGoogleStorage() + { + return STORAGE; + } + } +} diff --git a/extensions-core/google-extensions/src/test/java/org/apache/druid/firehose/google/StaticGoogleBlobStoreFirehoseFactoryTest.java b/extensions-core/google-extensions/src/test/java/org/apache/druid/firehose/google/StaticGoogleBlobStoreFirehoseFactoryTest.java index 5bb60d774c0..c9996b53ccb 100644 --- a/extensions-core/google-extensions/src/test/java/org/apache/druid/firehose/google/StaticGoogleBlobStoreFirehoseFactoryTest.java +++ b/extensions-core/google-extensions/src/test/java/org/apache/druid/firehose/google/StaticGoogleBlobStoreFirehoseFactoryTest.java @@ -19,17 +19,9 @@ package org.apache.druid.firehose.google; -import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.module.guice.ObjectMapperModule; import com.google.common.collect.ImmutableList; -import com.google.inject.Binder; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.Provides; -import org.apache.druid.initialization.DruidModule; -import org.apache.druid.jackson.DefaultObjectMapper; +import org.apache.druid.data.input.google.GoogleCloudStorageInputSourceTest; import org.apache.druid.storage.google.GoogleStorage; import org.junit.Assert; import org.junit.Test; @@ -44,7 +36,7 @@ public class StaticGoogleBlobStoreFirehoseFactoryTest @Test public void testSerde() throws IOException { - final ObjectMapper mapper = createObjectMapper(new TestGoogleModule()); + final ObjectMapper mapper = GoogleCloudStorageInputSourceTest.createGoogleObjectMapper(); final List blobs = ImmutableList.of( new GoogleBlob("foo", "bar"), @@ -68,37 +60,4 @@ public class StaticGoogleBlobStoreFirehoseFactoryTest Assert.assertEquals(factory, outputFact); } - - private static ObjectMapper createObjectMapper(DruidModule baseModule) - { - final ObjectMapper baseMapper = new DefaultObjectMapper(); - baseModule.getJacksonModules().forEach(baseMapper::registerModule); - - final Injector injector = Guice.createInjector( - new ObjectMapperModule(), - baseModule - ); - return injector.getInstance(ObjectMapper.class); - } - - private static class TestGoogleModule implements DruidModule - { - @Override - public List getJacksonModules() - { - return ImmutableList.of(new SimpleModule()); - } - - @Override - public void configure(Binder binder) - { - - } - - @Provides - public GoogleStorage getRestS3Service() - { - return STORAGE; - } - } } diff --git a/website/.spelling b/website/.spelling index c177e709b17..d28328196e8 100644 --- a/website/.spelling +++ b/website/.spelling @@ -358,6 +358,7 @@ unmerged unparseable unparsed uptime +uris v1 v2 vCPUs