-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add thrift input format #11360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add thrift input format #11360
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,6 +141,36 @@ | |
| <artifactId>hamcrest-core</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.code.findbugs</groupId> | ||
| <artifactId>jsr305</artifactId> | ||
| <version>2.0.1</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>commons-io</groupId> | ||
| <artifactId>commons-io</artifactId> | ||
| <version>2.9.0</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>joda-time</groupId> | ||
| <artifactId>joda-time</artifactId> | ||
| <version>2.10.5</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-core</artifactId> | ||
| <version>2.10.2</version> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think versions on a lot of these should be already defined in the top level pom (the dependency checker in travis sometimes suggests more than is necessary to fix the issue) |
||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.druid</groupId> | ||
| <artifactId>druid-processing</artifactId> | ||
| <version>0.22.0-SNAPSHOT</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...thrift-extensions/src/main/java/org/apache/druid/data/input/thrift/ThriftInputFormat.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * 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.thrift; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.google.common.base.Preconditions; | ||
| import org.apache.druid.data.input.InputEntity; | ||
| import org.apache.druid.data.input.InputEntityReader; | ||
| import org.apache.druid.data.input.InputRowSchema; | ||
| import org.apache.druid.data.input.impl.NestedInputFormat; | ||
| import org.apache.druid.java.util.common.parsers.JSONPathSpec; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import java.io.File; | ||
| import java.util.Objects; | ||
|
|
||
| public class ThriftInputFormat extends NestedInputFormat | ||
| { | ||
| private final String jarPath; | ||
| private final String thriftClassName; | ||
|
|
||
| @JsonCreator | ||
| public ThriftInputFormat( | ||
| @JsonProperty("flattenSpec") @Nullable JSONPathSpec flattenSpec, | ||
| @JsonProperty("thriftJar") String jarPath, | ||
| @JsonProperty("thriftClass") String thriftClassName | ||
| ) | ||
| { | ||
| super(flattenSpec); | ||
| this.jarPath = jarPath; | ||
| this.thriftClassName = thriftClassName; | ||
| Preconditions.checkNotNull(thriftClassName, "thrift class name"); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSplittable() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public String getThriftJar() | ||
| { | ||
| return jarPath; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public String getThriftClass() | ||
| { | ||
| return thriftClassName; | ||
| } | ||
|
|
||
| @Override | ||
| public InputEntityReader createReader(InputRowSchema inputRowSchema, InputEntity source, File temporaryDirectory) | ||
| { | ||
| return new ThriftReader( | ||
| inputRowSchema, | ||
| source, | ||
| jarPath, | ||
| thriftClassName, | ||
| getFlattenSpec() | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(final Object o) | ||
| { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| final ThriftInputFormat that = (ThriftInputFormat) o; | ||
| return Objects.equals(getFlattenSpec(), that.getFlattenSpec()) && | ||
| Objects.equals(jarPath, that.jarPath) && | ||
| Objects.equals(thriftClassName, that.thriftClassName); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() | ||
| { | ||
| return Objects.hash(jarPath, thriftClassName, getFlattenSpec()); | ||
| } | ||
|
|
||
| } |
156 changes: 156 additions & 0 deletions
156
...trib/thrift-extensions/src/main/java/org/apache/druid/data/input/thrift/ThriftReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * 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.thrift; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonFactory; | ||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.MappingIterator; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.google.common.collect.FluentIterable; | ||
| import com.google.common.collect.Iterators; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.apache.druid.data.input.InputEntity; | ||
| import org.apache.druid.data.input.InputRow; | ||
| import org.apache.druid.data.input.InputRowSchema; | ||
| import org.apache.druid.data.input.IntermediateRowParsingReader; | ||
| import org.apache.druid.data.input.impl.MapInputRowParser; | ||
| import org.apache.druid.java.util.common.CloseableIterators; | ||
| import org.apache.druid.java.util.common.IAE; | ||
| import org.apache.druid.java.util.common.parsers.CloseableIterator; | ||
| import org.apache.druid.java.util.common.parsers.JSONFlattenerMaker; | ||
| import org.apache.druid.java.util.common.parsers.JSONPathSpec; | ||
| import org.apache.druid.java.util.common.parsers.ObjectFlattener; | ||
| import org.apache.druid.java.util.common.parsers.ObjectFlatteners; | ||
| import org.apache.druid.java.util.common.parsers.ParseException; | ||
| import org.apache.druid.utils.CollectionUtils; | ||
| import org.apache.thrift.TBase; | ||
| import org.apache.thrift.TException; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.net.URLClassLoader; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class ThriftReader extends IntermediateRowParsingReader<String> | ||
| { | ||
| private final InputEntity source; | ||
| private final String jarPath; | ||
| private final String thriftClassName; | ||
| private volatile Class<TBase> thriftClass = null; | ||
| private final ObjectMapper objectMapper; | ||
| private final ObjectFlattener<JsonNode> flattener; | ||
| private final InputRowSchema inputRowSchema; | ||
|
|
||
|
|
||
| ThriftReader( | ||
| InputRowSchema inputRowSchema, | ||
| InputEntity source, | ||
| String jarPath, | ||
| String thriftClassName, | ||
| JSONPathSpec flattenSpec | ||
| ) | ||
| { | ||
| this.inputRowSchema = inputRowSchema; | ||
| this.flattener = ObjectFlatteners.create(flattenSpec, new JSONFlattenerMaker(true)); | ||
| this.source = source; | ||
| this.jarPath = jarPath; | ||
| this.thriftClassName = thriftClassName; | ||
| this.objectMapper = new ObjectMapper(); | ||
| if (thriftClass == null) { | ||
| try { | ||
| thriftClass = getThriftClass(); | ||
| } | ||
| catch (IOException e) { | ||
| throw new IAE(e, "failed to load jar [%s]", jarPath); | ||
| } | ||
| catch (ClassNotFoundException e) { | ||
| throw new IAE(e, "class [%s] not found in jar", thriftClassName); | ||
| } | ||
| catch (InstantiationException | IllegalAccessException e) { | ||
| throw new IAE(e, "instantiation thrift instance failed"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public Class<TBase> getThriftClass() | ||
| throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException | ||
| { | ||
| final Class<TBase> thrift; | ||
| if (jarPath != null) { | ||
| File jar = new File(jarPath); | ||
| URLClassLoader child = new URLClassLoader( | ||
| new URL[] {jar.toURI().toURL()}, | ||
| this.getClass().getClassLoader() | ||
| ); | ||
| thrift = (Class<TBase>) Class.forName(thriftClassName, true, child); | ||
| } else { | ||
| thrift = (Class<TBase>) Class.forName(thriftClassName); | ||
| } | ||
| thrift.newInstance(); | ||
| return thrift; | ||
| } | ||
|
|
||
| @Override | ||
| protected CloseableIterator<String> intermediateRowIterator() throws IOException | ||
| { | ||
| final String json; | ||
| try { | ||
| final byte[] bytes = IOUtils.toByteArray(source.open()); | ||
| TBase o = thriftClass.newInstance(); | ||
| ThriftDeserialization.detectAndDeserialize(bytes, o); | ||
| json = ThriftDeserialization.SERIALIZER_SIMPLE_JSON.get().toString(o); | ||
| } | ||
| catch (IllegalAccessException | InstantiationException | TException e) { | ||
| throw new IAE("some thing wrong with your thrift?"); | ||
| } | ||
|
|
||
| return CloseableIterators.withEmptyBaggage( | ||
| Iterators.singletonIterator(json) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| protected List<InputRow> parseInputRows(String intermediateRow) throws IOException, ParseException | ||
| { | ||
| final List<InputRow> inputRows; | ||
| JsonParser parser = new JsonFactory().createParser(intermediateRow); | ||
| final MappingIterator<JsonNode> delegate = this.objectMapper.readValues(parser, JsonNode.class); | ||
| inputRows = FluentIterable.from(() -> delegate) | ||
| .transform(jsonNode -> MapInputRowParser.parse(inputRowSchema, flattener.flatten(jsonNode))) | ||
| .toList(); | ||
| if (CollectionUtils.isNullOrEmpty(inputRows)) { | ||
| throw new ParseException("Unable to parse [%s] as the intermediateRow resulted in empty input row", intermediateRow); | ||
| } | ||
| return inputRows; | ||
| } | ||
|
|
||
| @Override | ||
| protected List<Map<String, Object>> toMap(String intermediateRow) throws IOException | ||
| { | ||
| JsonParser parser = new JsonFactory().createParser(intermediateRow); | ||
| final MappingIterator<Map> delegate = objectMapper.readValues(parser, Map.class); | ||
| return FluentIterable.from(() -> delegate) | ||
| .transform(map -> (Map<String, Object>) map) | ||
| .toList(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, I'm not sure we have any other 'contrib' extensions described in this section, it might be best if this lives in https://github.com/apache/druid/blob/master/docs/development/extensions-contrib/thrift.md for now. On the other hand, thrift i think is the only data format that isn't a core extension (maybe in the future we should just consider adding integration tests and making it a core extension?), so maybe it is ok to be here. @techdocsmith do you have any thoughts?
Also, looking closer at the code, I guess this might also work with batch ingestion too since the deserializer detects the format based on the bytes given to it, though I haven't personally used this extension or tested this scenario. I'll see if I can find some time to pull your branch and test it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for @clintropolis suggestion to keep the doc in /docs/development/extensions-contrib/thrift.md until the extension is made core.