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---------"+"\n");
        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函数所在的类

 

 

转载于:https://www.cnblogs.com/dyh004/p/7865629.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值