package com.wxj.hdfs;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/*
* Created by wxj on 2019/8/3 0003 17:38
*/
public class HdfsOperateStudy {
/**
* @des 第一种获取FileSystem的方法
* @author wxj
* @paramters
* @date 2019/8/3 0003 19:37
* @return
*/
@Test
public void getFileSystem() throws URISyntaxException, IOException {
//获取配置项
Configuration conf =new Configuration();
//获取hdfs文件系统
FileSystem fs = FileSystem.get(new URI("hdfs://***.***.***.***:8082"),conf);
System.out.println(fs.toString());
}
//第二种获取FileSystem的方法
@Test
public void getFileSystem2() throws URISyntaxException, IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://***.***.***.***:8020");
FileSystem fs = FileSystem.get(new URI("/"),conf);
System.out.println(fs);
}
//第三种获取FileSystem的方法
@Test
public void getFileSystem3() throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://***.***.***.***:8020");
FileSystem fs =FileSystem.newInstance(conf);
System.out.println(fs);
}
//第四种获取FileSystem的方法
@Test
public void getFileSystem4() throws IOException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem fs =FileSystem.newInstance(new URI("hdfs://***.***.***.***:8020"),conf);
System.out.println(fs);
}
/**
* @des 创建文件夹
* @author wxj
* @paramters []
* @date 2019/8/4 0004 10:17
* @return void
*/
@Test
public void mkHdfsDir() throws IOException, URISyntaxException {
//获取hdfs 文件系统
FileSystem fs = FileSystem.get(new URI("hdfs://***.***.***.***:8020"),new Configuration());
fs.mkdirs(new Path("/hello/mydir/test"));
fs.close();
}
/**
* @des 删除文件或文件夹
* @author wxj
* @paramters []
* @date 2019/8/4 0004 12:55
* @return void
*/
@Test
public void delHdfsDir() throws URISyntaxException, IOException {
/**
* @des delHdfsDir
* @author wxj
* @paramters []
* @date 2019/8/4 0004 12:55
* @return void
*/
//获取hdfs 文件系统
FileSystem fs = FileSystem.get(new URI("hdfs://***.***.***.***:8020"),new Configuration());
//fs.deleteOnExit(new Path("/hello/mydir/test/"));//只删除最下面的目录
//fs.delete(new Path("/hello/mydir/test"),false);//文件夹下有文件和文件夹会删除失败,/hello/mydir/test is non empty
fs.delete(new Path("/hello/mydir/test"),true);//只删除最下面的目录以及目录中文件
//fs.delete(new Path("/hello/mydir/test/"));
fs.close();
}
/**
* @des 复制hdfs文件到本地
* @author wxj
* @paramters []
* @date 2019/8/4 0004 10:22
* @return void
*/
@Test
public void copyFsToLocal() throws URISyntaxException, IOException, InterruptedException {
//获取hdfs 文件系统
FileSystem fs = FileSystem.get(new URI("hdfs://***.***.***.***:8020"),new Configuration());
//获取指定文件流
FSDataInputStream open = fs.open(new Path("/test/input/install.log"));
//创建输出流
FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\install.log"));
//复制流
IOUtils.copy(open,fileOutputStream);
//关闭流
IOUtils.closeQuietly(fileOutputStream);
IOUtils.closeQuietly(open);
fs.close();
}
/**
* @des 上传本地文件到hdfs
* @author wxj
* @paramters []
* @date 2019/8/4 0004 10:25
* @return void
*/
@Test
public void moveFileToHdfs() throws URISyntaxException, IOException {
//获取FileSystem
FileSystem fs = FileSystem.get(new URI("hdfs://***.***.***.***:8020"),new Configuration());
//如果hdfs路径不存在,会创建目录
fs.copyFromLocalFile(new Path("file:///C:\\Users\\Administrator\\Desktop\\install.log"),new Path("/hello/mydir/test"));
fs.close();
}
/**
* @des 获取所有的文件目录
* @author wxj
* @paramters []
* @date 2019/8/4 0004 12:57
* @return void
*/
@Test
public void listMyFiles()throws Exception{
//获取fileSystem类
FileSystem fileSystem = FileSystem.get(new URI("hdfs://***.***.***.***:8020"), new Configuration());
//获取RemoteIterator 得到所有的文件或者文件夹,第一个参数指定遍历的路径,第二个参数表示是否要递归遍历
RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"), true);
while (locatedFileStatusRemoteIterator.hasNext()){
LocatedFileStatus next = locatedFileStatusRemoteIterator.next();
System.out.println(next.getPath().toString());
}
fileSystem.close();
}
}
文中的地址已经使用*替换,使用的时候替换成自己要操作的hdfs地址就可以