|
| 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.file.writer; |
| 19 | + |
| 20 | +import org.apache.seatunnel.shade.com.typesafe.config.ConfigFactory; |
| 21 | + |
| 22 | +import org.apache.seatunnel.api.source.Collector; |
| 23 | +import org.apache.seatunnel.api.table.catalog.CatalogTableUtil; |
| 24 | +import org.apache.seatunnel.api.table.type.BasicType; |
| 25 | +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
| 26 | +import org.apache.seatunnel.api.table.type.SeaTunnelRow; |
| 27 | +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
| 28 | +import org.apache.seatunnel.connectors.seatunnel.file.config.FileFormat; |
| 29 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.config.FileSinkConfig; |
| 30 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.writer.CsvWriteStrategy; |
| 31 | +import org.apache.seatunnel.connectors.seatunnel.file.source.reader.CsvReadStrategy; |
| 32 | + |
| 33 | +import org.junit.jupiter.api.Assertions; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 36 | +import org.junit.jupiter.api.condition.OS; |
| 37 | + |
| 38 | +import lombok.extern.slf4j.Slf4j; |
| 39 | + |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | + |
| 45 | +import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.FS_DEFAULT_NAME_DEFAULT; |
| 46 | + |
| 47 | +@Slf4j |
| 48 | +public class CsvWriteStrategyTest { |
| 49 | + private static final String TMP_PATH = "file:///tmp/seatunnel/csv/test"; |
| 50 | + |
| 51 | + @DisabledOnOs(OS.WINDOWS) |
| 52 | + @Test |
| 53 | + public void testParquetWriteInt96() throws Exception { |
| 54 | + Map<String, Object> writeConfig = new HashMap<>(); |
| 55 | + writeConfig.put("tmp_path", TMP_PATH); |
| 56 | + writeConfig.put("path", "file:///tmp/seatunnel/csv/int96"); |
| 57 | + writeConfig.put("file_format_type", FileFormat.CSV.name()); |
| 58 | + |
| 59 | + SeaTunnelRowType writeRowType = |
| 60 | + new SeaTunnelRowType( |
| 61 | + new String[] {"id", "name", "age"}, |
| 62 | + new SeaTunnelDataType[] { |
| 63 | + BasicType.INT_TYPE, BasicType.STRING_TYPE, BasicType.INT_TYPE |
| 64 | + }); |
| 65 | + FileSinkConfig writeSinkConfig = |
| 66 | + new FileSinkConfig(ConfigFactory.parseMap(writeConfig), writeRowType); |
| 67 | + CsvWriteStrategy writeStrategy = new CsvWriteStrategy(writeSinkConfig); |
| 68 | + ParquetReadStrategyTest.LocalConf hadoopConf = |
| 69 | + new ParquetReadStrategyTest.LocalConf(FS_DEFAULT_NAME_DEFAULT); |
| 70 | + writeStrategy.setCatalogTable( |
| 71 | + CatalogTableUtil.getCatalogTable("test", null, null, "test", writeRowType)); |
| 72 | + writeStrategy.init(hadoopConf, "test1", "test1", 0); |
| 73 | + writeStrategy.beginTransaction(1L); |
| 74 | + writeStrategy.write(new SeaTunnelRow(new Object[] {1, "a", 20})); |
| 75 | + writeStrategy.finishAndCloseFile(); |
| 76 | + writeStrategy.close(); |
| 77 | + |
| 78 | + CsvReadStrategy readStrategy = new CsvReadStrategy(); |
| 79 | + readStrategy.init(hadoopConf); |
| 80 | + List<String> readFiles = readStrategy.getFileNamesByPath(TMP_PATH); |
| 81 | + readStrategy.setPluginConfig(ConfigFactory.empty()); |
| 82 | + readStrategy.setCatalogTable( |
| 83 | + CatalogTableUtil.getCatalogTable( |
| 84 | + "test", |
| 85 | + new SeaTunnelRowType( |
| 86 | + new String[] {"id", "name", "age"}, |
| 87 | + new SeaTunnelDataType[] { |
| 88 | + BasicType.INT_TYPE, BasicType.STRING_TYPE, BasicType.INT_TYPE |
| 89 | + }))); |
| 90 | + Assertions.assertEquals(1, readFiles.size()); |
| 91 | + String readFilePath = readFiles.get(0); |
| 92 | + List<SeaTunnelRow> readRows = new ArrayList<>(); |
| 93 | + Collector<SeaTunnelRow> readCollector = |
| 94 | + new Collector<SeaTunnelRow>() { |
| 95 | + @Override |
| 96 | + public void collect(SeaTunnelRow record) { |
| 97 | + Assertions.assertEquals(1, record.getField(0)); |
| 98 | + Assertions.assertEquals("a", record.getField(1)); |
| 99 | + Assertions.assertEquals(20, record.getField(2)); |
| 100 | + readRows.add(record); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Object getCheckpointLock() { |
| 105 | + return null; |
| 106 | + } |
| 107 | + }; |
| 108 | + readStrategy.read(readFilePath, "test", readCollector); |
| 109 | + Assertions.assertEquals(1, readRows.size()); |
| 110 | + readStrategy.close(); |
| 111 | + } |
| 112 | +} |
0 commit comments