Skip to content

Commit d5c639e

Browse files
committed
Merge branch 'feat_1.10_kuduKerberos' into '1.10_test_4.1.x'
Feat 1.10 kudu kerberos See merge request dt-insight-engine/flinkStreamSQL!98
2 parents ff81914 + 2080cd0 commit d5c639e

21 files changed

Lines changed: 482 additions & 51 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.dtstack.flink.sql.constant;
20+
21+
/**
22+
* @program: flinkStreamSQL
23+
* @author: wuren
24+
* @create: 2020/09/15
25+
**/
26+
public class PluginParamConsts {
27+
public static final String PRINCIPAL = "principal";
28+
public static final String KEYTAB = "keytab";
29+
public static final String KRB5_CONF = "krb5conf";
30+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.dtstack.flink.sql.krb;
20+
21+
import com.google.common.base.Strings;
22+
23+
/**
24+
* @program: flinkStreamSQL
25+
* @author: wuren
26+
* @create: 2020/09/15
27+
**/
28+
public interface KerberosTable {
29+
30+
String getPrincipal();
31+
32+
void setPrincipal(String principal);
33+
34+
String getKeytab();
35+
36+
void setKeytab(String keytab);
37+
38+
String getKrb5conf();
39+
40+
void setKrb5conf(String krb5conf);
41+
42+
boolean isEnableKrb();
43+
44+
void setEnableKrb(boolean enableKrb);
45+
46+
default void judgeKrbEnable() {
47+
boolean allSet =
48+
!Strings.isNullOrEmpty(getPrincipal()) &&
49+
!Strings.isNullOrEmpty(getKeytab()) &&
50+
!Strings.isNullOrEmpty(getKrb5conf());
51+
52+
boolean allNotSet =
53+
Strings.isNullOrEmpty(getPrincipal()) &&
54+
Strings.isNullOrEmpty(getKeytab()) &&
55+
Strings.isNullOrEmpty(getKrb5conf());
56+
57+
if (allSet) {
58+
setEnableKrb(true);
59+
} else if (allNotSet) {
60+
setEnableKrb(false);
61+
} else {
62+
throw new RuntimeException("Missing kerberos parameter! all kerberos params must be set, or all kerberos params are not set");
63+
}
64+
}
65+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.dtstack.flink.sql.util;
20+
21+
import org.apache.hadoop.conf.Configuration;
22+
import org.apache.hadoop.security.UserGroupInformation;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.io.IOException;
27+
28+
/**
29+
* @program: flinkStreamSQL
30+
* @author: wuren
31+
* @create: 2020/09/14
32+
**/
33+
public class KrbUtils {
34+
35+
private static final Logger LOG = LoggerFactory.getLogger(KrbUtils.class);
36+
37+
public static final String KRB5_CONF_KEY = "java.security.krb5.conf";
38+
public static final String HADOOP_AUTH_KEY = "hadoop.security.authentication";
39+
public static final String KRB_STR = "Kerberos";
40+
// public static final String FALSE_STR = "false";
41+
// public static final String SUBJECT_ONLY_KEY = "javax.security.auth.useSubjectCredsOnly";
42+
43+
public static UserGroupInformation getUgi(String principal, String keytabPath, String krb5confPath) throws IOException {
44+
LOG.info("Kerberos login with principal: {} and keytab: {}", principal, keytabPath);
45+
System.setProperty(KRB5_CONF_KEY, krb5confPath);
46+
// TODO 尚未探索出此选项的意义,以后研究明白方可打开
47+
// System.setProperty(SUBJECT_ONLY_KEY, FALSE_STR);
48+
Configuration configuration = new Configuration();
49+
configuration.set(HADOOP_AUTH_KEY , KRB_STR);
50+
UserGroupInformation.setConfiguration(configuration);
51+
return UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal, keytabPath);
52+
}
53+
54+
}

docs/plugin/kuduSide.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
| isFaultTolerant |查询是否容错 查询失败是否扫描第二个副本 默认false 容错 |||
6161
| cache | 维表缓存策略(NONE/LRU/ALL)||NONE|
6262
| partitionedJoin | 是否在維表join之前先根据 設定的key 做一次keyby操作(可以減少维表的数据缓存量)||false|
63+
| principal |kerberos用于登录的principal |||
64+
| keytab |keytab文件的路径 |||
65+
| krb5conf |conf文件路径 |||
66+
Kerberos三个参数全部设置则开启Kerberos认证,如果缺少任何一个则会提示缺少参数错误。
67+
如果全部未设置则不开启Kerberos连接Kudu集群。
6368
--------------
6469

6570
## 5.样例
@@ -163,3 +168,20 @@ into
163168
on t1.id = t2.id;
164169
```
165170

171+
## 7.kerberos示例
172+
```
173+
create table dim (
174+
name varchar,
175+
id int,
176+
PERIOD FOR SYSTEM_TIME
177+
) WITH (
178+
type='kudu',
179+
kuduMasters='host1',
180+
tableName='foo',
181+
parallelism ='1',
182+
cache ='ALL',
183+
keytab='foo/foobar.keytab',
184+
krb5conf='bar/krb5.conf',
185+
principal='kudu/host1@DTSTACK.COM'
186+
);
187+
```

docs/plugin/kuduSink.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ kudu 1.9.0+cdh6.2.0
4242
| defaultOperationTimeoutMs | 操作超时时间 ||
4343
| defaultSocketReadTimeoutMs | socket读取超时时间 ||
4444
| parallelism | 并行度设置||1|
45-
45+
| principal |kerberos用于登录的principal |||
46+
| keytab |keytab文件的路径 |||
47+
| krb5conf |conf文件路径 |||
48+
Kerberos三个参数全部设置则开启Kerberos认证,如果缺少任何一个则会提示缺少参数错误。
49+
如果全部未设置则不开启Kerberos连接Kudu集群。
4650

4751
## 5.样例:
4852
```
@@ -123,4 +127,21 @@ into
123127
### 结果数据
124128
```
125129
{"a":"2","b":"2","c":"3","d":"4"}
126-
```
130+
```
131+
132+
## 7.kerberos示例
133+
```
134+
create table dwd (
135+
name varchar,
136+
id int
137+
) WITH (
138+
type='kudu',
139+
kuduMasters='host1',
140+
tableName='foo',
141+
writeMode='insert',
142+
parallelism ='1',
143+
keytab='foo/foobar.keytab',
144+
krb5conf='bar/krb5.conf',
145+
principal='kudu/host1@DTSTACK.COM'
146+
);
147+
```

kafka-base/kafka-base-source/src/main/java/com/dtstack/flink/sql/source/kafka/AbstractKafkaSource.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ protected Properties getKafkaProperties(KafkaSourceTableInfo kafkaSourceTableInf
5454

5555
if (DtStringUtil.isJson(kafkaSourceTableInfo.getOffsetReset())) {
5656
props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, EKafkaOffset.NONE.name().toLowerCase());
57+
} else if(StringUtils.equalsIgnoreCase(EKafkaOffset.TIMESTAMP.name().toLowerCase(), kafkaSourceTableInfo.getOffsetReset())){
58+
props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, EKafkaOffset.EARLIEST.name().toLowerCase());
5759
} else {
5860
props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, kafkaSourceTableInfo.getOffsetReset());
5961
}
@@ -95,10 +97,12 @@ protected void setParallelism(Integer parallelism, DataStreamSource kafkaSource)
9597
}
9698
}
9799

98-
protected void setStartPosition(String offset, String topicName, FlinkKafkaConsumerBase<Row> kafkaSrc) {
100+
protected void setStartPosition(String offset, String topicName, FlinkKafkaConsumerBase<Row> kafkaSrc, Runnable runnable) {
99101
if (StringUtils.equalsIgnoreCase(offset, EKafkaOffset.EARLIEST.name())) {
100102
kafkaSrc.setStartFromEarliest();
101-
} else if (DtStringUtil.isJson(offset)) {
103+
} else if(StringUtils.equalsIgnoreCase(offset, EKafkaOffset.TIMESTAMP.name())) {
104+
runnable.run();
105+
}else if (DtStringUtil.isJson(offset)) {
102106
Map<KafkaTopicPartition, Long> specificStartupOffsets = buildOffsetMap(offset, topicName);
103107
kafkaSrc.setStartFromSpecificOffsets(specificStartupOffsets);
104108
} else {

kafka-base/kafka-base-source/src/main/java/com/dtstack/flink/sql/source/kafka/enums/EKafkaOffset.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ public enum EKafkaOffset {
2727

2828
LATEST,
2929
EARLIEST,
30+
TIMESTAMP,
3031
NONE
3132
}

kafka-base/kafka-base-source/src/main/java/com/dtstack/flink/sql/source/kafka/table/KafkaSourceParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public AbstractTableInfo getTableInfo(String tableName, String fieldsInfo, Map<S
5656
kafkaSourceTableInfo.setFieldDelimiter(MathUtil.getString(props.getOrDefault(KafkaSourceTableInfo.CSV_FIELD_DELIMITER_KEY.toLowerCase(), "|")));
5757
kafkaSourceTableInfo.setSourceDataType(MathUtil.getString(props.getOrDefault(KafkaSourceTableInfo.SOURCE_DATA_TYPE_KEY.toLowerCase(), FormatType.DT_NEST.name())));
5858

59+
if(props.containsKey(KafkaSourceTableInfo.TIMESTAMP_OFFSET.toLowerCase())){
60+
kafkaSourceTableInfo.setTimestampOffset(MathUtil.getLongVal(props.getOrDefault(KafkaSourceTableInfo.TIMESTAMP_OFFSET.toLowerCase(), System.currentTimeMillis())));
61+
}
5962
Map<String, String> kafkaParams = props.keySet().stream()
6063
.filter(key -> !key.isEmpty() && key.startsWith("kafka."))
6164
.collect(Collectors.toMap(

kafka-base/kafka-base-source/src/main/java/com/dtstack/flink/sql/source/kafka/table/KafkaSourceTableInfo.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class KafkaSourceTableInfo extends AbstractSourceTableInfo {
5555

5656
public static final String CHARSET_NAME_KEY = "charsetName";
5757

58+
public static final String TIMESTAMP_OFFSET = "timestampOffset";
59+
5860
private String bootstrapServers;
5961

6062
private String topic;
@@ -75,6 +77,8 @@ public class KafkaSourceTableInfo extends AbstractSourceTableInfo {
7577

7678
public String charsetName;
7779

80+
private Long timestampOffset;
81+
7882
public String getBootstrapServers() {
7983
return bootstrapServers;
8084
}
@@ -159,6 +163,14 @@ public void setCharsetName(String charsetName) {
159163
this.charsetName = charsetName;
160164
}
161165

166+
public Long getTimestampOffset() {
167+
return timestampOffset;
168+
}
169+
170+
public void setTimestampOffset(Long timestampOffset) {
171+
this.timestampOffset = timestampOffset;
172+
}
173+
162174
@Override
163175
public boolean check() {
164176
Preconditions.checkNotNull(getType(), "kafka of type is required");

kafka/kafka-source/src/main/java/com/dtstack/flink/sql/source/kafka/KafkaSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Table genStreamSource(AbstractSourceTableInfo sourceTableInfo, StreamExec
5151
DataStreamSource kafkaSource = env.addSource(kafkaSrc, sourceOperatorName, typeInformation);
5252

5353
setParallelism(kafkaSourceTableInfo.getParallelism(), kafkaSource);
54-
setStartPosition(kafkaSourceTableInfo.getOffsetReset(), topicName, kafkaSrc);
54+
setStartPosition(kafkaSourceTableInfo.getOffsetReset(), topicName, kafkaSrc, () -> kafkaSrc.setStartFromTimestamp(kafkaSourceTableInfo.getTimestampOffset()));
5555
String fields = StringUtils.join(kafkaSourceTableInfo.getFields(), ",");
5656

5757
return tableEnv.fromDataStream(kafkaSource, fields);

0 commit comments

Comments
 (0)