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
原创粉丝点击