forked from apache/inlong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-8643][Sort] Support Iceberg source (apache#8818)
- Loading branch information
1 parent
4cd37a7
commit e775827
Showing
19 changed files
with
1,746 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...-common/src/main/java/org/apache/inlong/sort/protocol/node/extract/IcebergExtracNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...sort-core/src/test/java/org/apache/inlong/sort/parser/Iceberg2StarRocksSqlParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.