Skip to content

Commit

Permalink
[INLONG-9569][Sort] Support rowdata way of sort InLong message csv fo…
Browse files Browse the repository at this point in the history
…rmat (#9646)
  • Loading branch information
baomingyu authored Feb 18, 2024
1 parent 9fdcd47 commit 09f9429
Show file tree
Hide file tree
Showing 14 changed files with 2,109 additions and 144 deletions.
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-csv</artifactId>
<packaging>jar</packaging>
<name>Apache InLong - Sort Format-InLongMsg-RowData-CSV</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,119 @@
/*
* 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.inlongmsgcsv;

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.inlongmsg.InLongMsgOptions.ATTRIBUTE_FIELD_NAME;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.CHARSET;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.DELETE_HEAD_DELIMITER;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.ESCAPE_CHARACTER;
import static org.apache.inlong.sort.formats.inlongmsg.InLongMsgOptions.FIELD_DELIMITER;
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.TIME_FIELD_NAME;

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

private final ReadableConfig formatOptions;

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

@Override
public DeserializationSchema<RowData> createRuntimeDecoder(
DynamicTableSource.Context context, DataType dataType) {
InLongMsgCsvRowDataDeserializationSchema.Builder builder =
new InLongMsgCsvRowDataDeserializationSchema.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,
InLongMsgCsvRowDataDeserializationSchema.Builder schemaBuilder) {
schemaBuilder.setCharset(formatOptions.getOptional(CHARSET).orElse(CHARSET.defaultValue()))
.setMetadataKeys(metadataKeys);

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

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

formatOptions
.getOptional(CHARSET)
.ifPresent(schemaBuilder::setCharset);

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

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

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);

formatOptions
.getOptional(DELETE_HEAD_DELIMITER)
.ifPresent(schemaBuilder::setDeleteHeadDelimiter);
}
}
Loading

0 comments on commit 09f9429

Please sign in to comment.