-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-22732] Add Structured Streaming APIs to DataSourceV2 #19925
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
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
cbf7837
add tests
jose-torres 10ac599
writer impl
jose-torres 5594f7a
rm useless writer
jose-torres a364fa6
rm weird docs
jose-torres 41d732c
shuffle around public interfaces
jose-torres 60c12c2
fix imports
jose-torres 57db413
put deserialize in reader so we don't have to port SerializedOffset
jose-torres 93b6976
off by one errors grr
jose-torres abd20db
document right semantics
jose-torres 3629d27
document checkpoint location
jose-torres d021f31
add getStart to continuous and clarify semantics
jose-torres 052808a
cleanup offset set/get docs
jose-torres 7a7638b
cleanup reader docs
jose-torres 81efbee
explain getOffset
jose-torres a9c43d9
fix fmt
jose-torres 3f03f50
fix doc
jose-torres 6338043
note interfaces are temporary
jose-torres 8949571
fix wording
jose-torres df9f224
lifecycle
jose-torres ddaee34
fix offset semantic implementation
jose-torres 1608444
remove unneeded restriction
jose-torres 49525b4
deserializeOffset
jose-torres a9fbf33
add copyright headers
jose-torres 22d07cd
rebase against datasource package change
jose-torres 0b68873
refer properly to sink
jose-torres f924a8a
widen tolerance interval in continuous rate source test
jose-torres 4d166de
widen tolerance interval
jose-torres 7c46b33
clarify start offset
jose-torres 8a2a4f1
add docs
jose-torres 8809bf9
fix problems
jose-torres 0974ac3
even timestamps
jose-torres c1a6322
unify offsets
jose-torres 3cb6cee
undo spurious rename
jose-torres 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
42 changes: 42 additions & 0 deletions
42
sql/core/src/main/java/org/apache/spark/sql/sources/v2/ContinuousReadSupport.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,42 @@ | ||
/* | ||
* 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.spark.sql.sources.v2; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.spark.sql.sources.v2.reader.ContinuousReader; | ||
import org.apache.spark.sql.sources.v2.reader.DataSourceV2Reader; | ||
import org.apache.spark.sql.types.StructType; | ||
|
||
/** | ||
* A mix-in interface for {@link DataSourceV2}. Data sources can implement this interface to | ||
* provide data reading ability for continuous stream processing. | ||
*/ | ||
public interface ContinuousReadSupport extends DataSourceV2 { | ||
/** | ||
* Creates a {@link ContinuousReader} to scan the data from this data source. | ||
* | ||
* @param schema the user provided schema, or empty() if none was provided | ||
* @param checkpointLocation a path to Hadoop FS scratch space that can be used for failure | ||
* recovery. Readers for the same logical source in the same query | ||
* will be given the same checkpointLocation. | ||
* @param options the options for the returned data source reader, which is an immutable | ||
* case-insensitive string-to-string map. | ||
*/ | ||
ContinuousReader createContinuousReader(Optional<StructType> schema, String checkpointLocation, DataSourceV2Options options); | ||
} |
54 changes: 54 additions & 0 deletions
54
sql/core/src/main/java/org/apache/spark/sql/sources/v2/ContinuousWriteSupport.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,54 @@ | ||
/* | ||
* 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.spark.sql.sources.v2; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.spark.annotation.InterfaceStability; | ||
import org.apache.spark.sql.execution.streaming.BaseStreamingSink; | ||
import org.apache.spark.sql.sources.v2.writer.ContinuousWriter; | ||
import org.apache.spark.sql.sources.v2.writer.DataSourceV2Writer; | ||
import org.apache.spark.sql.streaming.OutputMode; | ||
import org.apache.spark.sql.types.StructType; | ||
|
||
/** | ||
* A mix-in interface for {@link DataSourceV2}. Data sources can implement this interface to | ||
* provide data writing ability for continuous stream processing. | ||
*/ | ||
@InterfaceStability.Evolving | ||
public interface ContinuousWriteSupport extends BaseStreamingSink { | ||
|
||
/** | ||
* Creates an optional {@link ContinuousWriter} to save the data to this data source. Data | ||
* sources can return None if there is no writing needed to be done. | ||
* | ||
* @param queryId A unique string for the writing query. It's possible that there are many writing | ||
* queries running at the same time, and the returned {@link DataSourceV2Writer} | ||
* can use this id to distinguish itself from others. | ||
* @param schema the schema of the data to be written. | ||
* @param mode the output mode which determines what successive epoch output means to this | ||
* sink, please refer to {@link OutputMode} for more details. | ||
* @param options the options for the returned data source writer, which is an immutable | ||
* case-insensitive string-to-string map. | ||
*/ | ||
Optional<ContinuousWriter> createContinuousWriter( | ||
String queryId, | ||
StructType schema, | ||
OutputMode mode, | ||
DataSourceV2Options options); | ||
} |
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
52 changes: 52 additions & 0 deletions
52
sql/core/src/main/java/org/apache/spark/sql/sources/v2/MicroBatchReadSupport.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,52 @@ | ||
/* | ||
* 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.spark.sql.sources.v2; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.spark.annotation.InterfaceStability; | ||
import org.apache.spark.sql.sources.v2.reader.MicroBatchReader; | ||
import org.apache.spark.sql.types.StructType; | ||
|
||
/** | ||
* A mix-in interface for {@link DataSourceV2}. Data sources can implement this interface to | ||
* provide streaming micro-batch data reading ability. | ||
*/ | ||
@InterfaceStability.Evolving | ||
public interface MicroBatchReadSupport extends DataSourceV2 { | ||
/** | ||
* Creates a {@link MicroBatchReader} to read batches of data from this data source in a | ||
* streaming query. | ||
* | ||
* The execution engine will create a micro-batch reader at the start of a streaming query, | ||
* alternate calls to setOffsetRange and createReadTasks for each batch to process, and then | ||
* call stop() when the execution is complete. Note that a single query may have multiple | ||
* executions due to restart or failure recovery. | ||
* | ||
* @param schema the user provided schema, or empty() if none was provided | ||
* @param checkpointLocation a path to Hadoop FS scratch space that can be used for failure | ||
* recovery. Readers for the same logical source in the same query | ||
* will be given the same checkpointLocation. | ||
* @param options the options for the returned data source reader, which is an immutable | ||
* case-insensitive string-to-string map. | ||
*/ | ||
MicroBatchReader createMicroBatchReader( | ||
Optional<StructType> schema, | ||
String checkpointLocation, | ||
DataSourceV2Options options); | ||
} |
58 changes: 58 additions & 0 deletions
58
sql/core/src/main/java/org/apache/spark/sql/sources/v2/MicroBatchWriteSupport.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,58 @@ | ||
/* | ||
* 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.spark.sql.sources.v2; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.spark.annotation.InterfaceStability; | ||
import org.apache.spark.sql.execution.streaming.BaseStreamingSink; | ||
import org.apache.spark.sql.sources.v2.writer.DataSourceV2Writer; | ||
import org.apache.spark.sql.streaming.OutputMode; | ||
import org.apache.spark.sql.types.StructType; | ||
|
||
/** | ||
* A mix-in interface for {@link DataSourceV2}. Data sources can implement this interface to | ||
* provide data writing ability and save the data from a microbatch to the data source. | ||
*/ | ||
@InterfaceStability.Evolving | ||
public interface MicroBatchWriteSupport extends BaseStreamingSink { | ||
|
||
/** | ||
* Creates an optional {@link DataSourceV2Writer} to save the data to this data source. Data | ||
* sources can return None if there is no writing needed to be done. | ||
* | ||
* @param queryId A unique string for the writing query. It's possible that there are many writing | ||
* queries running at the same time, and the returned {@link DataSourceV2Writer} | ||
* can use this id to distinguish itself from others. | ||
* @param epochId The uniquenumeric ID of the batch within this writing query. This is an | ||
* incrementing counter representing a consistent set of data; the same batch may | ||
* be started multiple times in failure recovery scenarios, but it will always | ||
* contain the same records. | ||
* @param schema the schema of the data to be written. | ||
* @param mode the output mode which determines what successive batch output means to this | ||
* sink, please refer to {@link OutputMode} for more details. | ||
* @param options the options for the returned data source writer, which is an immutable | ||
* case-insensitive string-to-string map. | ||
*/ | ||
Optional<DataSourceV2Writer> createMicroBatchWriter( | ||
String queryId, | ||
long epochId, | ||
StructType schema, | ||
OutputMode mode, | ||
DataSourceV2Options options); | ||
} |
36 changes: 36 additions & 0 deletions
36
sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/ContinuousDataReader.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,36 @@ | ||
/* | ||
* 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.spark.sql.sources.v2.reader; | ||
|
||
import org.apache.spark.sql.sources.v2.reader.PartitionOffset; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* A variation on {@link DataReader} for use with streaming in continuous processing mode. | ||
*/ | ||
public interface ContinuousDataReader<T> extends DataReader<T> { | ||
/** | ||
* Get the offset of the current record, or the start offset if no records have been read. | ||
* | ||
* The execution engine will call this method along with get() to keep track of the current | ||
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. better to use a real java doc link, e.g. |
||
* offset. When an epoch ends, the offset of the previous record in each partition will be saved | ||
* as a restart checkpoint. | ||
*/ | ||
PartitionOffset getOffset(); | ||
} |
68 changes: 68 additions & 0 deletions
68
sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/ContinuousReader.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,68 @@ | ||
/* | ||
* 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.spark.sql.sources.v2.reader; | ||
|
||
import org.apache.spark.sql.sources.v2.reader.PartitionOffset; | ||
import org.apache.spark.sql.execution.streaming.BaseStreamingSource; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* A mix-in interface for {@link DataSourceV2Reader}. Data source readers can implement this | ||
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. It's not a mix-in interface but a variation on |
||
* interface to allow reading in a continuous processing mode stream. | ||
* | ||
* Implementations must ensure each read task output is a {@link ContinuousDataReader}. | ||
*/ | ||
public interface ContinuousReader extends BaseStreamingSource, DataSourceV2Reader { | ||
/** | ||
* Merge offsets coming from {@link ContinuousDataReader} instances in each partition to | ||
* a single global offset. | ||
*/ | ||
Offset mergeOffsets(PartitionOffset[] offsets); | ||
|
||
/** | ||
* Deserialize a JSON string into an Offset of the implementation-defined offset type. | ||
* @throws IllegalArgumentException if the JSON does not encode a valid offset for this reader | ||
*/ | ||
Offset deserializeOffset(String json); | ||
|
||
/** | ||
* Set the desired start offset for read tasks created from this reader. The scan will start | ||
* from the first record after the provided offset, or from an implementation-defined inferred | ||
* starting point if no offset is provided. | ||
*/ | ||
void setOffset(Optional<Offset> start); | ||
|
||
/** | ||
* Return the specified or inferred start offset for this reader. | ||
* | ||
* @throws IllegalStateException if setOffset has not been called | ||
*/ | ||
Offset getStartOffset(); | ||
|
||
/** | ||
* The execution engine will call this method in every epoch to determine if new read tasks need | ||
* to be generated, which may be required if for example the underlying source system has had | ||
* partitions added or removed. | ||
* | ||
* If true, the query will be shut down and restarted with a new reader. | ||
*/ | ||
default boolean needsReconfiguration() { | ||
return false; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
typo:
unique numeric