Search in sources :

Example 1 with FieldMappingUnit

use of org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit in project incubator-inlong by apache.

the class FieldInfoUtils method setAllMigrationBuiltInField.

/**
 * Get all migration built-in field for binlog source.
 */
public static void setAllMigrationBuiltInField(List<FieldInfo> sourceFields, List<FieldInfo> sinkFields, List<FieldMappingUnit> fieldMappingUnitList) {
    BuiltInFieldInfo dataField = new BuiltInFieldInfo("data", StringFormatInfo.INSTANCE, BuiltInField.MYSQL_METADATA_DATA);
    sourceFields.add(dataField);
    sinkFields.add(dataField);
    fieldMappingUnitList.add(new FieldMappingUnit(dataField, dataField));
    for (Map.Entry<String, BuiltInField> entry : BUILT_IN_FIELD_MAP.entrySet()) {
        if (entry.getKey().equals("data_time")) {
            continue;
        }
        BuiltInFieldInfo fieldInfo = new BuiltInFieldInfo(entry.getKey(), StringFormatInfo.INSTANCE, entry.getValue());
        sourceFields.add(fieldInfo);
        sinkFields.add(fieldInfo);
        fieldMappingUnitList.add(new FieldMappingUnit(fieldInfo, fieldInfo));
    }
}
Also used : BuiltInField(org.apache.inlong.sort.protocol.BuiltInFieldInfo.BuiltInField) BuiltInFieldInfo(org.apache.inlong.sort.protocol.BuiltInFieldInfo) HashMap(java.util.HashMap) Map(java.util.Map) FieldMappingUnit(org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit)

Example 2 with FieldMappingUnit

use of org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit in project incubator-inlong by apache.

the class TransformerTest method testTransformWithNotExistSinkFieldName.

@Test
public void testTransformWithNotExistSinkFieldName() {
    TransformationInfo transformationInfo = new TransformationInfo(new FieldMappingRule(new FieldMappingUnit[] { new FieldMappingUnit(new FieldInfo("age", StringFormatInfo.INSTANCE), new FieldInfo("age_out", StringFormatInfo.INSTANCE)), new FieldMappingUnit(new FieldInfo("name", StringFormatInfo.INSTANCE), // not exist sink field name
    new FieldInfo("name_out_not_exist", StringFormatInfo.INSTANCE)) }));
    Transformer transformer = new Transformer(transformationInfo, sourceFieldInfos, sinkFieldInfos);
    transformer.open(new Configuration());
    Row input = Row.of("name", 29, 179);
    ListCollector<Row> collector = new ListCollector<>();
    transformer.processElement(input, null, collector);
    assertEquals(Row.of(29, null), collector.getInnerList().get(0));
}
Also used : ListCollector(org.apache.inlong.sort.singletenant.flink.deserialization.ListCollector) FieldMappingRule(org.apache.inlong.sort.protocol.transformation.FieldMappingRule) Configuration(org.apache.flink.configuration.Configuration) Row(org.apache.flink.types.Row) FieldMappingUnit(org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit) FieldInfo(org.apache.inlong.sort.protocol.FieldInfo) TransformationInfo(org.apache.inlong.sort.protocol.transformation.TransformationInfo) Test(org.junit.Test)

Example 3 with FieldMappingUnit

use of org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit in project incubator-inlong by apache.

the class Transformer method getRowTransformer.

private Function<Row, Row> getRowTransformer() {
    TransformationRule transformationRule = transformationInfo.getTransformRule();
    if (transformationRule instanceof FieldMappingRule) {
        // Get 'sink field name' => 'source field name' map
        FieldMappingRule fieldMappingRule = (FieldMappingRule) transformationRule;
        final Map<String, String> sinkFieldNameToSourceFieldName = new HashMap<>();
        for (FieldMappingUnit fieldMappingUnit : fieldMappingRule.getFieldMappingUnits()) {
            sinkFieldNameToSourceFieldName.put(fieldMappingUnit.getSinkFieldInfo().getName(), fieldMappingUnit.getSourceFieldInfo().getName());
        }
        // Get 'source field name' => 'index in source row' map
        final Map<String, Integer> sourceFieldNameToIndex = new HashMap<>();
        for (int i = 0; i < sourceFieldInfos.length; i++) {
            sourceFieldNameToIndex.put(sourceFieldInfos[i].getName(), i);
        }
        return row -> {
            final int sinkFieldsLength = sinkFieldInfos.length;
            Row sinkRow = new Row(sinkFieldsLength);
            for (int i = 0; i < sinkFieldsLength; i++) {
                String sinkFieldName = sinkFieldInfos[i].getName();
                String sourceFieldName = sinkFieldNameToSourceFieldName.get(sinkFieldName);
                if (sourceFieldName == null) {
                    LOGGER.warn("Mapping failed! Can't find correspond source field! sink field name `{}`", sinkFieldName);
                    sinkRow.setField(i, null);
                    continue;
                }
                Integer sourceFieldIndex = sourceFieldNameToIndex.get(sourceFieldName);
                if (sourceFieldIndex == null) {
                    LOGGER.warn("Mapping failed! Can't find correspond source field!" + " source field name `{}`, sink field name `{}`", sourceFieldName, sinkFieldName);
                    sinkRow.setField(i, null);
                    continue;
                }
                sinkRow.setField(i, row.getField(sourceFieldIndex));
            }
            return sinkRow;
        };
    } else {
        throw new IllegalArgumentException("Unsupported transformation rule " + transformationRule.getClass());
    }
}
Also used : FieldMappingUnit(org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit) TransformationRule(org.apache.inlong.sort.protocol.transformation.TransformationRule) Logger(org.slf4j.Logger) Configuration(org.apache.flink.configuration.Configuration) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Function(java.util.function.Function) FieldMappingRule(org.apache.inlong.sort.protocol.transformation.FieldMappingRule) FieldInfo(org.apache.inlong.sort.protocol.FieldInfo) Collector(org.apache.flink.util.Collector) TransformationInfo(org.apache.inlong.sort.protocol.transformation.TransformationInfo) Map(java.util.Map) Preconditions(com.google.common.base.Preconditions) ProcessFunction(org.apache.flink.streaming.api.functions.ProcessFunction) Row(org.apache.flink.types.Row) FieldMappingRule(org.apache.inlong.sort.protocol.transformation.FieldMappingRule) HashMap(java.util.HashMap) TransformationRule(org.apache.inlong.sort.protocol.transformation.TransformationRule) Row(org.apache.flink.types.Row) FieldMappingUnit(org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit)

