HDFS的shell和API操作

发布时间:2020-07-04 13:53:59 作者:原生zzy
来源:网络 阅读:341

1. HDFS的shell操作

hadoop  version     //查看版本
hadoop   fs   -appendToFile  src(Linux中的文件) dest(hdfs目录下的文件)  //追加
hadoop   fs   -cat file(hdfs目录下的文件)   //查看文件内容
Hadoop   fs   -tail file(hdfs目录下的文件)   //查看文件末尾1kb的数据
hadoop   fs  -checksum  file(hdfs目录下的文件)  //校验当前文件是否正确
hadoop   fs  -copyFromLocal src dest  //从本地复制文件到HDFS
hadoop   fs  -copyToLocal   dest src  //从hdfs复制文件到本地
hadoop   fs  -count path    //对目录内容做计数
hadoop   fs  -find  path     //查看某个hdfs目录中是否相应的文件
hadoop   fs  -getmerge   src dest  //合并下载
hadoop   fs  -ls   path     //列表
hadoop   fs  -put  file(本地)  dest(hdfs)   //将本地文件上传到hdfs
hadoop   fs  -setrep  num  file   //设置hdfs系统中的某个文件的副本数(只能是hdfs中已上传的文件)
hadoop   fs  -setrep -R num file //设置hdfs系统中的目录下的所有文件的副本数
hadoop   fs  -text 压缩文件   //查看hdfs中的压缩文件
hadoop   fs  -truncate   dir  //清空hdfs目录
hdfs  getconf  -confkey  [key](配置文件的name)  //查看相应的配置的value

2. HDFS的API操作

1)eclipse中的hdfs的开发环境搭建:

2)添加hadoop依赖包的三种方式:

4)hdfs的实战编程:

public class HDFSApp {

    //文件系统
    FileSystem fileSystem = null;
    //配置类
    Configuration configuration = null;

    @Before
    public void setup() {
        configuration = new Configuration();
        configuration.set("fs.defautlFS", "hdfs://zzy:9000");
        configuration.addResource("core-site.xml");
        configuration.addResource("hdfs-site.xml");
        try {
            fileSystem = FileSystem.get(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //创建目录
    @Test
    public void mkdir() {
        try {
            System.setProperty("HADOOP_USER_NAME", "hadoop");
            boolean isMkdirs = fileSystem.mkdirs(new Path("/user/hadoop/test"));
            if (isMkdirs) {
                System.out.println("创建成功!!");
            } else {
                System.out.println("创建失败!!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //删除目录
    @Test
    public void deletedir() {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        try {
            fileSystem.delete(new Path("/每日任务.txt"), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //将本地文件copy到hdfs中
    @Test
    public void CopyFromeLocal() {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Path  src=new Path("C:\\Users\\aura-bd\\Desktop\\每日任务.txt");
        Path dest=new Path("/");
        try {
            fileSystem.copyFromLocalFile(src,dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //将hdfs文件copy到本地
    @Test public void CopyToLocal(){
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Path  src=new Path("C:\\Users\\aura-bd\\Desktop\\");
        Path dest=new Path("/user/hive/warehouse/test.db/pokes/data.txt");
        try {
            fileSystem.copyToLocalFile(dest,src);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //显示目录下的文件夹信息
    @Test
    public void FSListFile(){
        try {
            RemoteIterator<LocatedFileStatus> filelist = fileSystem.listFiles(new Path("/user/hive/warehouse/test.db"), true);
            while(filelist.hasNext()){
                LocatedFileStatus fileStatus = filelist.next();
                System.out.println(fileStatus.getPath());
                System.out.println(fileStatus.getGroup());
                System.out.println(fileStatus.getPath().getName());
                System.out.println(fileStatus.getReplication());

                BlockLocation[] blockLocations = fileStatus.getBlockLocations();
                for(BlockLocation block :blockLocations){
                    System.out.println(block.getHosts().toString());
                    System.out.println(block.getNames());
                    System.out.println(block.getOffset());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //显示文件夹以及文件信息
    @Test
    public void ListFiles(){
        try {
            FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
            for(FileStatus file:fileStatuses){
                if(file.isDirectory()){
                    System.out.println("directory:"+file.getPath().getName());
                }else{
                    System.out.println("file:"+file.getPath().getName());
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //下载文件
    @Test
    public void DownLoadFileToLocal(){
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        try {
            FSDataInputStream open = fileSystem.open(new
                    Path("/user/hive/warehouse/test.db/pokes/data.txt"));
            OutputStream out=new FileOutputStream(new File("D:\\data.txt"));
            IOUtils.copyBytes(open,out,1024);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //上传文件
    @Test
    public void upLoadFileToLocal(){
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        try {
            FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/a.txt"));
            InputStream in=new FileInputStream("D:\\data.txt");
            IOUtils.copyBytes(in,fsDataOutputStream,4096);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. HDFS的经典案例:

  在hadoop中MR编程计算框架中,都是计算向数据移动,那么如何获取一个文件的所有block信息,指定block块进行数据读取的呢?
代码实现

public class RamdomRead {
    private static Configuration conf;
    private static FileSystem fs;
    static {
        conf=new Configuration(true);
        conf.set("fs.defalutFS","hdfs://zzy:9000");
        conf.addResource("core-site.xml");
        conf.addResource("hdfs-site.xml");
        try {
            fs=FileSystem.get(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        System.setProperty("HADOOP_USER_NAME","hadoop");
        Path file=new Path("/data/2018-8-8/access.log");
        FSDataInputStream open = fs.open(file);
        //拿到文件信息
        try {
             FileStatus[] fileStatuses = fs.listStatus(file);
            // 获取这个文件的所有 block 的信息
            BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatuses[0], 0L, fileStatuses[0].getLen());
            // 第一个 block 的长度
            BlockLocation fileBlockLocation = fileBlockLocations[0];
            //第一个块的长度
            long length = fileBlockLocation.getLength();
            //第一个 block 的起始偏移量
            long offset = fileBlockLocation.getOffset();

            //读取数据
            byte flush[]=new byte[4096];
            FileOutputStream os = new FileOutputStream(new File("d:/block0"));
            while(open.read(offset,flush,0,4096)!=-1){
                os.write(flush);
                offset+=4096;
                if(offset>length){
                    return ;
                }
            }
            os.flush();
            os.close();
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
推荐阅读:
  1. HDFS的Java API操作(笔记)
  2. HDFS的Shell操作(笔记)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

hdfs的api he

上一篇:学习java技术有哪些优势?

下一篇:使用索引来排序查询结果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》