HDFS的java客户端编写
2017-07-03 14:09
453 查看
package it.cast.hdfs; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; import org.apache.commons.io.IOUtils; 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.LocatedFileStatus; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.RemoteIterator; import org.junit.Before; import org.junit.Test; import com.sun.org.apache.xalan.internal.xsltc.compiler.sym; public class HdfsUtil { FileSystem fs=null; @Before public void init() throws Exception{ //读取classpath下的xxx-site.xml配置文件,并解析其内容,封装到conf对象中 Configuration conf=new Configuration(); //也可以在代码中对conf中的配置信息进行手动设置,会覆盖掉配置文件中读取的值 conf.set("fs.defaultFS", "hdfs://weekend110:9000/"); //根据配置信息,去获取一个具体文件系统的客户端操作实例 fs=FileSystem.get(new URI("hdfs://weekend110:9000/"),conf,"hadoop"); } /** * 上传文件 * @throws IOException */ @Test public void upload() throws IOException{ Path dst=new Path("hdfs://weekend110:9000//aa/qingshu.txt"); FSDataOutputStream os=fs.create(dst); FileInputStream is=new FileInputStream("c:/qingshu.txt"); IOUtils.copy(is, os); } @Test public void upload2() throws IllegalArgumentException, IOException{ fs.copyFromLocalFile(new Path("c:/qingshu.txt"), new Path("hdfs://weekend110:9000/aa/qingshu2.txt")); } /** * 下载文件 * @throws IOException * @throws IllegalArgumentException */ public void download() throws IllegalArgumentException, IOException{ fs.copyToLocalFile(new Path("hdfs://weekend110:9000/aa/qingshu2.txt"),new Path("c:/qingshu2.txt")); } /** * 查看文件信息 * @throws IOException * @throws IllegalArgumentException * @throws FileNotFoundException */ @Test public void listFiles() throws FileNotFoundException, IllegalArgumentException, IOException{ //ListFiles 列出的是文件信息,而且提供递归遍历 RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while(listFiles.hasNext()){ LocatedFileStatus file = listFiles.next(); Path path = file.getPath(); String name = path.getName(); System.out.println(name); } System.out.println("-------------------"); //可以列出文件和文件夹的信息,但是不提供自带的递归遍历 FileStatus[] listStatus = fs.listStatus(new Path("/")); for(FileStatus status:listStatus){ String name=status.getPath().getName(); System.out.println(name + (status.isDirectory()?"is dir":"is file")); } } /** * 创建文件夹 * @throws IOException * @throws IllegalArgumentException */ @Test public void mkdir() throws IllegalArgumentException, IOException{ boolean mkdirs = fs.mkdirs(new Path("/aaa/bbb/ccc")); } /** * 删除文件或文件夹 * @throws Exception * @throws IllegalArgumentException */ public void rm() throws IllegalArgumentException, Exception{ fs.delete(new Path("/aa"),true); } }
相关文章推荐
- 使用JAVA客户端对HDFS进行代码编写(五)
- HDFS的JAVA客户端编写
- HDFS的JAVA客户端编写(JAVA代码实现对HDFS的操作)
- 一脸懵逼学习hadoop之HDFS的java客户端编写
- Hadoop3-HDFS的java客户端编写
- HDFS的java客户端编写
- hdfs shell命令及java客户端编写
- 【Hadoop】HDFS的java客户端编写
- HDFS的java客户端编写
- 和我一起学Hadoop(四):java客户端操作HDFS
- Hadoop之HDFS客户端------java实现
- hdfs的java客户端
- delphi7编写客户端调用java服务器端webservice示例
- 使用wsdl2java简化客户端的编写
- Java网络编程学习笔记(1)用java编写客户端/服务器程序(简易)
- Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据
- [置顶] Unity3d搭建HTTP弱联网的服务器搭建及客户端编写(一)之java服务器
- JAVA AXIS2 WEBSERVICE JAVA客户端的编写
- Android(java)学习笔记205:网易新闻RSS客户端应用编写逻辑过程
- java编写服务_客户端通信实例讲解(适合初学者)