Example 4 with FieldMappingUnit

use of org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit in project incubator-inlong by apache.

the class TransformerTest method testTransform.

@Test
public void testTransform() {
    TransformationInfo transformationInfo = new TransformationInfo(new FieldMappingRule(new FieldMappingUnit[] { new FieldMappingUnit(new FieldInfo("age", StringFormatInfo.INSTANCE), new FieldInfo("age_out", StringFormatInfo.INSTANCE)), new FieldMappingUnit(new FieldInfo("name", StringFormatInfo.INSTANCE), new FieldInfo("name_out", StringFormatInfo.INSTANCE)) }));
    Transformer transformer = new Transformer(transformationInfo, sourceFieldInfos, sinkFieldInfos);
    transformer.open(new Configuration());
    Row input = Row.of("name", 29, 179);
    ListCollector<Row> collector = new ListCollector<>();
    transformer.processElement(input, null, collector);
    assertEquals(Row.of(29, "name"), collector.getInnerList().get(0));
}
Also used : ListCollector(org.apache.inlong.sort.singletenant.flink.deserialization.ListCollector) FieldMappingRule(org.apache.inlong.sort.protocol.transformation.FieldMappingRule) Configuration(org.apache.flink.configuration.Configuration) Row(org.apache.flink.types.Row) FieldMappingUnit(org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit) FieldInfo(org.apache.inlong.sort.protocol.FieldInfo) TransformationInfo(org.apache.inlong.sort.protocol.transformation.TransformationInfo) Test(org.junit.Test)

Example 5 with FieldMappingUnit

use of org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit in project incubator-inlong by apache.

the class TransformerTest method testTransformWithNotExistSourceFieldName.

@Test
public void testTransformWithNotExistSourceFieldName() {
    TransformationInfo transformationInfo = new TransformationInfo(new FieldMappingRule(new FieldMappingUnit[] { new FieldMappingUnit(new FieldInfo("age", StringFormatInfo.INSTANCE), new FieldInfo("age_out", StringFormatInfo.INSTANCE)), new FieldMappingUnit(// not exist source field name
    new FieldInfo("name_not_exist", StringFormatInfo.INSTANCE), new FieldInfo("name_out", StringFormatInfo.INSTANCE)) }));
    Transformer transformer = new Transformer(transformationInfo, sourceFieldInfos, sinkFieldInfos);
    transformer.open(new Configuration());
    Row input = Row.of("name", 29, 179);
    ListCollector<Row> collector = new ListCollector<>();
    transformer.processElement(input, null, collector);
    assertEquals(Row.of(29, null), collector.getInnerList().get(0));
}
Also used : ListCollector(org.apache.inlong.sort.singletenant.flink.deserialization.ListCollector) FieldMappingRule(org.apache.inlong.sort.protocol.transformation.FieldMappingRule) Configuration(org.apache.flink.configuration.Configuration) Row(org.apache.flink.types.Row) FieldMappingUnit(org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit) FieldInfo(org.apache.inlong.sort.protocol.FieldInfo) TransformationInfo(org.apache.inlong.sort.protocol.transformation.TransformationInfo) Test(org.junit.Test)

Aggregations

FieldMappingUnit (org.apache.inlong.sort.protocol.transformation.FieldMappingRule.FieldMappingUnit)6 FieldInfo (org.apache.inlong.sort.protocol.FieldInfo)5 FieldMappingRule (org.apache.inlong.sort.protocol.transformation.FieldMappingRule)5 Configuration (org.apache.flink.configuration.Configuration)4 Row (org.apache.flink.types.Row)4 TransformationInfo (org.apache.inlong.sort.protocol.transformation.TransformationInfo)4 ListCollector (org.apache.inlong.sort.singletenant.flink.deserialization.ListCollector)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BuiltInFieldInfo (org.apache.inlong.sort.protocol.BuiltInFieldInfo)2 Preconditions (com.google.common.base.Preconditions)1 ArrayList (java.util.ArrayList)1 Function (java.util.function.Function)1 ProcessFunction (org.apache.flink.streaming.api.functions.ProcessFunction)1 Collector (org.apache.flink.util.Collector)1 SinkFieldResponse (org.apache.inlong.manager.common.pojo.sink.SinkFieldResponse)1 TimestampFormatInfo (org.apache.inlong.sort.formats.common.TimestampFormatInfo)1 BuiltInField (org.apache.inlong.sort.protocol.BuiltInFieldInfo.BuiltInField)1 TransformationRule (org.apache.inlong.sort.protocol.transformation.TransformationRule)1