Skip to content

Commit

Permalink
[INLONG-8643][Sort] Support Iceberg source (apache#8818)
Browse files Browse the repository at this point in the history
  • Loading branch information
vernedeng authored and liaosunny123 committed Oct 11, 2023
1 parent 4cd37a7 commit e775827
Show file tree
Hide file tree
Showing 19 changed files with 1,746 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,4 @@ private void addToGroupExt(InlongGroupInfo groupInfo, String value) {
groupInfo.getExtList().removeIf(ext -> extInfo.getKeyName().equals(ext.getKeyName()));
groupInfo.getExtList().add(extInfo);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
*/
public class IcebergConstant {

public static final String DEFAULT_CATALOG_NAME = "ICEBERG_HIVE";
public static final String CONNECTOR_KEY = "connector";
public static final String CONNECTOR = "iceberg-inlong";
public static final String DATABASE_KEY = "catalog-database";
public static final String DEFAULT_DATABASE_KEY = "default-database";
public static final String TABLE_KEY = "catalog-table";
public static final String CATALOG_TYPE_KEY = "catalog-type";
public static final String CATALOG_NAME_KEY = "catalog-name";
public static final String URI_KEY = "uri";
public static final String WAREHOUSE_KEY = "warehouse";
public static final String START_SNAPSHOT_ID = "start-snapshot-id";
public static final String STREAMING = "streaming";

/**
* Iceberg supported catalog type
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.inlong.sort.protocol.node.extract.DorisExtractNode;
import org.apache.inlong.sort.protocol.node.extract.FileSystemExtractNode;
import org.apache.inlong.sort.protocol.node.extract.HudiExtractNode;
import org.apache.inlong.sort.protocol.node.extract.IcebergExtracNode;
import org.apache.inlong.sort.protocol.node.extract.KafkaExtractNode;
import org.apache.inlong.sort.protocol.node.extract.MongoExtractNode;
import org.apache.inlong.sort.protocol.node.extract.MySqlExtractNode;
Expand Down Expand Up @@ -64,6 +65,7 @@
@JsonSubTypes.Type(value = RedisExtractNode.class, name = "redisExtract"),
@JsonSubTypes.Type(value = DorisExtractNode.class, name = "dorisExtract"),
@JsonSubTypes.Type(value = HudiExtractNode.class, name = "hudiExtract"),
@JsonSubTypes.Type(value = IcebergExtracNode.class, name = "icebergExtract"),
})
@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.inlong.sort.protocol.node.extract.DorisExtractNode;
import org.apache.inlong.sort.protocol.node.extract.FileSystemExtractNode;
import org.apache.inlong.sort.protocol.node.extract.HudiExtractNode;
import org.apache.inlong.sort.protocol.node.extract.IcebergExtracNode;
import org.apache.inlong.sort.protocol.node.extract.KafkaExtractNode;
import org.apache.inlong.sort.protocol.node.extract.MongoExtractNode;
import org.apache.inlong.sort.protocol.node.extract.MySqlExtractNode;
Expand Down Expand Up @@ -78,6 +79,7 @@
@JsonSubTypes.Type(value = RedisExtractNode.class, name = "redisExtract"),
@JsonSubTypes.Type(value = DorisExtractNode.class, name = "dorisExtract"),
@JsonSubTypes.Type(value = HudiExtractNode.class, name = "hudiExtract"),
@JsonSubTypes.Type(value = IcebergExtracNode.class, name = "icebergExtract"),
@JsonSubTypes.Type(value = TransformNode.class, name = "baseTransform"),
@JsonSubTypes.Type(value = DistinctNode.class, name = "distinct"),
@JsonSubTypes.Type(value = KafkaLoadNode.class, name = "kafkaLoad"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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.protocol.node.extract;

import org.apache.inlong.sort.protocol.FieldInfo;
import org.apache.inlong.sort.protocol.constant.IcebergConstant;
import org.apache.inlong.sort.protocol.node.ExtractNode;
import org.apache.inlong.sort.protocol.transformation.WatermarkField;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonInclude;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
* Iceberg extract node for extract data from iceberg
*/
@EqualsAndHashCode(callSuper = true)
@JsonTypeName("icebergExtract")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class IcebergExtracNode extends ExtractNode implements Serializable {

@JsonProperty("tableName")
@Nonnull
private String tableName;

@JsonProperty("dbName")
@Nonnull
private String dbName;

@JsonProperty("catalogType")
private IcebergConstant.CatalogType catalogType;

@Nullable
@JsonProperty("uri")
private String uri;

@JsonProperty("warehouse")
private String warehouse;

@JsonProperty("catalogName")
private String catalogName;

@JsonProperty("primaryKey")
private String primaryKey;

@JsonProperty("startSnapShotId")
@Nullable
private Long startSnapShotId;

public IcebergExtracNode(
@Nonnull @JsonProperty("id") String id,
@Nonnull @JsonProperty("name") String name,
@Nonnull @JsonProperty("fields") List<FieldInfo> fields,
@Nullable @JsonProperty("watermarkField") WatermarkField watermarkField,
@Nullable @JsonProperty("uri") String uri,
@Nullable @JsonProperty("warehouse") String warehouse,
@Nonnull @JsonProperty("dbName") String dbName,
@Nonnull @JsonProperty("tableName") String tableName,
@JsonProperty("catalogType") IcebergConstant.CatalogType catalogType,
@Nullable @JsonProperty("catalogName") String catalogName,
@JsonProperty("primaryKey") String primaryKey,
@Nullable @JsonProperty("startSnapShotId") Long startSnapShotId,
@Nullable @JsonProperty("properties") Map<String, String> properties) {
super(id, name, fields, watermarkField, properties);
this.uri = uri;
this.warehouse = warehouse;
this.dbName = dbName;
this.tableName = tableName;
this.catalogName = catalogName == null ? IcebergConstant.DEFAULT_CATALOG_NAME : catalogName;
this.primaryKey = primaryKey;
this.startSnapShotId = startSnapShotId;
this.catalogType = catalogType == null ? IcebergConstant.CatalogType.HIVE : catalogType;
}

@Override
public String genTableName() {
return String.format("iceberg_table_%s", getId());
}

@Override
public Map<String, String> tableOptions() {
Map<String, String> options = super.tableOptions();
options.put(IcebergConstant.CONNECTOR_KEY, IcebergConstant.CONNECTOR);
options.put(IcebergConstant.DATABASE_KEY, dbName);
options.put(IcebergConstant.TABLE_KEY, tableName);
options.put(IcebergConstant.CATALOG_TYPE_KEY, catalogType.name());
options.put(IcebergConstant.CATALOG_NAME_KEY, catalogName);
options.put(IcebergConstant.STREAMING, "true");
if (null != uri) {
options.put(IcebergConstant.URI_KEY, uri);
}
if (null != warehouse) {
options.put(IcebergConstant.WAREHOUSE_KEY, warehouse);
}
if (null != startSnapShotId) {
options.put(IcebergConstant.START_SNAPSHOT_ID, startSnapShotId.toString());
}
return options;
}

@Override
public String getPrimaryKey() {
return primaryKey;
}

@Override
public List<FieldInfo> getPartitionFields() {
return super.getPartitionFields();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ public IcebergLoadNode(@JsonProperty("id") String id,
@Override
public Map<String, String> tableOptions() {
Map<String, String> options = super.tableOptions();
options.put("connector", "iceberg-inlong");
options.put(IcebergConstant.CONNECTOR_KEY, IcebergConstant.CONNECTOR);
// for test sink.ignore.changelog
// options.put("sink.ignore.changelog", "true");
options.put("catalog-database", dbName);
options.put("catalog-table", tableName);
options.put("default-database", dbName);
options.put("catalog-type", catalogType.name());
options.put("catalog-name", catalogType.name());
options.put(IcebergConstant.DATABASE_KEY, dbName);
options.put(IcebergConstant.TABLE_KEY, tableName);
options.put(IcebergConstant.DEFAULT_DATABASE_KEY, dbName);
options.put(IcebergConstant.CATALOG_TYPE_KEY, catalogType.name());
options.put(IcebergConstant.CATALOG_NAME_KEY, catalogType.name());
if (null != uri) {
options.put("uri", uri);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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.parser;

import org.apache.inlong.sort.formats.common.LongFormatInfo;
import org.apache.inlong.sort.formats.common.StringFormatInfo;
import org.apache.inlong.sort.parser.impl.FlinkSqlParser;
import org.apache.inlong.sort.parser.result.FlinkSqlParseResult;
import org.apache.inlong.sort.protocol.FieldInfo;
import org.apache.inlong.sort.protocol.GroupInfo;
import org.apache.inlong.sort.protocol.StreamInfo;
import org.apache.inlong.sort.protocol.constant.IcebergConstant;
import org.apache.inlong.sort.protocol.node.Node;
import org.apache.inlong.sort.protocol.node.extract.IcebergExtracNode;
import org.apache.inlong.sort.protocol.node.load.StarRocksLoadNode;
import org.apache.inlong.sort.protocol.transformation.FieldRelation;
import org.apache.inlong.sort.protocol.transformation.relation.NodeRelation;

import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.test.util.AbstractTestBase;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Iceberg2StarRocksSqlParserTest extends AbstractTestBase {

private String groupId = "b_test_wk_0801";
private String streamId = "b_test_wkstream_0801";

// iceberg
private String uri = "";
private String icDatabase = "";
private String icTable = "";
private String catalogName = "HIVE";
private String warehouse = "";

// starrocks
private String user = "";
private String password = "";
private String jdbc = "";
private String srDatabase = "";
private String srTable = "";
private String primaryKey = "id";
private String loadUrl = "";

private List<FieldInfo> fields() {
return Arrays.asList(
new FieldInfo("id", new LongFormatInfo()),
new FieldInfo("name", new StringFormatInfo()),
new FieldInfo("source", new StringFormatInfo()),
new FieldInfo("count", new LongFormatInfo()),
new FieldInfo("remark", new StringFormatInfo()),
new FieldInfo("send_time", new StringFormatInfo()));
}

private List<FieldRelation> relations() {
return fields().stream()
.map(info -> new FieldRelation(info, info))
.collect(Collectors.toList());
}

private IcebergExtracNode buildIcebergExtracNode(String id) {

return new IcebergExtracNode(id, "iceberg-source", fields(), null, uri,
warehouse, icDatabase, icTable, IcebergConstant.CatalogType.HIVE, catalogName,
null, null, null);

}

private StarRocksLoadNode buildStarRocksLoadNode(String id) {

Map<String, String> properties = new HashMap<>();
properties.put("sink.properties.format", "json");
properties.put("sink.properties.strip_outer_array", "true");
return new StarRocksLoadNode(id, "sink", fields(), relations(), null, null,
1, properties, jdbc, loadUrl, user, password, srDatabase,
srTable, primaryKey, null, null, null, null);
}

private NodeRelation buildNodeRelation(List<Node> inputs, List<Node> outputs) {
List<String> inputIds = inputs.stream().map(Node::getId).collect(Collectors.toList());
List<String> outputIds = outputs.stream().map(Node::getId).collect(Collectors.toList());
return new NodeRelation(inputIds, outputIds);
}

@Test
public void testIceberg() throws Exception {
EnvironmentSettings settings = EnvironmentSettings
.newInstance()
.inStreamingMode()
.build();
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
env.enableCheckpointing(10000);
StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env, settings);
Node inputNode = buildIcebergExtracNode("1");
Node outputNode = buildStarRocksLoadNode("2");
StreamInfo streamInfo = new StreamInfo(streamId, Arrays.asList(inputNode, outputNode),
Arrays.asList(buildNodeRelation(Collections.singletonList(inputNode),
Collections.singletonList(outputNode))));
GroupInfo groupInfo = new GroupInfo(groupId, Collections.singletonList(streamInfo));

ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writeValueAsString(groupInfo));
FlinkSqlParser parser = FlinkSqlParser.getInstance(tableEnv, groupInfo);
FlinkSqlParseResult result = (FlinkSqlParseResult) parser.parse();
Assert.assertTrue(!result.getLoadSqls().isEmpty() && !result.getCreateTableSqls().isEmpty());
}
}
3 changes: 2 additions & 1 deletion inlong-sort/sort-flink/sort-flink-v1.13/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<hudi.version>0.12.3</hudi.version>
<sqlserver.jdbc.version>7.2.2.jre8</sqlserver.jdbc.version>
<thrift.version>0.9.3</thrift.version>
<flink.iceberg.version>1.1.0</flink.iceberg.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -148,7 +149,7 @@
<dependency>
<groupId>org.apache.iceberg</groupId>
<artifactId>iceberg-flink-runtime-1.14</artifactId>
<version>${iceberg.version}</version>
<version>${flink.iceberg.version}</version>
</dependency>

<!-- flink -->
Expand Down
5 changes: 0 additions & 5 deletions inlong-sort/sort-flink/sort-flink-v1.15/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@
<artifactId>mssql-jdbc</artifactId>
<version>${sqlserver.jdbc.version}</version>
</dependency>
<dependency>
<groupId>org.apache.iceberg</groupId>
<artifactId>iceberg-flink-runtime-1.14</artifactId>
<version>${iceberg.version}</version>
</dependency>

<!-- flink -->
<dependency>
Expand Down
Loading

0 comments on commit e775827

Please sign in to comment.