hdfs api java_HDFS中JAVA API的使用

本文档介绍了如何使用HDFS Java API进行文件操作,包括创建文件、上传本地文件、重命名文件、删除文件、创建目录以及读取文件内容。示例代码详细展示了每个操作的过程,便于理解和实践。
摘要由CSDN通过智能技术生成

importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.FSDataOutputStream;importorg.apache.hadoop.fs.FileStatus;importorg.apache.hadoop.fs.FileSystem;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.io.IOUtils;public classHdfsTest {//创建新文件

public static void createFile(String dst , byte[] contents) throwsIOException{

Configuration conf= newConfiguration();

FileSystem fs=FileSystem.get(conf);

Path dstPath= new Path(dst); //目标路径//打开一个输出流

FSDataOutputStream outputStream =fs.create(dstPath);

outputStream.write(contents);

outputStream.close();

fs.close();

System.out.println("文件创建成功!");

}//上传本地文件

public static void uploadFile(String src,String dst) throwsIOException{

Configuration conf= newConfiguration();

FileSystem fs=FileSystem.get(conf);

Path srcPath= new Path(src); //原路径

Path dstPath = new Path(dst); //目标路径//调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false

fs.copyFromLocalFile(false,srcPath, dstPath);//打印文件路径

System.out.println("Upload to "+conf.get("fs.default.name"));

System.out.println("------------list files------------"+"\n");

FileStatus [] fileStatus=fs.listStatus(dstPath);for(FileStatus file : fileStatus)

{

System.out.println(file.getPath());

}

fs.close();

}//文件重命名

public static void rename(String oldName,String newName) throwsIOException{

Configuration conf= newConfiguration();

FileSystem fs=FileSystem.get(conf);

Path oldPath= newPath(oldName);

Path newPath= newPath(newName);boolean isok =fs.rename(oldPath, newPath);if(isok){

System.out.println("rename ok!");

}else{

System.out.println("rename failure");

}

fs.close();

}//删除文件

public static void delete(String filePath) throwsIOException{

Configuration conf= newConfiguration();

FileSystem fs=FileSystem.get(conf);

Path path= newPath(filePath);boolean isok =fs.deleteOnExit(path);if(isok){

System.out.println("delete ok!");

}else{

System.out.println("delete failure");

}

fs.close();

}//创建目录

public static void mkdir(String path) throwsIOException{

Configuration conf= newConfiguration();

FileSystem fs=FileSystem.get(conf);

Path srcPath= newPath(path);boolean isok =fs.mkdirs(srcPath);if(isok){

System.out.println("create dir ok!");

}else{

System.out.println("create dir failure");

}

fs.close();

}//读取文件的内容

public static void readFile(String filePath) throwsIOException{

Configuration conf= newConfiguration();

FileSystem fs=FileSystem.get(conf);

Path srcPath= newPath(filePath);

InputStream in= null;try{

in=fs.open(srcPath);

IOUtils.copyBytes(in, System.out,4096, false); //复制到标准输出流

} finally{

IOUtils.closeStream(in);

}

}public static void main(String[] args) throwsIOException {//测试上传文件//uploadFile("D:\\c.txt", "/user/hadoop/test/");//测试创建文件

/*byte[] contents = "hello world 世界你好\n".getBytes();

createFile("/user/hadoop/test1/d.txt",contents);*/

//测试重命名//rename("/user/hadoop/test/d.txt", "/user/hadoop/test/dd.txt");//测试删除文件//delete("test/dd.txt");//使用相对路径//delete("test1");//删除目录//测试新建目录//mkdir("test1");//测试读取文件

readFile("test1/d.txt");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值