说明:本测试使用maven管理项目结构,测试前,请把 core-site.xml 拷贝到resource目录
package hadoop.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class Hdfstest {
public Hdfstest() throws IOException{
// 创建于hdfs的连接(操作)对象
hdfs = FileSystem.get(conf) ;
}
//配置hadoop配置信息对象
public final Configuration conf = new Configuration();
// 声明hdfs文件系统操作对象
public FileSystem hdfs ;
public void createHdfsFile(String path) throws IOException{
Path p = new Path(path);
// 根据path创建文件
FSDataOutputStream od = hdfs.create(p) ;
//od.writeUTF("hello" );
od.write("gbdghrherghr".getBytes());
}
// 内容追加
public void appendHdfsFile(String path,String content) throws IOException{
Path p = new Path(path) ;
FSDataOutputStream os = hdfs.append(p) ;
os.write(content.getBytes());
}
// 文件读取
public void readHdfsFile(String path) throws IOException{
Path p = new Path(path) ;
FSDataInputStream fp = hdfs.open(p) ;
InputStreamReader isr = new InputStreamReader(fp) ;
BufferedReader br = new BufferedReader(isr) ;
String line = br.readLine() ;
while(line !=null){
System.out.println(line);
line = br.readLine() ;
}
}
// 遍历输出hdfs目录
public void listHdfsFiles(String path) throws IOException{
Path p = new Path(path);
//判断给定的(路径)目录是否存在
boolean isExists = hdfs.exists(p) ;
if(isExists){
boolean idDirectory = hdfs.isDirectory(p) ;
boolean isFile = hdfs.isFile(p) ;
if(idDirectory){
System.out.println(p);
FileStatus[] fastatus = hdfs.listStatus(p) ;
for(FileStatus fs : fastatus){
Path cp = fs.getPath() ;
if(path.equals("/")){
listHdfsFiles(path+cp.getName()) ;
}else{
listHdfsFiles(path+"/"+cp.getName()) ;
}
}
}else if(isFile){
System.out.println(p);
}
}
}
// 文件删除
public void deleteHdfsFile(String path) throws IOException{
Path p = new Path(path);
hdfs.delete(p, true) ;
}
// 文件上传
public void uploadFile(String res,String des) throws IOException{
Path resPath = new Path(res) ;
Path desPath = new Path(des) ;
hdfs.copyFromLocalFile(resPath, desPath);
}
// 文件下载
public void copyTOLocalFile(String res,String des) throws IOException{
Path resPath = new Path(res) ;
Path desPath = new Path(des) ;
hdfs.copyToLocalFile(resPath, desPath);
}
public static void main(String[] args) throws IOException {
Hdfstest dfstest =new Hdfstest();
// dfstest.createHdfsFile("/hdfstest");
// 内容追加
// dfstest.appendHdfsFile("/hdfstest", "\n 34638767896");
// dfstest.readHdfsFile("/hdfstest") ;
// dfstest.listHdfsFiles("/") ;
// dfstest.deleteHdfsFile("/hdfstest") ;
// dfstest.uploadFile("D:\\KuGou","/user/root/input") ;
dfstest.copyTOLocalFile("/user/root/input/KuGou","D:\\KuGou") ;
dfstest.hdfs.close();
}
}