学习hadoop-读取hdfs上面的文件

获取hdfs上面的文件

首先进行远程链接  具体在  http://blog.csdn.net/qq_36020545/article/details/79062200 有介绍


首先在hadoop hdfs上面看看文件数据

bin/hdfs dfs -text /user/davidghs/mapreduce/wordcount/input/wc.input

进行远程连后获取到fileSystem对象

/**
 * 获取文件系统
 * @return fileSystem 文件系统
 * @throws Exception
 */
public static FileSystem getFileSystem() throws Exception{
    //默认加载 core-site.xml core-defult.xml hdfs-site.xml hdfs-defult.xml
    Configuration configuration = new Configuration();
    //获取文件系统
    FileSystem fileSystem = FileSystem.get(configuration);

    return fileSystem;
}

/**
 *  读取文件
 * @param fileName 文件路径
 * @throws Exception
 */
public static void readFile(String fileName) throws Exception {
    FileSystem fileSystem = getFileSystem();

    Path readPath = new Path(fileName);
    //文件流
    FSDataInputStream inputStream =  fileSystem.open(readPath);

    try{
        //读流
        IOUtils.copyBytes(inputStream,System.out,4096,false);

    }catch(Exception e){
        e.printStackTrace();
    }finally {
        //close steam 关闭资源
        IOUtils.closeStream(inputStream);

    }
}



public static void main(String[] args) throws Exception {
    String fileName = "/user/davidghs/mapreduce/wordcount/input/wc.input";
    //读文件路径
    readFile(fileName);
}




希望可以帮到你

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值