持续更新中。。。。
mainController
package cn.object.demo02.controller;
import cn.object.demo02.HDFS;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
//Rest模式
@RestController
@RequestMapping(value = "/hdfs"/*,method = RequestMethod.GET*/)
public class mainController {
@GetMapping
public String getById(){
System.out.println("Springboot is running");
return "Springboot is running";
}
@RequestMapping(value = "/getRootDirectory",method = RequestMethod.GET)
public List<String> getDirectory() throws IOException {
return HDFS.showRoot();
}
@RequestMapping(value = "/{str}",method = RequestMethod.DELETE)
public String delete(@PathVariable String str){
System.out.println(str);
return str;
}
// @Request
}
Demo02Application
package cn.object.demo02;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
@SpringBootApplication
public class Demo02Application {
private static FileSystem hdfs;
public static void main(String[] args) throws IOException {
HDFS.init();
SpringApplication.run(Demo02Application.class, args);
}
}
HDFS
package cn.object.demo02;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HDFS {
public static FileSystem hdfs;
public static List<String> hdfsDirectories = new ArrayList<>();
public static void init() throws IOException {
System.out.println("-----------hdfs-starting---------");
Configuration conf = new Configuration();
// conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
conf.set("fs.defaultFS","hdfs://centos01:9000");
hdfs = FileSystem.get(conf);
System.out.println("------------hdfs-running--------");
}
// public static void
public static List<String> showRoot() throws IOException {
FileStatus fs = hdfs.getFileStatus(new Path("hdfs:/"));
showDir(fs);
return hdfsDirectories;
}
private static void showDir(FileStatus fs) throws IOException {
Path path = fs.getPath();
hdfsDirectories.add(path.toString());
if(fs.isDirectory()){
FileStatus[] f = hdfs.listStatus(path);
if(f.length > 0)
for(FileStatus file : f)
showDir(file);
}
}
}