Flink连接知识库实现方案初始准备

This commit is contained in:
fy
2022-11-14 14:34:00 +08:00
parent 0a6f36393c
commit c58acdcfc9
15 changed files with 1315 additions and 5 deletions

View File

@@ -0,0 +1,56 @@
package com.zdjizhi.utils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class HdfsUtils {
private static final Logger logger = LoggerFactory.getLogger(HdfsUtils.class);
private static FileSystem fileSystem;
static {
Configuration configuration = new Configuration();
try {
//创建fileSystem,用于连接hdfs
fileSystem = FileSystem.get(configuration);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static byte[] getFileBytes(String filePath) throws IOException {
FSDataInputStream open = null;
try {
open = fileSystem.open(new Path(filePath));
byte[] bytes = new byte[open.available()];
open.read(0,bytes,0, open.available());
return bytes;
} finally {
if (open != null) {
open.close();
}
}
}
public static void uploadFileByBytes(String filePath, byte[] bytes) throws IOException {
FSDataOutputStream fsDataOutputStream = null;
try {
fsDataOutputStream = fileSystem.create(new Path(filePath), true);
fsDataOutputStream.write(bytes);
} finally {
if (fsDataOutputStream != null) {
fsDataOutputStream.close();
}
}
}
}