利用shell或java操作HDFS

发布于:2023-01-16 ⋅ 阅读:(530) ⋅ 点赞:(0)

一、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

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到