一脸懵逼学习hadoop之HDFS的java客户端编写
2017-09-11 18:30
489 查看
1:eclipse创建一个项目,然后导入对应的jar包:
鼠标右击项目,点击properties或者alt+enter快捷键--->java build path--->libraries--->add library--->user library--->next--->user libraries--->new--->hdfsLib(根据自己的需要填写)---》add external jars(添加自己的需求包):
2:开始添加自己的需求包,路径如
hadoop-2.4.1\share\hadoop\hdfs的hadoop-hdfs-2.4.1.jar和hadoop-2.4.1\share\hadoop\hdfs\lib下面的全部包;
hadoop-2.4.1\share\hadoop\common的hadoop-common-2.4.1.jar和hadoop-2.4.1\share\hadoop\common\lib下面的全部包;
3:NameNode的职责
(1):维护元数据的信息;
(2):维护hdfs的目录树;
(3):响应客户端的请求;
鼠标右击项目,点击properties或者alt+enter快捷键--->java build path--->libraries--->add library--->user library--->next--->user libraries--->new--->hdfsLib(根据自己的需要填写)---》add external jars(添加自己的需求包):
2:开始添加自己的需求包,路径如
hadoop-2.4.1\share\hadoop\hdfs的hadoop-hdfs-2.4.1.jar和hadoop-2.4.1\share\hadoop\hdfs\lib下面的全部包;
hadoop-2.4.1\share\hadoop\common的hadoop-common-2.4.1.jar和hadoop-2.4.1\share\hadoop\common\lib下面的全部包;
package com.master01; import java.io.FileInputStream; import java.io.IOException; import java.net.URISyntaxException; import org.apache.commons.io.IOUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; 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; public class HdfsTest { //public FileSystem fs = null; /* @Before public void init() throws IOException, InterruptedException, URISyntaxException{ //读配置文件 Configuration conf = new Configuration(); //这里直接拷贝配置或者直接设置值 conf.set("fs.defaultFS", "hdfs://master:9000/"); //获取配置文件里面的内容 fs = FileSystem.get(conf); //fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root"); } */ /** * 上传文件 * @throws IOException */ public static void upload() throws IOException{ //读配置文件 //读取classpath下的core-site.xml配置文件,并且解析其的内容,封装到conf的对象中; Configuration conf = new Configuration(); //这里直接拷贝配置或者直接设置值 //也可以在代码中对conf的配置信息进行手动设置,会覆盖配置文件中的配置信息 conf.set("fs.defaultFS", "hdfs://master:9000"); //获取配置文件里面的内容 //根据配置信息,去获取一个具体文件系统的客户端操作实例对象 FileSystem fs = FileSystem.get(conf); //本地文件是输入流,hdfs是输出流 //先搞出路径 Path src = new Path("hdfs://master:9000/aa/test.txt"); //搞出输出流,即向hdfs上面写内容 FSDataOutputStream create = fs.create(src); //输入流就是读,本地文件,输入流 FileInputStream fileInputStream = new FileInputStream("d:/test.txt"); //将文件fileInputStream到create即完成上传到hdfs IOUtils.copy(fileInputStream, create); } //最快的上传文件的方法 public void upload02() throws IllegalArgumentException, IOException, InterruptedException, URISyntaxException{ //读配置文件 Configuration conf = new Configuration(); //这里直接拷贝配置或者直接设置值 conf.set("fs.defaultFS", "hdfs://master:9000"); //获取配置文件里面的内容 FileSystem fs = FileSystem.get(conf); //FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root"); fs.copyFromLocalFile(new Path("d:/test.txt"), new Path("hdfs://master:9000/aa/test.txt")); } /** * 下载文件 * @throws IOException * @throws IllegalArgumentException */ public void download02() throws IllegalArgumentException, IOException{ //去配置文件 Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://master:9000"); //获取配置文件里面的内容 FileSystem fs = FileSystem.get(conf); fs.copyToLocalFile(new Path("hdfs://master:9000/aa/test.txt"), new Path("d:/test2.txt")); } /*** * 创建文件夹的方法 * @throws IOException */ public void mkdir02() throws IOException{ //主配置文件 Configuration conf = new Configuration(); //设置配置文件的值 conf.set("fs.defaultFS", "hdfs://master:9000"); //获取配置文件里面的内容 FileSystem fs = FileSystem.get(conf); //文件夹的创建 fs.mkdirs(new Path("hdfs://master:9000/aaa/bbb/ccc")); } /** * 删除文件 * @throws IOException */ public void remove02() throws IOException{ //主配置文件 Configuration conf = new Configuration(); //设置值 conf.set("fs.defaultFS", "hdfs://master:9000"); //获取配置文件里面的内容 FileSystem fs = FileSystem.get(conf); //执行删除操作 fs.delete(new Path("hdfs://master:9000/aaa/bbb/ccc"), true); } /** * 文件的移动 * @throws IOException */ public void move() throws IOException{ //主配置文件 Configuration conf = new Configuration(); //设置值 conf.set("fs.defaultFS", "hdfs://master:9000"); //获取配置文件里面的内容 FileSystem fs = FileSystem.get(conf); //移动操作 fs.rename(new Path("hdfs://master:9000/aa/test.txt"), new Path("hdfs://master:9000/aaa/bbb")); } /*** * 查看文件的信息 * @throws IOException */ public void listFiles() throws IOException{ //主配置文件 Configuration conf = new Configuration(); //设置值 conf.set("fs.defaultFS", "hdfs://master:9000"); //获取配置文件里面的内容 FileSystem fs = FileSystem.get(conf); //查看的是文件,不是文件夹 //listFiles列出的是文件信息,而且提供递归遍历 RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("hdfs://master:9000/"), true); //迭代输出信息 while(listFiles.hasNext()){ LocatedFileStatus file = listFiles.next(); //文件路径 Path path = file.getPath(); System.out.println(path.getName()); } System.out.println("============================================="); //listStatus列出文件和文件夹的信息,但是不提供自带的递归遍历 FileStatus[] listStatus = fs.listStatus(new Path("hdfs://master:9000/")); /*for(int i = 0 ; i<listStatus.length; i++){ System.out.println(listStatus[i]); }*/ for(FileStatus fileStatus : listStatus){ //根据获取的路径获取文件夹的名称 Path path = fileStatus.getPath(); System.out.println(path.getName()); } } public static void main(String[] args) { HdfsTest hdfsTest = new HdfsTest(); try { //上传文件的调用 //hdfsTest.upload02(); //下载文件的调用 //hdfsTest.download02(); //文件夹的创建 //hdfsTest.mkdir02(); //删除操作 //hdfsTest.remove02(); //移动文件的操作 //hdfsTest.move(); //查看文件信息 hdfsTest.listFiles(); } catch (Exception e) { e.printStackTrace(); } } }
3:NameNode的职责
(1):维护元数据的信息;
(2):维护hdfs的目录树;
(3):响应客户端的请求;
相关文章推荐
- 一脸懵逼加从入门到绝望学习hadoop之Caused by: java.net.UnknownHostException: master报错
- 【Hadoop】HDFS的java客户端编写
- Hadoop3-HDFS的java客户端编写
- Hadoop学习笔记——1.java读取Oracle中表的数据,创建新文件写入Hdfs
- HBase学习笔记1 - 如何编写高性能的客户端Java代码
- 和我一起学Hadoop(四):java客户端操作HDFS
- hadoop生态系统学习之路(三)java实现上传文件(本地或ftp)至hdfs
- hadoop 学习(四)之java操作hdfs
- Hadoop学习笔记(3)-java操作hdfs的API接口
- hadoop学习之HDFS(2.4):hadoop数据类型与java数据类型的对比与转换
- hadoop学习之HDFS(2.8):hdfs的javaAPI使用及示例
- Hadoop之HDFS客户端------java实现
- hadoop学习1 java操作HDFS
- 云计算学习笔记---异常处理---hadoop问题处理ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: java.lang.NullPoin
- hadoop学习笔记--5.HDFS的java api接口访问
- hadoop系列二:HDFS文件系统的命令及JAVA客户端API
- 使用JAVA客户端对HDFS进行代码编写(五)
- hadoop学习:Java对HDFS的基本操作
- HDFS的JAVA客户端编写
- Hadoop—HDFS的Java客户端编程