利用shell或java操作HDFS

本文详细介绍了HDFS的常见Shell操作,包括查看路径信息、上传和下载文件以及创建目录。同时,通过Java代码展示了如何设置HDFS配置并实现文件的上传与下载,解决运行时可能出现的环境变量异常问题。这些操作对于理解和使用HDFS非常实用。

一、HDFS常见的Shell操作

1、查看指定路径的信息

hdfs dfs -ls hdfs://bigdata02:9000/

可以简写成 hdfs dfs -ls /

2、从本地上传文件

可以采用put命令进行文件上传

3、可以采用get命令下载文件到本地

4、采用mkdir新建文件夹,要是递归目录,则在ls后面添加-R参数

二、通过java操作HDFS

本地idea运行时候,可能报异常:HADOOP_HOME and hadoop.home.dir are unset

1、使用java进行文件上传

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
        try {
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://192.168.221.131:9000");
            FileSystem fileSystem = FileSystem.get(conf);
            put(fileSystem);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //文件上传
    private static void put(FileSystem fileSystem) throws IOException{
        FSDataOutputStream fos = fileSystem.create(new Path("/user.txt"));
        FileInputStream fis = new FileInputStream("D:\\bigdata/user.txt");
        IOUtils.copyBytes(fis,fos,1024,true);
    }

运行结果:

2、使用java进行文件下载

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
        try {
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://192.168.221.131:9000");
            FileSystem fileSystem = FileSystem.get(conf);
            get(fileSystem);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //文件下载
    private static void get(FileSystem fileSystem) throws IOException{
        FSDataInputStream fis = fileSystem.open(new Path("/user.txt"));
        FileOutputStream fos = new FileOutputStream("D:\\bigdata/user1.txt");
        IOUtils.copyBytes(fis,fos,1024,true);
    }

运行结果:

三、实际过程中遇到的问题及解决方案

1.Java运行Hadoop发生异常:HADOOP_HOME and hadoop.home.dir are unset

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值