-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reuse transformer in stream indexing (#9625)
* Reuse transformer in stream indexing * remove unused method * memoize complied pattern
- Loading branch information
Showing
7 changed files
with
283 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 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
72 changes: 72 additions & 0 deletions
72
core/src/test/java/org/apache/druid/data/input/impl/RegexInputFormatTest.java
This file contains 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,72 @@ | ||
/* | ||
* 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 com.fasterxml.jackson.databind.jsontype.NamedType; | ||
import com.google.common.collect.ImmutableList; | ||
import org.apache.druid.data.input.InputFormat; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class RegexInputFormatTest | ||
{ | ||
private final ObjectMapper mapper; | ||
|
||
public RegexInputFormatTest() | ||
{ | ||
mapper = new ObjectMapper(); | ||
mapper.registerSubtypes(new NamedType(RegexInputFormat.class, "regex")); | ||
} | ||
|
||
@Test | ||
public void testSerde() throws IOException | ||
{ | ||
final RegexInputFormat expected = new RegexInputFormat( | ||
"//[^\\r\\n]*[\\r\\n]", | ||
"|", | ||
ImmutableList.of("col1", "col2", "col3") | ||
); | ||
|
||
final byte[] json = mapper.writeValueAsBytes(expected); | ||
final RegexInputFormat fromJson = (RegexInputFormat) mapper.readValue(json, InputFormat.class); | ||
|
||
Assert.assertEquals(expected.getPattern(), fromJson.getPattern()); | ||
Assert.assertEquals(expected.getListDelimiter(), fromJson.getListDelimiter()); | ||
Assert.assertEquals(expected.getColumns(), fromJson.getColumns()); | ||
} | ||
|
||
@Test | ||
public void testIgnoreCompiledPatternInJson() throws IOException | ||
{ | ||
final RegexInputFormat expected = new RegexInputFormat( | ||
"//[^\\r\\n]*[\\r\\n]", | ||
"|", | ||
ImmutableList.of("col1", "col2", "col3") | ||
); | ||
|
||
final byte[] json = mapper.writeValueAsBytes(expected); | ||
final Map<String, Object> map = mapper.readValue(json, Map.class); | ||
Assert.assertFalse(map.containsKey("compiledPattern")); | ||
} | ||
} |
This file contains 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
84 changes: 84 additions & 0 deletions
84
...vice/src/main/java/org/apache/druid/indexing/seekablestream/SettableByteEntityReader.java
This file contains 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,84 @@ | ||
/* | ||
* 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.indexing.seekablestream; | ||
|
||
import com.google.common.base.Preconditions; | ||
import org.apache.druid.data.input.InputEntityReader; | ||
import org.apache.druid.data.input.InputFormat; | ||
import org.apache.druid.data.input.InputRow; | ||
import org.apache.druid.data.input.InputRowListPlusRawValues; | ||
import org.apache.druid.data.input.InputRowSchema; | ||
import org.apache.druid.data.input.impl.ByteEntity; | ||
import org.apache.druid.java.util.common.parsers.CloseableIterator; | ||
import org.apache.druid.segment.transform.TransformSpec; | ||
import org.apache.druid.segment.transform.Transformer; | ||
import org.apache.druid.segment.transform.TransformingInputEntityReader; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* A settable {@link InputEntityReader}. This class is intended to be used for only stream parsing in Kafka or Kinesis | ||
* indexing. | ||
*/ | ||
class SettableByteEntityReader implements InputEntityReader | ||
{ | ||
private final InputFormat inputFormat; | ||
private final InputRowSchema inputRowSchema; | ||
private final Transformer transformer; | ||
private final File indexingTmpDir; | ||
|
||
private InputEntityReader delegate; | ||
|
||
SettableByteEntityReader( | ||
InputFormat inputFormat, | ||
InputRowSchema inputRowSchema, | ||
TransformSpec transformSpec, | ||
File indexingTmpDir | ||
) | ||
{ | ||
this.inputFormat = Preconditions.checkNotNull(inputFormat, "inputFormat"); | ||
this.inputRowSchema = inputRowSchema; | ||
this.transformer = transformSpec.toTransformer(); | ||
this.indexingTmpDir = indexingTmpDir; | ||
} | ||
|
||
void setEntity(ByteEntity entity) throws IOException | ||
{ | ||
this.delegate = new TransformingInputEntityReader( | ||
// Yes, we are creating a new reader for every stream chunk. | ||
// This should be fine as long as initializing a reader is cheap which it is for now. | ||
inputFormat.createReader(inputRowSchema, entity, indexingTmpDir), | ||
transformer | ||
); | ||
} | ||
|
||
@Override | ||
public CloseableIterator<InputRow> read() throws IOException | ||
{ | ||
return delegate.read(); | ||
} | ||
|
||
@Override | ||
public CloseableIterator<InputRowListPlusRawValues> sample() throws IOException | ||
{ | ||
return delegate.sample(); | ||
} | ||
} |
Oops, something went wrong.