博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 操作 hdfs
阅读量:4157 次
发布时间:2019-05-26

本文共 4864 字,大约阅读时间需要 16 分钟。

package com.wisedu.hadoop.hdfs;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;import org.apache.hadoop.hdfs.DistributedFileSystem;import org.apache.hadoop.hdfs.protocol.DatanodeInfo;import java.io.FileNotFoundException;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;/** * Created by heavy_li on 2016/11/19. */public class HdfsFileDemo {     private Configuration conf ;     private FileSystem fileSystem;     public HdfsFileDemo(){         //加载配置文件,默认会加载CLASSPATH下的core-site.xml         conf = new Configuration();         //也可以自己手动加载配置文件  如下         //conf.addResource("core-site.xml");         log(conf.get("fs.defaultFS"));         try {             //用超级管理员账号 不会存在权限问题             fileSystem = FileSystem.get(new URI(conf.get("fs.defaultFS")),conf,conf.get("hadoop.loginName"));         } catch (IOException e) {             e.printStackTrace();         } catch (InterruptedException e) {             e.printStackTrace();         } catch (URISyntaxException e) {             e.printStackTrace();         }     }    /**     * 显示目录下的所有文件 对应ls命令     * @param fileUri     */    public void showDir(String fileUri,PathFilter pathFilter){        try {            FileStatus[] fileStatuses = null;            if(pathFilter==null){                fileStatuses = fileSystem.listStatus(new Path(fileUri));            }else{                fileStatuses = fileSystem.listStatus(new Path(fileUri),pathFilter);            }            if(fileStatuses!=null&&fileStatuses.length!=0){                for(FileStatus fs : fileStatuses){                    log(fs.getOwner().concat("  ").concat(fs.getGroup()).concat("  ").concat(fs.getPath().toString()).concat("  ").concat(fs.getLen()+"  "));                }            }else{                log("该目录下没有任何文件!");            }        } catch (IOException e) {            if(e instanceof FileNotFoundException){                log("目录不存在!");            }else {                e.printStackTrace();            }        }    }    /**     * 读取文件,对应 cat命令  get命令 可以将字节从新生成文件     * @param fileUri     */    public void readFile(String fileUri){        Path path = new Path(fileUri);        try {            if(fileSystem.exists(path)){                FSDataInputStream is = fileSystem.open(path);                FileStatus status = fileSystem.getFileStatus(path);                byte[] buffer = new byte[Integer.parseInt(String.valueOf(status.getLen()))];                is.readFully(0, buffer);                is.close();                log(new String(buffer));            }else{                log("该文件不存在!");            }        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 创建目录,可以创建多级目录  对应命令mkdir     * @param uri     */    public void mkdir(String uri){        Path path = new Path(uri);        try {            fileSystem.create(path);            log(uri.concat(" created!"));        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 删除目录或者文件 对应 rm -r 命令     * @param uri     */    public void deleteDirOrFile(String uri){        Path path = new Path(uri);        try {            //  true 相当于rm -r            fileSystem.delete(path,true);            log(uri.concat(" deleted!"));        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 将本地文件上传到hdfs 对应命令: copyFromLocal     * @param localPath     * @param remoteUri  目录不存在的话会自动创建     */    public void copyFromLocal(String localPath,String remoteUri){        Path src = new Path(localPath);        Path dst = new Path(remoteUri);        try {            fileSystem.copyFromLocalFile(src, dst);            log("over!");        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 以字节形式写入hdfs系统     * @param data     * @param uri     */    public void writeFile(byte[] data,String uri){        Path path = new Path(uri);        try {            FSDataOutputStream out = fileSystem.create(path);            out.write(data);            log("over!");        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 直接写入字符     * @param s     * @param uri     */    public void writeFileStr(String s ,String uri){        Path path = new Path(uri);        try {            FSDataOutputStream out = fileSystem.create(path);            out.writeUTF(s);            log("over!");        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 查找文件块在哪个服务器上     * @param uri     */    public void getFileBlockLoaction(String uri){        Path path = new Path(uri);        FileStatus status = null;        try {            status = fileSystem.getFileStatus(path);            BlockLocation[] locations = fileSystem.getFileBlockLocations(status, 0, status.getLen());            int length = locations.length;            for(int i=0;i

转载地址:http://zdyxi.baihongyu.com/

你可能感兴趣的文章
Linux initial RAM disk (initrd) overview
查看>>
Timestamping Linux kernel printk output in dmesg for fun and profit
查看>>
There's Much More than Intel/AMD Inside
查看>>
CentOS7 安装MySQL 5.6.43
查看>>
使用Java 导入/导出 Excel ----Jakarta POI
查看>>
本地tomcat 服务器内存不足
查看>>
IntelliJ IDAE 2018.2 汉化
查看>>
Openwrt源码下载与编译
查看>>
我和ip_conntrack不得不说的一些事
查看>>
Linux 查看端口使用情况
查看>>
文件隐藏
查看>>
两个linux内核rootkit--之二:adore-ng
查看>>
两个linux内核rootkit--之一:enyelkm
查看>>
关于linux栈的一个深层次的问题
查看>>
rootkit related
查看>>
配置文件的重要性------轻化操作
查看>>
又是缓存惹的祸!!!
查看>>
为什么要实现程序指令和程序数据的分离?
查看>>
我对C++ string和length方法的一个长期误解------从protobuf序列化说起(没处理好会引起数据丢失、反序列化失败哦!)
查看>>
一起来看看protobuf中容易引起bug的一个细节
查看>>