Skip to content

Commit

Permalink
[INLONG-9568][Sort] Support rowdata way of sort InLong message kv format
Browse files Browse the repository at this point in the history
  • Loading branch information
baomingyu committed Feb 18, 2024
1 parent 1b63af3 commit cccd045
Show file tree
Hide file tree
Showing 15 changed files with 1,540 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class TableFormatConstants {
public static final String FORMAT_PROPERTY_VERSION = "format.property-version";
public static final String FORMAT_FIELD_DELIMITER = "format.field-delimiter";
public static final String FORMAT_TIME_FIELD_NAME = "format.time-field-name";
public static final String FORMAT_KV_ENTRY_DELIMITER = "entry-delimiter";
public static final String FORMAT_KV_ENTRY_DELIMITER = "format.entry-delimiter";
public static final String FORMAT_ATTRIBUTE_FIELD_NAME = "format.attribute-field-name";
public static final String FORMAT_IS_MIXED = "format.is-mixed";
public static final String FORMAT_DELETE_HEAD_DELIMITER = "format.delete-head-delimiter";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.inlong</groupId>
<artifactId>format-rowdata</artifactId>
<version>1.12.0-SNAPSHOT</version>
</parent>

<artifactId>sort-format-inlongmsg-rowdata-kv</artifactId>
<packaging>jar</packaging>
<name>Apache InLong - Sort Format-InLongMsg-RowData-KV</name>

<properties>
<inlong.root.dir>${project.parent.parent.parent.parent.basedir}</inlong.root.dir>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.inlong</groupId>
<artifactId>sort-format-inlongmsg-rowdata-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-common</artifactId>
<version>${flink.version}</version>
<scope>provided</scope>
</dependency>

<!-- test dependencies -->
<!-- CSV table descriptor testing -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-common</artifactId>
<version>${flink.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<!-- CSV RowData (de)serialization schema testing -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner_${scala.binary.version}</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
</dependency>

<!-- CSV rowData encoder/intputformat testing -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-test-utils</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner_${scala.binary.version}</artifactId>
<version>${flink.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.inlong.sort.formats.inlongmsgkv;

import org.apache.inlong.sort.formats.inlongmsg.AbstractInLongMsgDecodingFormat;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.flink.api.common.serialization.DeserializationSchema;
import org.apache.flink.configuration.ReadableConfig;
import org.apache.flink.table.connector.ChangelogMode;
import org.apache.flink.table.connector.source.DynamicTableSource;
import org.apache.flink.table.data.RowData;
import org.apache.flink.table.types.DataType;

import static org.apache.inlong.sort.formats.base.TableFormatOptions.ROW_FORMAT_INFO;
import static org.apache.inlong.sort.formats.base.TableFormatUtils.deserializeRowFormatInfo;
import static org.apache.inlong.sort.formats.base.TextFormatOptions.KV_DELIMITER;
import static org.apache.inlong.sort.formats.base.TextFormatOptions.KV_ENTRY_DELIMITER;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.ATTRIBUTE_FIELD_NAME;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.CHARSET;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.ESCAPE_CHARACTER;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.IGNORE_ERRORS;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.LINE_DELIMITER;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.NULL_LITERAL;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.QUOTE_CHARACTER;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.RETAIN_PREDEFINED_FIELD;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.TIME_FIELD_NAME;

/**
* InLongMsgKvDecodingFormat.
*/
public class InLongMsgKvDecodingFormat extends AbstractInLongMsgDecodingFormat {

private final ReadableConfig formatOptions;

public InLongMsgKvDecodingFormat(ReadableConfig formatOptions) {
this.formatOptions = formatOptions;
}

@Override
public DeserializationSchema<RowData> createRuntimeDecoder(
DynamicTableSource.Context context, DataType dataType) {
InLongMsgKvRowDataDeserializationSchema.Builder builder =
new InLongMsgKvRowDataDeserializationSchema.Builder(
deserializeRowFormatInfo(formatOptions.get(ROW_FORMAT_INFO)));
configureDeserializationSchema(formatOptions, builder);
return builder.build();
}

@Override
public ChangelogMode getChangelogMode() {
return ChangelogMode.insertOnly();
}

private void configureDeserializationSchema(
ReadableConfig formatOptions,
InLongMsgKvRowDataDeserializationSchema.Builder schemaBuilder) {
schemaBuilder.setCharset(formatOptions.getOptional(CHARSET).orElse(CHARSET.defaultValue()));

formatOptions
.getOptional(TIME_FIELD_NAME)
.ifPresent(schemaBuilder::setTimeFieldName);

formatOptions
.getOptional(ATTRIBUTE_FIELD_NAME)
.ifPresent(schemaBuilder::setAttributesFieldName);

formatOptions
.getOptional(RETAIN_PREDEFINED_FIELD)
.ifPresent(schemaBuilder::setRetainPredefinedField);

formatOptions
.getOptional(KV_ENTRY_DELIMITER)
.map(delimiter -> StringEscapeUtils.unescapeJava(delimiter).charAt(0))
.ifPresent(schemaBuilder::setEntryDelimiter);

formatOptions
.getOptional(KV_DELIMITER)
.map(delimiter -> StringEscapeUtils.unescapeJava(delimiter).charAt(0))
.ifPresent(schemaBuilder::setKvDelimiter);

formatOptions
.getOptional(LINE_DELIMITER)
.map(delimiter -> StringEscapeUtils.unescapeJava(delimiter).charAt(0))
.ifPresent(schemaBuilder::setLineDelimiter);

formatOptions
.getOptional(QUOTE_CHARACTER)
.map(quote -> quote.charAt(0))
.ifPresent(schemaBuilder::setQuoteCharacter);

formatOptions
.getOptional(ESCAPE_CHARACTER)
.map(escape -> escape.charAt(0))
.ifPresent(schemaBuilder::setEscapeCharacter);

formatOptions
.getOptional(NULL_LITERAL)
.ifPresent(schemaBuilder::setNullLiteral);

formatOptions
.getOptional(IGNORE_ERRORS)
.ifPresent(schemaBuilder::setIgnoreErrors);
}
}
Loading

0 comments on commit cccd045

Please sign in to comment.