|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.sources.v2.writer; |
| 19 | + |
| 20 | +import org.apache.spark.annotation.Evolving; |
| 21 | +import org.apache.spark.sql.sources.v2.SupportsBatchWrite; |
| 22 | +import org.apache.spark.sql.sources.v2.Table; |
| 23 | +import org.apache.spark.sql.types.StructType; |
| 24 | + |
| 25 | +/** |
| 26 | + * An interface for building the {@link BatchWrite}. Implementations can mix in some interfaces to |
| 27 | + * support different ways to write data to data sources. |
| 28 | + * |
| 29 | + * Unless modified by a mixin interface, the {@link BatchWrite} configured by this builder is to |
| 30 | + * append data without affecting existing data. |
| 31 | + */ |
| 32 | +@Evolving |
| 33 | +public interface WriteBuilder { |
| 34 | + |
| 35 | + /** |
| 36 | + * Passes the `queryId` from Spark to data source. `queryId` is a unique string of the query. It's |
| 37 | + * possible that there are many queries running at the same time, or a query is restarted and |
| 38 | + * resumed. {@link BatchWrite} can use this id to identify the query. |
| 39 | + * |
| 40 | + * @return a new builder with the `queryId`. By default it returns `this`, which means the given |
| 41 | + * `queryId` is ignored. Please override this method to take the `queryId`. |
| 42 | + */ |
| 43 | + default WriteBuilder withQueryId(String queryId) { |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Passes the schema of the input data from Spark to data source. |
| 49 | + * |
| 50 | + * @return a new builder with the `schema`. By default it returns `this`, which means the given |
| 51 | + * `schema` is ignored. Please override this method to take the `schema`. |
| 52 | + */ |
| 53 | + default WriteBuilder withInputDataSchema(StructType schema) { |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns a {@link BatchWrite} to write data to batch source. By default this method throws |
| 59 | + * exception, data sources must overwrite this method to provide an implementation, if the |
| 60 | + * {@link Table} that creates this scan implements {@link SupportsBatchWrite}. |
| 61 | + * |
| 62 | + * Note that, the returned {@link BatchWrite} can be null if the implementation supports SaveMode, |
| 63 | + * to indicate that no writing is needed. We can clean it up after removing |
| 64 | + * {@link SupportsSaveMode}. |
| 65 | + */ |
| 66 | + default BatchWrite buildForBatch() { |
| 67 | + throw new UnsupportedOperationException("Batch scans are not supported"); |
| 68 | + } |
| 69 | +} |
0 commit comments