Hadoop生态之HDFS客户端操作(三)

操作HDFS常用的两种客户端:

1.通过自带的shell方式进行操作

2.借助Hadoop提供的Java API进行操作

一、hadoop shell常用基本操作命令:

1> 查看HDFS指定的目录

命令:hdfs dfs -ls -R /input

2> 创建指定目录

命令:hdfs dfs -mkdir /output

3> 创建级联目录

命令:hdfs dfs -mkdir -p /output/data

4> 上传HDFS集群服务器文件

命令:hdfs dfs -put test.txt /input

5> 打开HDFS服务器文件

命令:hdfs dfs -cat /input/test.txt

6> 打开HDFS服务器文件

命令:hdfs dfs -tail /input/test.txt

7> 删除HDFS服务器文件

命令:hdfs dfs -rm /input/test.txt

8> 删除HDFS服务器文件夹目录

命令:hdfs dfs -rm -r /input

9> 下载文件到本地

命令:hdfs dfs -get /input/test.txt

10> HDFS文件目录授权

命令:hdfs dfs -chmod 777 /input

二、通过Hadoop Java API客户端操作HDFS文件:

1> 创建普通java项目

2> 导入需要的hadoop的jar包,直接复制hadoop/share/目录下面的所有jar添加到自己创建的java项目中并进行build path。

3> 创建测试类。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.fs.permission.FsAction;
import org.apache.hadoop.fs.permission.FsPermission;
import org.junit.Before;
import org.junit.Test;

public class HDFSDemo {

    FileSystem client = null;

    /**
     * 初始化连接hadoop的HDFS服务器
     */
    @Before
    public void init() {
        // 初始化配置类
        Configuration conf = new Configuration();
        try {
            // 初始化hadoop客户端
            client = FileSystem.get(new URI("hdfs://192.168.248.100:9000"), conf, "root");
            if (client != null) {
                System.out.println("连接hadoop hdfs集群成功!" + client);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从HDFS集群下载文件到本地目录
     */
    @Test
    public void downFile() {
        System.out.println("获取文件");
        try {
            client.copyToLocalFile(false, new Path("/input/test.txt"), new Path("E:\\"), true);
            client.close();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从本地上传文件到HDFS服务器
     */
    @Test
    public void uploadFile() {
        try {
            client.copyFromLocalFile(new Path("E://hello.txt"), new Path("/input"));
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除HDFS集群目录或文件 true表示递归删除目录 (path里可以是路径,也可以是文件)
     * 
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void deleteDir() throws IllegalArgumentException, IOException {
        client.delete(new Path("/input"), true);
        client.close();
    }

    /**
     * 创建目录,可以多级目录
     */
    @Test
    public void createDirHDFS() throws IllegalArgumentException, IOException {
//        client.mkdirs(new Path("/hadoopIn"), new FsPermission((short) 777));
//        client.close();
        FsPermission filePermission = null;
        // 当前用户权限,所在组用户权限,其它组用户权限
        filePermission = new FsPermission(FsAction.ALL, FsAction.EXECUTE, FsAction.READ);
        client.mkdirs(new Path("/input1"), filePermission);
        client.close();

    }

    /**
     * 对目录或者文件重命名 (源文件,目的)
     * 
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void renameDirName() throws IllegalArgumentException, IOException {
        client.rename(new Path("/input/hello.txt"), new Path("/input/hdfs.txt"));
        client.close();
    }

    /**
     * 读取文件元信息
     * 
     * @throws FileNotFoundException
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void readDir() throws FileNotFoundException, IllegalArgumentException, IOException {
        RemoteIterator<LocatedFileStatus> files = client.listFiles(new Path("/input"), true);
        while (files.hasNext()) {
            LocatedFileStatus file = files.next();
            System.out.println("最近访问时间:" + file.getAccessTime());
            System.out.println("块大小:" + file.getBlockSize());
            System.out.println("文件大小:" + file.getLen());
            System.out.println("文件所有者:" + file.getOwner());
            System.out.println("文件副本数:" + file.getReplication());
            System.out.println("文件全路径:" + file.getPath());
            System.out.println("---本地块信息---:" + Arrays.toString(file.getBlockLocations()));
        }
        client.close();
    }

    /**
     *  查找指定路径下的所有文件夹(当前目录下的一级目录或者文件,不遍历子目录)
     * @throws FileNotFoundException
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void readDir2() throws FileNotFoundException, IllegalArgumentException, IOException {
        FileStatus[] listStatus = client.listStatus(new Path("/"));
        for (FileStatus file : listStatus) {
            System.out.println(file.getPath());
            System.out.println(file.isDirectory() == true ? "File" : "Dir" + "----");
        }
        client.close();

    }

    /**
     *  读取文件中的部分内容( 从文件开始位置读取,读取20个字节)
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void readFilePart() throws IllegalArgumentException, IOException {
        FSDataInputStream in = client.open(new Path("/hadoop-pro/input/part-m-00000"));
        FileOutputStream out = new FileOutputStream("d:/test.txt");
        byte[] b = new byte[20];
        int len = 0;
        long count = 0;
        while ((len = in.read(b)) != -1) {
            out.write(b);
            count += len;
            if (count >= 20)
                break;
        }
        out.flush();
        out.close();
        in.close();
        client.close();

    }
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ansap

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值