Hadoop hdfs增删该查 简单写法

来源:互联网 发布:天猫打印发货单软件 编辑:程序博客网 时间:2024/05/18 03:05
package cn.itheima.bigdata.hadoop.hdfs;import java.io.FileNotFoundException;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.LocatedFileStatus;import org.apache.hadoop.fs.Path;import org.apache.hadoop.fs.RemoteIterator;import org.junit.Before;import org.junit.Test;public class HdfsClientEasy {private FileSystem fs = null;@Beforepublic void getFs() throws IOException, Exception{//拿到一个配置参数的封装对象,构造函数中就会对classpath下的xxx-site.xml文件进行解析//真实的项目工程中 应该把 xxx-site.xml 文件加入到工程中//get a configuration objectConfiguration conf = new Configuration();//to set a parameter, figure out the filesystem is hdfsconf.set("fs.defaultFS", "hdfs://itcast:9000/");conf.set("dfs.replication","1");//get a instance of HDFS FileSystem Client//获取到一个具体文件系统的客户端实例对象,产生的实例究竟是哪一种文件系统的客户端//根据conf中的相关参数来决定的//fs = FileSystem.get(conf);//这种获取fs的方法可以指定访问hdfs的客户端身份fs = FileSystem.get(new URI("hdfs://itcast:9000/"), conf, "root");}/** * 上传文件 * @throws IllegalArgumentException * @throws IOException */@Testpublic void testUpload() throws IllegalArgumentException, IOException{fs.copyFromLocalFile(new Path("/home/hadoop/mysoft/jdk-6u25-linux-i586.bin"), new Path("/aa/bb"));}@Testpublic void testRmfile() throws IllegalArgumentException, IOException {boolean res = fs.delete(new Path("/aa/bb"), true);System.out.println(res?"delete is successfully :)":"it is failed :(");}/** * 创建文件夹 * @throws IllegalArgumentException * @throws IOException */@Testpublic void testMkdir() throws IllegalArgumentException, IOException{fs.mkdirs(new Path("/aa/bb"));}/** * 从hdfs中下载 * Could not locate executable null\bin\winutils.exe in the Hadoop binaries. * 在window中解压hadoop 用 * 配置hadoop环境变量  hadoop_home/bin 将bin.zip和lib.zip 解压替换原来的(下载winutils.exe) * bin.zip 和lib.zip在hadoop第二天  windows下开发hadoop程序需要覆盖的本地库文件 * 中 */@Testpublic void testDownload() throws IllegalArgumentException, Exception{fs.copyToLocalFile(new Path("/README.txt"),new Path("d:/test.txt"));}/** * 重命名 * @throws IllegalArgumentException * @throws IOException */@Testpublic void testRename() throws IllegalArgumentException, IOException{fs.rename(new Path("/jdk.tgz"), new Path("/jdk.tgz.rename"));}/** * 列出文件 * @throws FileNotFoundException * @throws IllegalArgumentException * @throws IOException *//* * 在window下访问不了 * win7 默认没有安装telnet  在程序和功能中 打开或关闭Windows功能 中选择telnet安装 * 1.在hosts文件中配置主机名和ip关联  C:\Windows\System32\drivers\etc * 2.ping 主机名能否ping通 * 2.在使用Telnet 测试端口是否是通的  telnet itcast 9000 * 3.如果连不上 看linux防火墙有没有关闭, * 4.查看9000端口线程有没有启动 * [root@itcast home]# netstat -nltp |grep 9000  *  * 在浏览器中访问:192.168.101.106:50070 */@Testpublic void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException{RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);while(listFiles.hasNext()){LocatedFileStatus file = listFiles.next();System.out.println(file.getPath().getName());}System.out.println("--------------------------------------------");FileStatus[] status = fs.listStatus(new Path("/"));for(FileStatus file: status){System.out.println(file.getPath().getName() + "   " + (file.isDirectory()?"d":"f"));}}}


0 0
原创粉丝点击