|
| 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.fake.source; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.table.type.ArrayType; |
| 21 | +import org.apache.seatunnel.api.table.type.BasicType; |
| 22 | +import org.apache.seatunnel.api.table.type.DecimalType; |
| 23 | +import org.apache.seatunnel.api.table.type.MapType; |
| 24 | +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
| 25 | +import org.apache.seatunnel.api.table.type.SeaTunnelRow; |
| 26 | +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
| 27 | +import org.apache.seatunnel.connectors.seatunnel.common.schema.SeaTunnelSchema; |
| 28 | + |
| 29 | +import org.apache.commons.lang3.RandomStringUtils; |
| 30 | +import org.apache.commons.lang3.RandomUtils; |
| 31 | + |
| 32 | +import java.lang.reflect.Array; |
| 33 | +import java.math.BigDecimal; |
| 34 | +import java.time.LocalDateTime; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +public class FakeDataGenerator { |
| 40 | + public static final String SCHEMA = "schema"; |
| 41 | + private final SeaTunnelSchema schema; |
| 42 | + private final FakeConfig fakeConfig; |
| 43 | + |
| 44 | + public FakeDataGenerator(SeaTunnelSchema schema, FakeConfig fakeConfig) { |
| 45 | + this.schema = schema; |
| 46 | + this.fakeConfig = fakeConfig; |
| 47 | + } |
| 48 | + |
| 49 | + private SeaTunnelRow randomRow() { |
| 50 | + SeaTunnelRowType seaTunnelRowType = schema.getSeaTunnelRowType(); |
| 51 | + String[] fieldNames = seaTunnelRowType.getFieldNames(); |
| 52 | + SeaTunnelDataType<?>[] fieldTypes = seaTunnelRowType.getFieldTypes(); |
| 53 | + List<Object> randomRow = new ArrayList<>(fieldNames.length); |
| 54 | + for (SeaTunnelDataType<?> fieldType : fieldTypes) { |
| 55 | + randomRow.add(randomColumnValue(fieldType)); |
| 56 | + } |
| 57 | + return new SeaTunnelRow(randomRow.toArray()); |
| 58 | + } |
| 59 | + |
| 60 | + public List<SeaTunnelRow> generateFakedRows() { |
| 61 | + ArrayList<SeaTunnelRow> seaTunnelRows = new ArrayList<>(); |
| 62 | + for (int i = 0; i < fakeConfig.getRowNum(); i++) { |
| 63 | + seaTunnelRows.add(randomRow()); |
| 64 | + } |
| 65 | + return seaTunnelRows; |
| 66 | + } |
| 67 | + |
| 68 | + @SuppressWarnings("magicnumber") |
| 69 | + private Object randomColumnValue(SeaTunnelDataType<?> fieldType) { |
| 70 | + switch (fieldType.getSqlType()) { |
| 71 | + case ARRAY: |
| 72 | + ArrayType<?, ?> arrayType = (ArrayType<?, ?>) fieldType; |
| 73 | + BasicType<?> elementType = arrayType.getElementType(); |
| 74 | + int length = fakeConfig.getArraySize(); |
| 75 | + Object array = Array.newInstance(elementType.getTypeClass(), length); |
| 76 | + for (int i = 0; i < length; i++) { |
| 77 | + Object value = randomColumnValue(elementType); |
| 78 | + Array.set(array, i, value); |
| 79 | + } |
| 80 | + return array; |
| 81 | + case MAP: |
| 82 | + MapType<?, ?> mapType = (MapType<?, ?>) fieldType; |
| 83 | + SeaTunnelDataType<?> keyType = mapType.getKeyType(); |
| 84 | + SeaTunnelDataType<?> valueType = mapType.getValueType(); |
| 85 | + HashMap<Object, Object> objectMap = new HashMap<>(); |
| 86 | + int mapSize = fakeConfig.getMapSize(); |
| 87 | + for (int i = 0; i < mapSize; i++) { |
| 88 | + Object key = randomColumnValue(keyType); |
| 89 | + Object value = randomColumnValue(valueType); |
| 90 | + objectMap.put(key, value); |
| 91 | + } |
| 92 | + return objectMap; |
| 93 | + case STRING: |
| 94 | + return RandomStringUtils.randomAlphabetic(fakeConfig.getStringLength()); |
| 95 | + case BOOLEAN: |
| 96 | + return RandomUtils.nextInt(0, 2) == 1; |
| 97 | + case TINYINT: |
| 98 | + return (byte) RandomUtils.nextInt(0, 255); |
| 99 | + case SMALLINT: |
| 100 | + return (short) RandomUtils.nextInt(Byte.MAX_VALUE, Short.MAX_VALUE); |
| 101 | + case INT: |
| 102 | + return RandomUtils.nextInt(Short.MAX_VALUE, Integer.MAX_VALUE); |
| 103 | + case BIGINT: |
| 104 | + return RandomUtils.nextLong(Integer.MAX_VALUE, Long.MAX_VALUE); |
| 105 | + case FLOAT: |
| 106 | + return RandomUtils.nextFloat(Float.MIN_VALUE, Float.MAX_VALUE); |
| 107 | + case DOUBLE: |
| 108 | + return RandomUtils.nextDouble(Float.MAX_VALUE, Double.MAX_VALUE); |
| 109 | + case DECIMAL: |
| 110 | + DecimalType decimalType = (DecimalType) fieldType; |
| 111 | + return new BigDecimal(RandomStringUtils.randomNumeric(decimalType.getPrecision() - decimalType.getScale()) + "." + |
| 112 | + RandomStringUtils.randomNumeric(decimalType.getScale())); |
| 113 | + case NULL: |
| 114 | + return null; |
| 115 | + case BYTES: |
| 116 | + return RandomUtils.nextBytes(fakeConfig.getBytesLength()); |
| 117 | + case DATE: |
| 118 | + return randomLocalDateTime().toLocalDate(); |
| 119 | + case TIME: |
| 120 | + return randomLocalDateTime().toLocalTime(); |
| 121 | + case TIMESTAMP: |
| 122 | + return randomLocalDateTime(); |
| 123 | + case ROW: |
| 124 | + SeaTunnelDataType<?>[] fieldTypes = ((SeaTunnelRowType) fieldType).getFieldTypes(); |
| 125 | + Object[] objects = new Object[fieldTypes.length]; |
| 126 | + for (int i = 0; i < fieldTypes.length; i++) { |
| 127 | + Object object = randomColumnValue(fieldTypes[i]); |
| 128 | + objects[i] = object; |
| 129 | + } |
| 130 | + return new SeaTunnelRow(objects); |
| 131 | + default: |
| 132 | + // never got in there |
| 133 | + throw new UnsupportedOperationException("SeaTunnel Fake source connector not support this data type"); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @SuppressWarnings("magicnumber") |
| 138 | + private LocalDateTime randomLocalDateTime() { |
| 139 | + return LocalDateTime.of( |
| 140 | + LocalDateTime.now().getYear(), |
| 141 | + RandomUtils.nextInt(1, 12), |
| 142 | + RandomUtils.nextInt(1, 28), |
| 143 | + RandomUtils.nextInt(0, 24), |
| 144 | + RandomUtils.nextInt(0, 59) |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
0 commit comments