23
23
import org .apache .commons .lang3 .StringUtils ;
24
24
import org .apache .commons .lang3 .tuple .Pair ;
25
25
import org .apache .hadoop .conf .Configuration ;
26
- import org .apache .hadoop .fs .CommonConfigurationKeys ;
27
26
import org .apache .hadoop .fs .FSDataInputStream ;
28
27
import org .apache .hadoop .fs .FSDataOutputStream ;
29
28
import org .apache .hadoop .fs .FileStatus ;
43
42
import java .util .ArrayList ;
44
43
import java .util .List ;
45
44
46
- import static org .apache .parquet .avro .AvroReadSupport .READ_INT96_AS_FIXED ;
47
- import static org .apache .parquet .avro .AvroSchemaConverter .ADD_LIST_ELEMENT_RECORDS ;
48
- import static org .apache .parquet .avro .AvroWriteSupport .WRITE_FIXED_AS_INT96 ;
49
- import static org .apache .parquet .avro .AvroWriteSupport .WRITE_OLD_LIST_STRUCTURE ;
50
-
51
45
@ Slf4j
52
46
public class HadoopFileSystemProxy implements Serializable , Closeable {
53
47
@@ -64,30 +58,19 @@ public HadoopFileSystemProxy(@NonNull HadoopConf hadoopConf) {
64
58
}
65
59
66
60
public boolean fileExist (@ NonNull String filePath ) throws IOException {
67
- if (fileSystem == null ) {
68
- initialize ();
69
- }
70
- Path fileName = new Path (filePath );
71
- return fileSystem .exists (fileName );
61
+ return getFileSystem ().exists (new Path (filePath ));
72
62
}
73
63
74
64
public void createFile (@ NonNull String filePath ) throws IOException {
75
- if (fileSystem == null ) {
76
- initialize ();
77
- }
78
- Path path = new Path (filePath );
79
- if (!fileSystem .createNewFile (path )) {
65
+ if (!getFileSystem ().createNewFile (new Path (filePath ))) {
80
66
throw CommonError .fileOperationFailed ("SeaTunnel" , "create" , filePath );
81
67
}
82
68
}
83
69
84
70
public void deleteFile (@ NonNull String filePath ) throws IOException {
85
- if (fileSystem == null ) {
86
- initialize ();
87
- }
88
71
Path path = new Path (filePath );
89
- if (fileSystem .exists (path )) {
90
- if (!fileSystem .delete (path , true )) {
72
+ if (getFileSystem () .exists (path )) {
73
+ if (!getFileSystem () .delete (path , true )) {
91
74
throw CommonError .fileOperationFailed ("SeaTunnel" , "delete" , filePath );
92
75
}
93
76
}
@@ -98,9 +81,6 @@ public void renameFile(
98
81
@ NonNull String newFilePath ,
99
82
boolean removeWhenNewFilePathExist )
100
83
throws IOException {
101
- if (fileSystem == null ) {
102
- initialize ();
103
- }
104
84
Path oldPath = new Path (oldFilePath );
105
85
Path newPath = new Path (newFilePath );
106
86
@@ -116,15 +96,15 @@ public void renameFile(
116
96
117
97
if (removeWhenNewFilePathExist ) {
118
98
if (fileExist (newFilePath )) {
119
- fileSystem .delete (newPath , true );
99
+ getFileSystem () .delete (newPath , true );
120
100
log .info ("Delete already file: {}" , newPath );
121
101
}
122
102
}
123
103
if (!fileExist (newPath .getParent ().toString ())) {
124
104
createDir (newPath .getParent ().toString ());
125
105
}
126
106
127
- if (fileSystem .rename (oldPath , newPath )) {
107
+ if (getFileSystem () .rename (oldPath , newPath )) {
128
108
log .info ("rename file :[" + oldPath + "] to [" + newPath + "] finish" );
129
109
} else {
130
110
throw CommonError .fileOperationFailed (
@@ -133,42 +113,33 @@ public void renameFile(
133
113
}
134
114
135
115
public void createDir (@ NonNull String filePath ) throws IOException {
136
- if (fileSystem == null ) {
137
- initialize ();
138
- }
139
116
Path dfs = new Path (filePath );
140
- if (!fileSystem .mkdirs (dfs )) {
117
+ if (!getFileSystem () .mkdirs (dfs )) {
141
118
throw CommonError .fileOperationFailed ("SeaTunnel" , "create" , filePath );
142
119
}
143
120
}
144
121
145
122
public List <LocatedFileStatus > listFile (String path ) throws IOException {
146
- if (fileSystem == null ) {
147
- initialize ();
148
- }
149
123
List <LocatedFileStatus > fileList = new ArrayList <>();
150
124
if (!fileExist (path )) {
151
125
return fileList ;
152
126
}
153
127
Path fileName = new Path (path );
154
128
RemoteIterator <LocatedFileStatus > locatedFileStatusRemoteIterator =
155
- fileSystem .listFiles (fileName , false );
129
+ getFileSystem () .listFiles (fileName , false );
156
130
while (locatedFileStatusRemoteIterator .hasNext ()) {
157
131
fileList .add (locatedFileStatusRemoteIterator .next ());
158
132
}
159
133
return fileList ;
160
134
}
161
135
162
136
public List <Path > getAllSubFiles (@ NonNull String filePath ) throws IOException {
163
- if (fileSystem == null ) {
164
- initialize ();
165
- }
166
137
List <Path > pathList = new ArrayList <>();
167
138
if (!fileExist (filePath )) {
168
139
return pathList ;
169
140
}
170
141
Path fileName = new Path (filePath );
171
- FileStatus [] status = fileSystem .listStatus (fileName );
142
+ FileStatus [] status = getFileSystem () .listStatus (fileName );
172
143
if (status != null ) {
173
144
for (FileStatus fileStatus : status ) {
174
145
if (fileStatus .isDirectory ()) {
@@ -180,31 +151,26 @@ public List<Path> getAllSubFiles(@NonNull String filePath) throws IOException {
180
151
}
181
152
182
153
public FileStatus [] listStatus (String filePath ) throws IOException {
183
- if (fileSystem == null ) {
184
- initialize ();
185
- }
186
- return fileSystem .listStatus (new Path (filePath ));
154
+ return getFileSystem ().listStatus (new Path (filePath ));
187
155
}
188
156
189
157
public FileStatus getFileStatus (String filePath ) throws IOException {
190
- if (fileSystem == null ) {
191
- initialize ();
192
- }
193
- return fileSystem .getFileStatus (new Path (filePath ));
158
+ return getFileSystem ().getFileStatus (new Path (filePath ));
194
159
}
195
160
196
161
public FSDataOutputStream getOutputStream (String filePath ) throws IOException {
197
- if (fileSystem == null ) {
198
- initialize ();
199
- }
200
- return fileSystem .create (new Path (filePath ), true );
162
+ return getFileSystem ().create (new Path (filePath ), true );
201
163
}
202
164
203
165
public FSDataInputStream getInputStream (String filePath ) throws IOException {
166
+ return getFileSystem ().open (new Path (filePath ));
167
+ }
168
+
169
+ public FileSystem getFileSystem () {
204
170
if (fileSystem == null ) {
205
171
initialize ();
206
172
}
207
- return fileSystem . open ( new Path ( filePath )) ;
173
+ return fileSystem ;
208
174
}
209
175
210
176
@ SneakyThrows
@@ -258,16 +224,7 @@ private void initialize() {
258
224
}
259
225
260
226
private Configuration createConfiguration () {
261
- Configuration configuration = new Configuration ();
262
- configuration .setBoolean (READ_INT96_AS_FIXED , true );
263
- configuration .setBoolean (WRITE_FIXED_AS_INT96 , true );
264
- configuration .setBoolean (ADD_LIST_ELEMENT_RECORDS , false );
265
- configuration .setBoolean (WRITE_OLD_LIST_STRUCTURE , true );
266
- configuration .set (CommonConfigurationKeys .FS_DEFAULT_NAME_KEY , hadoopConf .getHdfsNameKey ());
267
- configuration .setBoolean (
268
- String .format ("fs.%s.impl.disable.cache" , hadoopConf .getSchema ()), true );
269
- configuration .set (
270
- String .format ("fs.%s.impl" , hadoopConf .getSchema ()), hadoopConf .getFsHdfsImpl ());
227
+ Configuration configuration = hadoopConf .toConfiguration ();
271
228
hadoopConf .setExtraOptionsForConfiguration (configuration );
272
229
return configuration ;
273
230
}
0 commit comments