mirror of https://github.com/apache/druid.git
add google cloud storage InputSource for native batch (#8907)
* add google cloud storage InputSource for native batch * rename * checkstyle * fix * fix spelling * review comments
This commit is contained in:
parent
baefc65f80
commit
074a45219d
|
@ -46,7 +46,7 @@ public class InputEntityIteratingReader implements InputSourceReader
|
|||
private final Iterator<InputEntity> sourceIterator;
|
||||
private final File temporaryDirectory;
|
||||
|
||||
InputEntityIteratingReader(
|
||||
public InputEntityIteratingReader(
|
||||
InputRowSchema inputRowSchema,
|
||||
InputFormat inputFormat,
|
||||
Stream<InputEntity> sourceStream,
|
||||
|
|
|
@ -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|
|
||||
|
||||
|
||||
<a name="input-source"></a>
|
||||
|
||||
## 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
|
||||
|
||||
<a name="firehose"></a>
|
||||
|
|
|
@ -117,7 +117,12 @@
|
|||
<artifactId>google-api-client</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.findbugs</groupId>
|
||||
<artifactId>jsr305</artifactId>
|
||||
<version>2.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Tests -->
|
||||
<dependency>
|
||||
<groupId>org.apache.druid</groupId>
|
||||
|
|
|
@ -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<Throwable> getFetchRetryCondition()
|
||||
{
|
||||
return GoogleUtils.GOOGLE_RETRY;
|
||||
}
|
||||
}
|
|
@ -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<URI>
|
||||
{
|
||||
private final GoogleStorage storage;
|
||||
private final List<URI> uris;
|
||||
|
||||
@JsonCreator
|
||||
public GoogleCloudStorageInputSource(
|
||||
@JacksonInject GoogleStorage storage,
|
||||
@JsonProperty("uris") List<URI> uris
|
||||
)
|
||||
{
|
||||
this.storage = storage;
|
||||
this.uris = uris;
|
||||
}
|
||||
|
||||
@JsonProperty("uris")
|
||||
public List<URI> getUris()
|
||||
{
|
||||
return uris;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Stream<InputSplit<URI>> 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<URI> withSplit(InputSplit<URI> 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);
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Throwable> GOOGLE_RETRY = e -> isRetryable(e);
|
||||
}
|
||||
|
|
|
@ -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<URI> uris = Arrays.asList(
|
||||
new URI("gs://foo/bar/file.gz"),
|
||||
new URI("gs://bar/foo/file2.gz")
|
||||
);
|
||||
|
||||
final List<URI> 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<URI> uris = Arrays.asList(
|
||||
URI.create("gs://foo/bar/file.gz"),
|
||||
URI.create("gs://bar/foo/file2.gz")
|
||||
);
|
||||
|
||||
GoogleCloudStorageInputSource inputSource = new GoogleCloudStorageInputSource(STORAGE, uris);
|
||||
|
||||
Stream<InputSplit<URI>> 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<? extends Module> getJacksonModules()
|
||||
{
|
||||
return ImmutableList.of(new SimpleModule());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(Binder binder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Provides
|
||||
public GoogleStorage getGoogleStorage()
|
||||
{
|
||||
return STORAGE;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<GoogleBlob> 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<? extends Module> getJacksonModules()
|
||||
{
|
||||
return ImmutableList.of(new SimpleModule());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(Binder binder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Provides
|
||||
public GoogleStorage getRestS3Service()
|
||||
{
|
||||
return STORAGE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -358,6 +358,7 @@ unmerged
|
|||
unparseable
|
||||
unparsed
|
||||
uptime
|
||||
uris
|
||||
v1
|
||||
v2
|
||||
vCPUs
|
||||
|
|
Loading…
Reference in New Issue