-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add "lines" input format. #18433
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
Merged
Merged
Add "lines" input format. #18433
Changes from all commits
Commits
Show all changes
2 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
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
60 changes: 60 additions & 0 deletions
60
processing/src/main/java/org/apache/druid/data/input/impl/LinesInputFormat.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,60 @@ | ||
| /* | ||
| * 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.impl; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import org.apache.druid.data.input.InputEntity; | ||
| import org.apache.druid.data.input.InputEntityReader; | ||
| import org.apache.druid.data.input.InputFormat; | ||
| import org.apache.druid.data.input.InputRowSchema; | ||
| import org.apache.druid.utils.CompressionUtils; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| /** | ||
| * Input format that breaks the input on newlines, and returns a single column named {@link LinesReader#LINE_COLUMN}. | ||
| */ | ||
| public class LinesInputFormat implements InputFormat | ||
| { | ||
| public static final String TYPE_KEY = "lines"; | ||
|
|
||
| @JsonCreator | ||
| public LinesInputFormat() | ||
| { | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSplittable() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public InputEntityReader createReader(InputRowSchema inputRowSchema, InputEntity source, File temporaryDirectory) | ||
| { | ||
| return new LinesReader(inputRowSchema, source); | ||
| } | ||
|
|
||
| @Override | ||
| public long getWeightedSize(String path, long size) | ||
| { | ||
| return size * CompressionUtils.estimatedCompressionFactor(CompressionUtils.Format.fromFileName(path)); | ||
| } | ||
| } |
80 changes: 80 additions & 0 deletions
80
processing/src/main/java/org/apache/druid/data/input/impl/LinesReader.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,80 @@ | ||
| /* | ||
| * 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.impl; | ||
|
|
||
| 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.TextReader; | ||
| import org.apache.druid.java.util.common.parsers.ParseException; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Reader for {@link LinesInputFormat}. | ||
| */ | ||
| public class LinesReader extends TextReader.Strings | ||
| { | ||
| private static final String LINE_COLUMN = "line"; | ||
|
|
||
| LinesReader( | ||
| InputRowSchema inputRowSchema, | ||
| InputEntity source | ||
| ) | ||
| { | ||
| super(inputRowSchema, source); | ||
| } | ||
|
|
||
| @Override | ||
| public List<InputRow> parseInputRows(String intermediateRow) throws ParseException | ||
| { | ||
| return List.of(MapInputRowParser.parse(getInputRowSchema(), parseLine(intermediateRow))); | ||
| } | ||
|
|
||
| @Override | ||
| protected List<Map<String, Object>> toMap(String intermediateRow) | ||
| { | ||
| return List.of(parseLine(intermediateRow)); | ||
| } | ||
|
|
||
| private Map<String, Object> parseLine(String line) | ||
| { | ||
| return Map.of(LINE_COLUMN, line); | ||
| } | ||
|
|
||
| @Override | ||
| public int getNumHeaderLinesToSkip() | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean needsToProcessHeaderLine() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void processHeaderLine(String line) | ||
| { | ||
| // Nothing to do. | ||
| } | ||
| } |
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
79 changes: 79 additions & 0 deletions
79
processing/src/test/java/org/apache/druid/data/input/impl/LinesInputFormatTest.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,79 @@ | ||
| /* | ||
| * 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.impl; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.apache.druid.data.input.InputFormat; | ||
| import org.apache.druid.segment.TestHelper; | ||
| import org.apache.druid.utils.CompressionUtils; | ||
| import org.hamcrest.MatcherAssert; | ||
| import org.hamcrest.Matchers; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| public class LinesInputFormatTest | ||
| { | ||
| private ObjectMapper mapper; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception | ||
| { | ||
| mapper = TestHelper.makeJsonMapper(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSerde() throws IOException | ||
| { | ||
| final LinesInputFormat expected = new LinesInputFormat(); | ||
|
|
||
| final byte[] json = mapper.writeValueAsBytes(expected); | ||
|
|
||
| // Read as map | ||
| final Map<String, Object> map = mapper.readValue(json, Map.class); | ||
| Assert.assertEquals("lines", map.get("type")); | ||
|
|
||
| // Read as InputFormat | ||
| final InputFormat fromJson = mapper.readValue(json, InputFormat.class); | ||
| MatcherAssert.assertThat(fromJson, Matchers.instanceOf(LinesInputFormat.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_getWeightedSize_withoutCompression() | ||
| { | ||
| final LinesInputFormat format = new LinesInputFormat(); | ||
| final long unweightedSize = 100L; | ||
| Assert.assertEquals(unweightedSize, format.getWeightedSize("file.txt", unweightedSize)); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_getWeightedSize_withGzCompression() | ||
| { | ||
| final LinesInputFormat format = new LinesInputFormat(); | ||
| final long unweightedSize = 100L; | ||
| Assert.assertEquals( | ||
| unweightedSize * CompressionUtils.COMPRESSED_TEXT_WEIGHT_FACTOR, | ||
| format.getWeightedSize("file.txt.gz", unweightedSize) | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Perhaps we can extend
CompressionUtils.estimatedCompressionFactor()or add a thin wrapper that accepts thepathdirectly and returns the compression factor - internally invokingCompressionUtils.Format.fromFileName()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.
IMO these are equivalently useful for what we need them for right now, so I'll keep it the way I had it.
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.
thanks for reviewing!