Hdfs 的读写操作
来源:互联网 发布:鹏鹏扣字软件谁? 编辑:程序博客网 时间:2024/06/05 15:15
<span style="font-size:32px;color:#ff0000;">说明:本测试使用maven管理项目结构,测试前,请把 core-site.xml 拷贝到resource目录</span>
<span style="font-size:18px;">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());}</span>
<span style="font-size:18px;"><span style="white-space:pre"></span>// 内容追加</span>
<span style="font-size:18px;">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();}}</span>
0 0
- Hdfs 的读写操作
- HDFS文件的读写操作理论解析
- Hadoop的HDFS Java pai 读写操作
- HDFS之SequenceFile的读写操作
- HDFS中文件的读写操作
- HDFS读写文件操作
- hadoop java HDFS 读写操作
- HDFS的读写流程
- hdfs读写数据操作分析总结
- hdfs——hadoop文件读写操作
- HDFS的读写限流方案
- HDFS基本的读写文件
- HDFS数据的读写过程
- HDFS读写文件的过程
- HDFS的读写限流方案
- 调用JAVA API对HDFS文件进行文件的读写、上传下载、删除等操作代码详解
- HDFS的JavaAPI操作
- HDFS的文件操作
- 那些走在身后的时间
- 强大的SpannableStringBuilder,玩转文本特效
- spark基本知识点之Shuffle
- 根据wsdl文件或者wsdl地址生成对应的java代码(借助MyEclipse工具快速生成Axis1的客户端代码)
- R语言各种查询
- Hdfs 的读写操作
- 基于开源jabber(XMPP)架设内部即时通讯服务的解决方案
- 关于Avoid non-default constructors in fragments的错误
- Android 设计模式 之 单例模式
- LeetCode 415. Add Strings 题解(C++)
- linux IPC--信号量
- js如何获取对象中的属性名
- 反编译相关记录
- Solr开发文档