|
| 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.seatunnel.connectors.seatunnel.maxcompute.sink; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.configuration.ReadonlyConfig; |
| 21 | +import org.apache.seatunnel.api.sink.DataSaveMode; |
| 22 | +import org.apache.seatunnel.api.sink.DefaultSaveModeHandler; |
| 23 | +import org.apache.seatunnel.api.sink.SchemaSaveMode; |
| 24 | +import org.apache.seatunnel.api.table.catalog.Catalog; |
| 25 | +import org.apache.seatunnel.api.table.catalog.CatalogTable; |
| 26 | +import org.apache.seatunnel.connectors.seatunnel.maxcompute.catalog.MaxComputeCatalog; |
| 27 | + |
| 28 | +import org.apache.commons.lang3.StringUtils; |
| 29 | + |
| 30 | +import com.aliyun.odps.PartitionSpec; |
| 31 | + |
| 32 | +import static org.apache.seatunnel.connectors.seatunnel.maxcompute.config.MaxcomputeConfig.PARTITION_SPEC; |
| 33 | + |
| 34 | +public class MaxComputeSaveModeHandler extends DefaultSaveModeHandler { |
| 35 | + |
| 36 | + private final ReadonlyConfig readonlyConfig; |
| 37 | + |
| 38 | + public MaxComputeSaveModeHandler( |
| 39 | + SchemaSaveMode schemaSaveMode, |
| 40 | + DataSaveMode dataSaveMode, |
| 41 | + Catalog catalog, |
| 42 | + CatalogTable catalogTable, |
| 43 | + String customSql, |
| 44 | + ReadonlyConfig readonlyConfig) { |
| 45 | + super(schemaSaveMode, dataSaveMode, catalog, catalogTable, customSql); |
| 46 | + this.readonlyConfig = readonlyConfig; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void createSchemaWhenNotExist() { |
| 51 | + super.createSchemaWhenNotExist(); |
| 52 | + if (StringUtils.isNotEmpty(readonlyConfig.get(PARTITION_SPEC))) { |
| 53 | + ((MaxComputeCatalog) catalog) |
| 54 | + .createPartition( |
| 55 | + tablePath, new PartitionSpec(readonlyConfig.get(PARTITION_SPEC))); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void recreateSchema() { |
| 61 | + super.recreateSchema(); |
| 62 | + if (StringUtils.isNotEmpty(readonlyConfig.get(PARTITION_SPEC))) { |
| 63 | + ((MaxComputeCatalog) catalog) |
| 64 | + .createPartition( |
| 65 | + tablePath, new PartitionSpec(readonlyConfig.get(PARTITION_SPEC))); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments