-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add InputSource and InputFormat interfaces #8823
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
Changes from all commits
095bb32
f52f967
93ab23f
e0b80cb
b4f041e
d349db5
f308f13
d451582
b7c8b87
e942a21
08d7872
c70af75
546d957
7bb5d5f
6dba81a
ea2c8f9
1ea7758
218b392
7098056
7381277
2a3b114
355777c
e466ea9
87b83fa
c1c3fb9
169ab49
a9d167a
42a6965
230803b
540759b
ce88049
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.io.File; | ||
|
|
||
| /** | ||
| * Abstract class for {@link InputSource}. This class provides a default implementation of {@link #reader} with | ||
| * a sanity check. Child classes should implement one of {@link #formattableReader} or {@link #fixedFormatReader} | ||
| * depending on {@link #needsFormat()}. | ||
| */ | ||
| public abstract class AbstractInputSource implements InputSource | ||
| { | ||
| @Override | ||
| public InputSourceReader reader( | ||
| InputRowSchema inputRowSchema, | ||
| @Nullable InputFormat inputFormat, | ||
| @Nullable File temporaryDirectory | ||
| ) | ||
| { | ||
| if (needsFormat()) { | ||
| return formattableReader( | ||
| inputRowSchema, | ||
| Preconditions.checkNotNull(inputFormat, "inputFormat"), | ||
| temporaryDirectory | ||
| ); | ||
| } else { | ||
| return fixedFormatReader(inputRowSchema, temporaryDirectory); | ||
| } | ||
| } | ||
|
|
||
| protected InputSourceReader formattableReader( | ||
| InputRowSchema inputRowSchema, | ||
| InputFormat inputFormat, | ||
| @Nullable File temporaryDirectory | ||
| ) | ||
| { | ||
| throw new UnsupportedOperationException("Implement this method properly if needsFormat() = true"); | ||
| } | ||
|
|
||
| protected InputSourceReader fixedFormatReader(InputRowSchema inputRowSchema, @Nullable File temporaryDirectory) | ||
| { | ||
| throw new UnsupportedOperationException("Implement this method properly if needsFormat() = false"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,13 +74,13 @@ public interface Firehose extends Closeable | |
| * | ||
| * @return an InputRowPlusRaw which may contain any of: an InputRow, the raw data, or a ParseException | ||
|
Contributor
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. javadoc for
Contributor
Author
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. This method is only for sampler and will be removed in the follow-up pr. |
||
| */ | ||
| default InputRowPlusRaw nextRowWithRaw() throws IOException | ||
| default InputRowListPlusJson nextRowWithRaw() throws IOException | ||
| { | ||
| try { | ||
| return InputRowPlusRaw.of(nextRow(), null); | ||
| return InputRowListPlusJson.of(nextRow(), null); | ||
| } | ||
| catch (ParseException e) { | ||
| return InputRowPlusRaw.of(null, e); | ||
| return InputRowListPlusJson.of((byte[]) null, e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import org.apache.druid.data.input.impl.FirehoseToInputSourceReaderAdaptor; | ||
| import org.apache.druid.data.input.impl.InputRowParser; | ||
| import org.apache.druid.data.input.impl.SplittableInputSource; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class FirehoseFactoryToInputSourceAdaptor extends AbstractInputSource implements SplittableInputSource | ||
| { | ||
| private final FiniteFirehoseFactory firehoseFactory; | ||
| private final InputRowParser inputRowParser; | ||
|
|
||
| public FirehoseFactoryToInputSourceAdaptor(FiniteFirehoseFactory firehoseFactory, InputRowParser inputRowParser) | ||
| { | ||
| this.firehoseFactory = firehoseFactory; | ||
| this.inputRowParser = Preconditions.checkNotNull(inputRowParser, "inputRowParser"); | ||
| } | ||
|
|
||
| public FiniteFirehoseFactory getFirehoseFactory() | ||
| { | ||
| return firehoseFactory; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSplittable() | ||
| { | ||
| return firehoseFactory.isSplittable(); | ||
| } | ||
|
|
||
| @Override | ||
| public Stream<InputSplit> createSplits(InputFormat inputFormat, @Nullable SplitHintSpec splitHintSpec) | ||
| throws IOException | ||
| { | ||
| if (firehoseFactory.isSplittable()) { | ||
| return firehoseFactory.getSplits(splitHintSpec); | ||
| } else { | ||
| throw new UnsupportedOperationException(); | ||
|
Contributor
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. Is supporting unsplittable
Contributor
Author
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. No, only splittable firehose can create splits. |
||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int getNumSplits(InputFormat inputFormat, @Nullable SplitHintSpec splitHintSpec) throws IOException | ||
| { | ||
| if (firehoseFactory.isSplittable()) { | ||
| return firehoseFactory.getNumSplits(splitHintSpec); | ||
| } else { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public SplittableInputSource withSplit(InputSplit split) | ||
| { | ||
| if (firehoseFactory.isSplittable()) { | ||
| return new FirehoseFactoryToInputSourceAdaptor( | ||
| firehoseFactory.withSplit(split), | ||
| inputRowParser | ||
| ); | ||
| } else { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean needsFormat() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| protected InputSourceReader fixedFormatReader(InputRowSchema inputRowSchema, @Nullable File temporaryDirectory) | ||
| { | ||
| return new FirehoseToInputSourceReaderAdaptor(firehoseFactory, inputRowParser, temporaryDirectory); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import com.google.common.base.Predicate; | ||
| import org.apache.druid.guice.annotations.UnstableApi; | ||
| import org.apache.druid.java.util.common.FileUtils; | ||
| import org.apache.druid.java.util.common.StringUtils; | ||
| import org.apache.druid.java.util.common.logger.Logger; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.io.Closeable; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.URI; | ||
|
|
||
| /** | ||
| * InputEntity abstracts an input entity and knows how to read bytes from the given entity. | ||
| */ | ||
| @UnstableApi | ||
| public interface InputEntity | ||
| { | ||
| Logger LOG = new Logger(InputEntity.class); | ||
|
|
||
| int DEFAULT_FETCH_BUFFER_SIZE = 4 * 1024; // 4 KB | ||
| int DEFAULT_MAX_NUM_FETCH_TRIES = 3; // 3 tries including the initial try | ||
|
|
||
| /** | ||
| * CleanableFile is the result type of {@link #fetch}. | ||
| * It should clean up any temporary resource on {@link #close()}. | ||
| */ | ||
| interface CleanableFile extends Closeable | ||
| { | ||
| File file(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns an URI to identify the input entity. Implementations can return null if they don't have | ||
| * an unique URI. | ||
| */ | ||
| @Nullable | ||
| URI getUri(); | ||
|
|
||
| /** | ||
| * Opens an {@link InputStream} on the input entity directly. | ||
| * This is the basic way to read the given entity. | ||
| * | ||
| * @see #fetch as an alternative way to read data. | ||
| */ | ||
| InputStream open() throws IOException; | ||
|
|
||
| /** | ||
| * Fetches the input entity into the local storage. | ||
| * This method might be preferred instead of {@link #open()}, for example | ||
| * | ||
| * - {@link InputFormat} requires expensive random access on remote storage. | ||
| * - Holding a connection until you consume the entire InputStream is expensive. | ||
| * | ||
| * @param temporaryDirectory to store temp data. This directory will be removed automatically once | ||
| * the task finishes. | ||
| * @param fetchBuffer is used to fetch remote entity into local storage. | ||
| * | ||
| * @see FileUtils#copyLarge | ||
| */ | ||
| default CleanableFile fetch(File temporaryDirectory, byte[] fetchBuffer) throws IOException | ||
| { | ||
| final File tempFile = File.createTempFile("druid-input-entity", ".tmp", temporaryDirectory); | ||
| LOG.debug("Fetching entity into file[%s]", tempFile.getAbsolutePath()); | ||
| try (InputStream is = open()) { | ||
| FileUtils.copyLarge( | ||
| is, | ||
| tempFile, | ||
| fetchBuffer, | ||
| getFetchRetryCondition(), | ||
| DEFAULT_MAX_NUM_FETCH_TRIES, | ||
| StringUtils.format("Failed to fetch into [%s]", tempFile.getAbsolutePath()) | ||
| ); | ||
| } | ||
|
|
||
| return new CleanableFile() | ||
| { | ||
| @Override | ||
| public File file() | ||
| { | ||
| return tempFile; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() | ||
| { | ||
| if (!tempFile.delete()) { | ||
| LOG.warn("Failed to remove file[%s]", tempFile.getAbsolutePath()); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * {@link #fetch} will retry during the fetch if it sees an exception matching to the returned predicate. | ||
| */ | ||
| Predicate<Throwable> getFetchRetryCondition(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.ObjectWriter; | ||
| import org.apache.druid.guice.annotations.UnstableApi; | ||
| import org.apache.druid.java.util.common.parsers.CloseableIterator; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * InputEntityReader knows how to parse data into {@link InputRow}. | ||
| * This class is <i>stateful</i> and a new InputEntityReader should be created per {@link InputEntity}. | ||
| * | ||
| * @see IntermediateRowParsingReader | ||
| * @see TextReader | ||
| */ | ||
| @UnstableApi | ||
| public interface InputEntityReader | ||
| { | ||
| /** | ||
| * Default JSON writer for sampler. This writer can be used to create an {@link InputRowListPlusJson}. | ||
| * Note that this writer uses the default serializer of Jackson. You may want to create a custom writer | ||
| * to serialize your custom types. | ||
| */ | ||
| ObjectWriter DEFAULT_JSON_WRITER = new ObjectMapper().writerWithDefaultPrettyPrinter(); | ||
|
|
||
| CloseableIterator<InputRow> read(InputEntity source, File temporaryDirectory) throws IOException; | ||
|
|
||
| CloseableIterator<InputRowListPlusJson> sample(InputEntity source, File temporaryDirectory) throws IOException; | ||
| } |
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.
suggest adding a link to InputSource to show what's replacing this
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.
👍 Added.