zoukankan      html  css  js  c++  java
  • hadoop-hdfs编程

    1、开发环境搭建

    一、新建一个普通的java工程

    二、引入hdfs相关的jar包

    需要引入的jar包:

    common下的jar

    hdfs下的jar

    2、编写HDFS相关的程序

    package com.cvicse.ump.hadoop.hdfs;
    
    import org.apache.hadoop.conf.Configuration;
    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 FileOperation {
        
        //创建文件
        public static void createFile(String dst,byte[] contents) throws Exception{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(conf);
            Path dstPath = new Path(dst);
            FSDataOutputStream outputStream = fs.create(dstPath);;
            outputStream.write(contents);
            outputStream.close();
            fs.close();
            System.out.println(dst+",文件创建成果");
        }
        
        //上传文件
        public static void uploadFile(String src,String dst) throws Exception{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(conf);
            Path srcPath = new Path(src);
            Path dstPath = new Path(dst);
            fs.copyFromLocalFile(srcPath, dstPath);
            System.out.println("Upload to "+conf.get("fs.default.name"));
            System.out.println("------list files---------"+"
    ");
            FileStatus[] fileStatus = fs.listStatus(dstPath);
            for(FileStatus file:fileStatus){
                System.out.println(file.getPath());
            }
            fs.close();
            
        }
        
        //删除目录
        public static void delete(String filePath)throws Exception{
            
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(conf);
            Path path = new Path(filePath);
            boolean isOk = fs.deleteOnExit(path);
            if(isOk){
                System.out.println("delete OK.");
            }else{
                System.out.println("delete failure.");
            }
            fs.close();
            
        }
        //创建目录
        public static void mkdir(String path)throws Exception{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(conf);
            Path srcPath = new Path(path);
            boolean isOK = fs.mkdirs(srcPath);
            if(isOK){
                System.out.println("create dir ok!");
            }else{
                System.out.println("create dir failure!");
            }
            fs.close();
        }
        
        //下载文件
        public static void downFile(String src,String dst)throws Exception{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(conf);
            Path srcPath = new Path(src);
            Path dstPath = new Path(dst);
            
            fs.copyToLocalFile(srcPath, dstPath);
            System.out.println("down load over");
            
        }
        
        public static void main(String[] args) throws Exception {
            /*String dst = args[0];
            byte[] contents = "hello,dyh".getBytes();
            createFile(dst, contents);*/
            
            /*String src = args[0];
            String dst = args[1];
            uploadFile(src, dst);*/
            
            /*String filePath = args[0];
            delete(filePath);*/
            
            /*String path = args[0];
            mkdir(path);*/
            
            String src = args[0];
            String dst = args[1];
            downFile(src, dst);
        }
    
    }

    导出jar包

    上传jar到HADOOP运行环境,并执行

    执行命令:hadoop jar jar包名字 main函数所在的类

  • 相关阅读:
    C++11——nullptr和NULL的区别
    C++11——原始字面量
    查看电脑内存是ddr3还是ddr4
    建立虚拟课堂需要考虑哪些因素?
    【解决方案】AI赋能智慧楼宇,如何实现多场景下的精细管理?
    H265网页播放器EasyPlayer.JS如何监听播放等相关事件回调?
    TSINGSEE青犀视频流媒体平台按需拉流和非按需拉流的区别及适用情况
    TSINGSEE青犀视频助力医疗废物处置可视化监管,筑牢口岸医疗废物管控防线
    TSINGSEE青犀视频行人智能检测测试报错panic: runtime error排查过程
    TSINGSEE青犀视频接入海康解码器SDK解码远程文件流程
  • 原文地址:https://www.cnblogs.com/dyh004/p/7865629.html
Copyright © 2011-2022 走看看