Skip to content

Commit 7575a12

Browse files
committed
[feat-35399]1.[http] 使用PoolingHttpClientConnectionManager来创建连接线程池; 2.[file] 优化File source 代码,移除location, hdfsSite, coreSite等不必要参数,调整FileSourceTableInfo的结构,将Csv、Json、Arvo等拆分为独立类
1 parent 27a6dd4 commit 7575a12

File tree

18 files changed

+843
-550
lines changed

18 files changed

+843
-550
lines changed

core/src/main/java/com/dtstack/flink/sql/util/DtStringUtil.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
package com.dtstack.flink.sql.util;
2222

2323
import com.dtstack.flink.sql.enums.ColumnType;
24-
import com.fasterxml.jackson.databind.ObjectMapper;
2524
import com.google.common.base.Strings;
2625
import com.google.common.collect.Maps;
2726
import org.apache.commons.lang3.StringUtils;
27+
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
28+
import org.apache.flink.util.Preconditions;
2829

2930
import java.math.BigDecimal;
3031
import java.sql.Timestamp;
@@ -46,7 +47,7 @@ public class DtStringUtil {
4647

4748
private static final Pattern NO_VERSION_PATTERN = Pattern.compile("([a-zA-Z]+).*");
4849

49-
private static ObjectMapper objectMapper = new ObjectMapper();
50+
private static final ObjectMapper objectMapper = new ObjectMapper();
5051

5152
/**
5253
* Split the specified string delimiter --- ignored quotes delimiter
@@ -253,11 +254,11 @@ public static String col2string(Object column, String type) {
253254
}
254255

255256
public static String getPluginTypeWithoutVersion(String engineType) {
257+
Preconditions.checkNotNull(engineType, "type can't be null!");
258+
256259
Matcher matcher = NO_VERSION_PATTERN.matcher(engineType);
257260

258-
if (!engineType.equals("kafka")) {
259-
return engineType;
260-
} else if (!matcher.find()) {
261+
if (!matcher.find()) {
261262
return engineType;
262263
}
263264

core/src/main/java/com/dtstack/flink/sql/util/PluginUtil.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222

2323
import com.dtstack.flink.sql.dirtyManager.consumer.DirtyConsumerFactory;
2424
import com.dtstack.flink.sql.enums.EPluginLoadMode;
25-
import com.fasterxml.jackson.core.JsonGenerationException;
26-
import com.fasterxml.jackson.core.JsonParseException;
27-
import com.fasterxml.jackson.databind.JsonMappingException;
28-
import com.fasterxml.jackson.databind.ObjectMapper;
2925
import org.apache.commons.lang3.StringUtils;
26+
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException;
27+
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
3028
import org.slf4j.Logger;
3129
import org.slf4j.LoggerFactory;
3230

@@ -128,7 +126,11 @@ public static Map<String,Object> objectToMap(Object obj) throws Exception{
128126
return objectMapper.readValue(objectMapper.writeValueAsBytes(obj), Map.class);
129127
}
130128

131-
public static <T> T jsonStrToObject(String jsonStr, Class<T> clazz) throws JsonParseException, JsonMappingException, JsonGenerationException, IOException{
129+
public static String objectToString(Object obj) throws JsonProcessingException {
130+
return objectMapper.writeValueAsString(obj);
131+
}
132+
133+
public static <T> T jsonStrToObject(String jsonStr, Class<T> clazz) throws IOException{
132134
return objectMapper.readValue(jsonStr, clazz);
133135
}
134136

core/src/main/java/com/dtstack/flink/sql/util/ThreadUtil.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
public class ThreadUtil {
2929
public static final Long DEFAULT_SLEEP_TIME = 10L;
30+
3031
public static void sleepSeconds(long timeout) {
3132
try {
3233
TimeUnit.SECONDS.sleep(timeout);
@@ -50,4 +51,12 @@ public static void sleepMicroseconds(long timeout) {
5051
throw new RuntimeException(ie);
5152
}
5253
}
54+
55+
public static void sleepMilliseconds(long timeout) {
56+
try {
57+
TimeUnit.MILLISECONDS.sleep(timeout);
58+
} catch (InterruptedException e) {
59+
throw new RuntimeException(e);
60+
}
61+
}
5362
}

file/file-source/src/main/java/com/dtstack/flink/sql/source/file/FileSource.java

Lines changed: 57 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.dtstack.flink.sql.table.AbstractSourceTableInfo;
2424
import org.apache.commons.lang3.StringUtils;
2525
import org.apache.flink.api.common.serialization.DeserializationSchema;
26-
import org.apache.flink.api.common.typeinfo.TypeInformation;
2726
import org.apache.flink.runtime.execution.SuppressRestartsException;
2827
import org.apache.flink.streaming.api.datastream.DataStreamSource;
2928
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
@@ -40,12 +39,13 @@
4039
import java.io.BufferedReader;
4140
import java.io.File;
4241
import java.io.FileInputStream;
42+
import java.io.FileNotFoundException;
4343
import java.io.IOException;
4444
import java.io.InputStream;
4545
import java.io.InputStreamReader;
4646
import java.net.URI;
4747
import java.util.Locale;
48-
import java.util.Objects;
48+
import java.util.concurrent.atomic.AtomicBoolean;
4949

5050
/**
5151
* @author tiezhu
@@ -63,113 +63,108 @@ public class FileSource implements IStreamSourceGener<Table>, SourceFunction<Row
6363
/**
6464
* Flag to mark the main work loop as alive.
6565
*/
66-
private volatile boolean running = true;
66+
private final AtomicBoolean running = new AtomicBoolean(true);
6767

68-
private FileSourceTableInfo fileSourceTableInfo;
68+
private URI fileUri;
6969

7070
private InputStream inputStream;
7171

72-
public void setDeserializationSchema(DeserializationSchema<Row> deserializationSchema) {
73-
this.deserializationSchema = deserializationSchema;
74-
}
75-
76-
public void setFileSourceTableInfo(FileSourceTableInfo fileSourceTableInfo) {
77-
this.fileSourceTableInfo = fileSourceTableInfo;
78-
}
79-
80-
public FileSourceTableInfo getFileSourceTableInfo() {
81-
return fileSourceTableInfo;
82-
}
72+
private String charset;
8373

8474
@Override
8575
public Table genStreamSource(AbstractSourceTableInfo sourceTableInfo,
8676
StreamExecutionEnvironment env,
8777
StreamTableEnvironment tableEnv) {
8878
FileSource fileSource = new FileSource();
8979
FileSourceTableInfo tableInfo = (FileSourceTableInfo) sourceTableInfo;
90-
fileSource.setFileSourceTableInfo(tableInfo);
91-
fileSource.setDeserializationSchema(tableInfo.buildDeserializationSchema());
92-
93-
TypeInformation<Row> rowTypeInformation = tableInfo.getRowTypeInformation();
94-
String operatorName = tableInfo.getOperatorName();
9580

96-
DataStreamSource<?> source = env.addSource(fileSource, operatorName, rowTypeInformation);
81+
DataStreamSource<?> source = fileSource.initDataStream(tableInfo, env);
82+
String fields = StringUtils.join(tableInfo.getFields(), ",");
9783

98-
String fields = StringUtils.join(fileSource.getFileSourceTableInfo().getFields(), ",");
9984
return tableEnv.fromDataStream(source, fields);
10085
}
10186

87+
public DataStreamSource<?> initDataStream(FileSourceTableInfo tableInfo,
88+
StreamExecutionEnvironment env) {
89+
deserializationSchema = tableInfo.getDeserializationSchema();
90+
fileUri = URI.create(tableInfo.getFilePath() + SP + tableInfo.getFileName());
91+
charset = tableInfo.getCharsetName();
92+
return env.addSource(
93+
this,
94+
tableInfo.getOperatorName(),
95+
tableInfo.buildRowTypeInfo());
96+
}
97+
10298
/**
10399
* 根据存储位置的不同,获取不同的input stream
104100
*
105-
* @param sourceTableInfo source table info
101+
* @param fileUri file uri
106102
* @return input stream
107-
* @throws Exception reader exception
108103
*/
109-
private InputStream getInputStream(FileSourceTableInfo sourceTableInfo) throws Exception {
110-
switch (sourceTableInfo.getLocation().toLowerCase(Locale.ROOT)) {
111-
case "local":
112-
return fromLocalFile(sourceTableInfo);
113-
case "hdfs":
114-
return fromHdfsFile(sourceTableInfo);
115-
default:
116-
throw new IllegalArgumentException();
104+
private InputStream getInputStream(URI fileUri) {
105+
try {
106+
String scheme = fileUri.getScheme() == null ? "local" : fileUri.getScheme();
107+
switch (scheme.toLowerCase(Locale.ROOT)) {
108+
case "local":
109+
return fromLocalFile(fileUri);
110+
case "hdfs":
111+
return fromHdfsFile(fileUri);
112+
default:
113+
throw new UnsupportedOperationException(
114+
String.format("Unsupported type [%s] of file.", scheme)
115+
);
116+
}
117+
} catch (IOException e) {
118+
throw new SuppressRestartsException(e);
117119
}
118120
}
119121

120122
/**
121123
* 从HDFS上获取文件内容
122124
*
123-
* @param sourceTableInfo source table info
125+
* @param fileUri file uri of file
124126
* @return hdfs file input stream
125-
* @throws Exception reader exception
127+
* @throws IOException reader exception
126128
*/
127-
private InputStream fromHdfsFile(FileSourceTableInfo sourceTableInfo) throws Exception {
128-
String filePath = sourceTableInfo.getFilePath();
129-
String fileName = sourceTableInfo.getFileName();
130-
String path = filePath + SP + fileName;
131-
129+
private InputStream fromHdfsFile(URI fileUri) throws IOException {
132130
Configuration conf = new Configuration();
133-
conf.addResource(new Path(sourceTableInfo.getHdfsSite()));
134-
conf.addResource(new Path(sourceTableInfo.getCoreSite()));
135-
FileSystem fs = FileSystem.newInstance(new URI(filePath), conf, sourceTableInfo.getHdfsUser());
136-
return fs.open(new Path(path));
131+
132+
// get conf from HADOOP_CONF_DIR
133+
String hadoopConfDir = System.getenv("HADOOP_CONF_DIR");
134+
String confHome = hadoopConfDir == null ? "." : hadoopConfDir;
135+
136+
conf.addResource(new Path(confHome + SP + "hdfs-site.xml"));
137+
138+
FileSystem fs = FileSystem.get(fileUri, conf);
139+
return fs.open(new Path(fileUri.getPath()));
137140
}
138141

139142
/**
140143
* 从本地获取文件内容
141144
*
142-
* @param sourceTableInfo source table
145+
* @param fileUri file uri of file
143146
* @return local file input stream
144-
* @throws Exception read exception
147+
* @throws FileNotFoundException read exception
145148
*/
146-
private InputStream fromLocalFile(FileSourceTableInfo sourceTableInfo) throws Exception {
147-
String filePath = sourceTableInfo.getFilePath();
148-
String fileName = sourceTableInfo.getFileName();
149-
String path = filePath + SP + fileName;
150-
File file = new File(path);
149+
private InputStream fromLocalFile(URI fileUri) throws FileNotFoundException {
150+
File file = new File(fileUri.getPath());
151151
if (file.exists()) {
152152
return new FileInputStream(file);
153153
} else {
154154
throw new SuppressRestartsException(new IOException(
155-
String.format(
156-
"File [%s] not exist. File path: [%s]",
157-
sourceTableInfo.getFileName(),
158-
sourceTableInfo.getFilePath())
159-
));
155+
String.format("File not exist. File path: [%s]", fileUri.getPath())));
160156
}
161157
}
162158

163-
164159
@Override
165160
public void run(SourceContext<Row> ctx) throws Exception {
166-
inputStream = getInputStream(fileSourceTableInfo);
167-
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
161+
inputStream = getInputStream(fileUri);
162+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset));
168163

169-
while (running) {
164+
while (running.get()) {
170165
String line = bufferedReader.readLine();
171166
if (line == null) {
172-
running = false;
167+
running.compareAndSet(true, false);
173168
inputStream.close();
174169
break;
175170
} else {
@@ -182,8 +177,8 @@ public void run(SourceContext<Row> ctx) throws Exception {
182177
@Override
183178
public void cancel() {
184179
LOG.info("File source cancel..");
185-
running = false;
186-
if (Objects.nonNull(inputStream)) {
180+
running.compareAndSet(true, false);
181+
if (inputStream != null) {
187182
try {
188183
inputStream.close();
189184
} catch (IOException ioException) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.source.file;
20+
21+
import java.io.File;
22+
23+
/**
24+
* @author tiezhu
25+
* @date 2021/3/22 星期一
26+
* Company dtstack
27+
*/
28+
public class FileSourceConstant {
29+
public static final String TYPE_KEY = "type";
30+
31+
public static final String FORMAT_KEY = "format";
32+
33+
public static final String FIELD_DELIMITER_KEY = "fieldDelimiter";
34+
35+
public static final String FILE_NAME_KEY = "fileName";
36+
37+
public static final String FILE_PATH_KEY = "filePath";
38+
39+
public static final String IGNORE_PARSER_ERROR = "ignoreParserError";
40+
41+
public static final String CHARSET_NAME_KEY = "charsetName";
42+
43+
public static final String AVRO_FORMAT_KEY = "avroFormat";
44+
45+
public static final String NULL_LITERAL_KEY = "nullLiteral";
46+
47+
public static final String ALLOW_COMMENT_KEY = "allowComment";
48+
49+
public static final String ARRAY_ELEMENT_DELIMITER_KEY = "arrayElementDelimiter";
50+
51+
public static final String QUOTA_CHARACTER_KEY = "quoteCharacter";
52+
53+
public static final String ESCAPE_CHARACTER_KEY = "escapeCharacter";
54+
55+
public static final Character DEFAULT_DELIMITER = ',';
56+
57+
public static final String DEFAULT_FILE_FORMAT = "csv";
58+
59+
public static final String DEFAULT_CHARSET = "UTF-8";
60+
61+
public static final String DEFAULT_PATH = ".";
62+
63+
public static final char DEFAULT_QUOTE_CHAR = '"';
64+
65+
public static final char DEFAULT_ESCAPE_CHAR = '\\';
66+
67+
public static final String DEFAULT_NULL_LITERAL = "null";
68+
69+
}

0 commit comments

Comments
 (0)