从本地目录往集群添加整个目录的操作是很简单的 我们只需要知道集群和File文件的操作哪里有区别就行了.File建目录以及写文件请见https://editor.csdn.net/md/?articleId=107520726。下面我们介绍集群遍历本地目录以及创建文件夹
@Before
public void start() throws URISyntaxException, IOException, InterruptedException {
//获取文件系统
configuration = new Configuration();
//配置后在集群上运行
fileSystem = FileSystem.get(new URI("hdfs://hadoop2:9820"), configuration,"***用户名**");
}
@After
public void end() throws IOException {
//关闭文件系统
fileSystem.close();
}
核心代码
//递归文本及目录
public void recursioncontents(String hdfsStr, String localStr,FileSystem fs) throws IOException {
File file = new File(localStr);
File[] files = file.listFiles();
if (files.length == 0){
return;
}else{
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()){
String string = files[i].getPath();
FileInputStream fis = new FileInputStream(new File(string));
String s1 =files[i].getName();
FSDataOutputStream fsDataOutputStream = fs.create(new Path(hdfsStr + "/" + s1));
IOUtils.copyBytes(fis,fsDataOutputStream,configuration);
IOUtils.closeStream(fis);
IOUtils.closeStream(fsDataOutputStream);
}else {
String s1 = hdfsStr + "/" + files[i].getName();//这里与File文件创建目录有区别
String string = files[i].getPath();
fs.mkdirs(new Path(s1));//这里与File文件创建目录有区别
recursioncontents(s1,string,fs);
}
}
}
}
传入路径
@Test
public void Recursion() throws IOException {
String hdfsStr = "/Result";
String localStr = "D:\\Hadoop_Testresult";
recursioncontents(hdfsStr,localStr,fileSystem);
}