获取hdfs文件系统对象
java操作hdfs,需要先获取到文件对象,执行url和用户名等,连接配置很多,需要实际项目需要时补充,基础可用的简单代码如下:
private static String hdfsPath = "hdfs://192.168.139.9:9000";
/**
* 获取HDFS文件系统对象
*
* @return
* @throws Exception
*/
private static FileSystem getFileSystem() throws Exception
{
FileSystem fileSystem = FileSystem.get(new URI(hdfsPath), getConfiguration(), "root");
return fileSystem;
}
/**
* 获取HDFS配置信息
*
* @return
*/
private static Configuration getConfiguration() {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", hdfsPath);
return configuration;
}
java中hdfs基础的增删改查
连接上hdfs,获取到文件系统对象后,就可以进行相关的操作,如下所示:
/**
* 在HDFS创建文件夹
*
* @param path
* @return
* @throws Exception
*/
public static void mkdir(String path) throws Exception
{
FileSystem fs = getFileSystem();
// 目标路径
Path srcPath = new Path(path);
boolean isOk = fs.mkdirs(srcPath);
fs.close();
}
/**
* 读取HDFS目录信息
*
* @param path
* @return
* @throws Exception
*/
public static void readPathInfo(String path)
throws Exception
{
FileSystem fs = getFileSystem();
// 目标路径
Path newPath = new Path(path);
FileStatus[] statusList = fs.listStatus(newPath);
List<Map<String, Object>> list = new ArrayList<>();
if (null != statusList && statusList.length > 0) {
for (FileStatus fileStatus : statusList) {
System.out.print("filePath:"+fileStatus.getPath());
System.out.println(",fileStatus:"+ fileStatus.toString());
}
}
}
/**
* HDFS创建文件
*
* @throws Exception
*/
public static void createFile()
throws Exception
{
File myFile = new File("C:\\Users\\tuzongxun\\Desktop\\tzx.txt");
FileInputStream fis = new FileInputStream(myFile);
String fileName = myFile.getName();
FileSystem fs = getFileSystem();
// 上传时默认当前目录,后面自动拼接文件的目录
Path newPath = new Path("/demo1/" + fileName);
// 打开一个输出流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
FSDataOutputStream outputStream = fs.create(newPath);
outputStream.write(bos.toByteArray());
outputStream.close();
fs.close();
}
/**
* 读取文件列表
* @param path
* @throws Exception
*/
public static void listFile(String path)
throws Exception
{
FileSystem fs = getFileSystem();
// 目标路径
Path srcPath = new Path(path);
// 递归找到所有文件
RemoteIterator<LocatedFileStatus> filesList = fs.listFiles(srcPath, true);
while (filesList.hasNext()) {
LocatedFileStatus next = filesList.next();
String fileName = next.getPath().getName();
Path filePath = next.getPath();
System.out.println("##########################fileName:" + fileName);
System.out.println("##########################filePath:" + filePath.toString());
}
fs.close();
}
/**
* 读取HDFS文件内容
*
* @param path
* @return
* @throws Exception
*/
public static String readFile(String path) throws Exception
{
FileSystem fs = getFileSystem();
// 目标路径
Path srcPath = new Path(path);
FSDataInputStream inputStream = null;
try {
inputStream = fs.open(srcPath);
// 防止中文乱码
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String lineTxt = "";
StringBuffer sb = new StringBuffer();
while ((lineTxt = reader.readLine()) != null) {
sb.append(lineTxt);
}
return sb.toString();
}
finally {
inputStream.close();
fs.close();
}
}
/**
* 上传HDFS文件
*
* @param path
* @param uploadPath
* @throws Exception
*/
public static void uploadFile(String path, String uploadPath) throws Exception
{
if (StringUtils.isEmpty(path) || StringUtils.isEmpty(uploadPath)) {
return;
}
FileSystem fs = getFileSystem();
// 上传路径
Path clientPath = new Path(path);
// 目标路径
Path serverPath = new Path(uploadPath);
// 调用文件系统的文件复制方法,第一个参数是否删除原文件true为删除,默认为false
fs.copyFromLocalFile(false, clientPath, serverPath);
fs.close();
}
/**
* 调用
* @param args
*/
public static void main(String[] args) {
try {
//创建目录
//mkdir("/test2");
//列出目录列表
readPathInfo("/");
//列出文件列表
// listFile("/");
// 创建文件
// createFile();
// 读取文件内容
String a = readFile("/test/test2.txt");
// System.out.println("###########################" + a);
//上传文件
//uploadFile("C:\\Users\\tuzongxun\\Desktop\\tzx.txt", "/test2");
}
catch (Exception e) {
e.printStackTrace();
}
}