HDFS的客户端shell和java命令

hdfs

 hdfs  dfs  -ls 

 hdfs dfs -du -h

 hdfs dfs -du -h

5 hdfs dfs -rm -r 

6 hdfs dfs -put  

 hdfs dfs -get   HDFS的路径  本

java客户端maven需要的配置文件


    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-core</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-jobclient</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-common</artifactId>
        <version>2.8.5</version>
    </dependency>

地路径

 

创建文件夹

 @Auther: 多易教育-行哥
 * @Date: 2020/7/8
 * @Description:  使用java程序 操作HDFS文件系统
 * 1 获取代表这个文件系统的对象
 *    1)hdfs的位置 2) 配置对象 3)  用户名
 * 2 操作
 */
public class ClientDemo1 {
    public static void main(String[] args) throws Exception {
        // 指定hdfs的位置   alt+enter 抛出异常
        URI uri = new URI("hdfs://linux01:9000");
        // 用户的配置设置对象
        Configuration conf = new Configuration();
        // 当前操作客户端的用户名
        String username = "root" ;
        /**
         * 参数一  URI  指定HDFS文件系统的位置
         * 参数二 配置对象  用户的自定义设置  文件上传的副本个数
         * 参数三 用户名  root
         */
        FileSystem fs = FileSystem.newInstance(uri, conf, username);
        // 创建一个文件夹  new File("") ; new Path("") ;
        // Path对象是hdfs中对路径的抽象
        Path path = new Path("/doit16/hdp");
 
        // 创建层级文件夹
        boolean b = fs.mkdirs(path);
        if(b){
            System.out.println("创建成功");
        }else{
            System.out.println("创建失败");
        }
        // 释放资源
        fs.close();
    }

 

上传文件夹

 */
public class Upload2Hdfs {
    public static void main(String[] args) throws Exception {
        // 1 创建文件系统的对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:9000"), conf, "root");
        Path src = new Path("d://hadoop-2.8.1.zip"); // 本地路径  待上传的内容
        Path dis = new Path("/doit16/hdp/");// HDFS系统的路径
        /**
         * 参数一 本地路径  待上传的内容
         * 参数二 HDFS系统的路径
         */
      //  fs.copyFromLocalFile(src, dis); // 没有返回值
        /**
         * 参数一  是否删除上传的文件   默认 是false 不删除   true 删除(剪切)
         * 参数二  是否覆盖HDFS中已存在的内容  默认 true
         * 参数三  本地路径  待上传的内容
         * 参数四  HDFS系统的路径
         */
        fs.copyFromLocalFile(true , true ,src, dis);
        fs.close();
    }

 

下载


 * @Auther: 多易教育-行哥
 * @Date: 2020/7/8
 * @Description:
 *    配置本地hdp环境
 */
public class DownLoad {
 
    public static void main(String[] args) throws Exception {
        // 获取客户端对象
        FileSystem fs = DoitUtils.getFs();
        Path p1 = new Path("/doit16/hdp/hadoop-2.8.1.zip");
        Path p2 = new Path("d://");
        /**
         * 参数一  HDFS的路径
         * 参数二  本地路径
         */
       // fs.copyToLocalFile(p1, p2);
        /**
         * 参数一  是否删除HDFS中下载的文件
         * 参数二  HDFS的路径
         * 参数三  本地路径
         * 参数四  是否不生成校验文件  默认是false 生成
         */
        fs.copyToLocalFile(false, p1 , p2 , true);
        fs.close();
    }
}

 

删除操作

import com._51doit.utils.DoitUtils;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
 
/**
 * @Auther: 多易教育-行哥
 * @Date: 2020/7/8
 * @Description:  删除HDFS中的内容
 */
public class DeleteDemo {
    public static void main(String[] args) throws Exception {
        FileSystem fs = DoitUtils.getFs();
        /**
         * 参数一  要删除的内容文件  文件夹
         * 参数二 是否递归删除(强制)  true
         * 返回值 删除成功返回true
         */
        Path path = new Path("/doit16");
        if(fs.exists(path)){ // 判断内容是否存在
            boolean b = fs.delete(path, true);
            if(b){
                System.out.println("删除内容成功");
            }else{
                System.out.println("删除内容失败");
            }
        }else{
            System.out.println("删除的内容不存在");
        }
        fs.close();
    }

 

读取数据

import com._51doit.utils.DoitUtils;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
/**
 * @Auther: 多易教育-行哥
 * @Date: 2020/7/8
 * @Description:
 */
public class ReadData {
    public static void main(String[] args) throws Exception {
        FileSystem fs = DoitUtils.getFs();
        // 开启文件的输入流
        FSDataInputStream fin = fs.open(new Path("/app.txt"));
      //  fin.seek(1); 随机读取
        // 将输入流包装成缓冲字符流
        BufferedReader br = new BufferedReader(new InputStreamReader(fin));
        String line = null;
        long len = 0l ;
        // 读取所有的数据
        while ((line = br.readLine()) != null) {
            len += line.length()+1 ; //  记录读取数据的长度
            System.out.println(line);
        }
        // 释放资源
        fin.close();
        br.close();
        fs.close();
    }
}

7 写数据

HDFS分布式文件系统,存储的一般都是一些静态文件数据, 不太建议向文件中写数据, 不支持随机写数据 , 支持追加内容

一次存储 多次读取

 @Date: 2020/7/8
 * @Description:
 */
public class WriteData {
    public static void main(String[] args) throws Exception {
        FileSystem fs = DoitUtils.getFs();
        /**
         * 参数一  hdfs文件路径   自动创建
         * 参数二  是否覆盖已经存在的文件
         */
        FSDataOutputStream fout = fs.create(new Path("/app.txt"), true);
        fout.write("hello boys \n".getBytes());
        fout.write("hello girl \n".getBytes());
        fout.close();
        fs.close();
    }
 
    /**
     * 追加写
     * @param fs
     * @throws IOException
     */
    private static void appendData(FileSystem fs) throws IOException {
        // 追加内容   路径中的文件 一定是存在的
        FSDataOutputStream fout = fs.append(new Path("/app.txt"));
        // 追加写
        fout.write("\nhello tom jim cat \n".getBytes());
        fout.close();
    }
}

 

8 查看指定目录下的内容查看文件信息

@Date: 2020/7/8
 * @Description: 变量路径下所有的内容
 *   文件
 *    文件夹
 */
public class ListContents {
    public static void main(String[] args) throws Exception {
        FileSystem fs = DoitUtils.getFs();
        // 遍历文件夹下的内容 包括文件和文件夹
        FileStatus[] status = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : status) {
            Path path = fileStatus.getPath();
           //if(fs.isDirectory())  // 是否是文件夹
               if(fs.isFile(path)){// 是否是文件
                   // 获取元信息
               }else{
                   // 遍历一下
               }
        }
        fs.close();
 
    }
 
    private static void myListFile(FileSystem fs) throws IOException {
        // 遍历文件  递归遍历/ 路径下所有的文件
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
        while(files.hasNext()){
            LocatedFileStatus file = files.next();
            Path path = file.getPath();  // 路径
            String name = path.getName(); // 文件名
            long len = file.getLen(); // 大小
            long size = file.getBlockSize(); //  物理切块大小
            short number = file.getReplication();// 副本的个数 默认3
            /**
             * 获取文件的数据块位置
             *   数据物理切块的信息
             */
            BlockLocation[] blockLocations = file.getBlockLocations();
            // 遍历每个文件的物理块
            for (BlockLocation blockLocation : blockLocations) {
                long length = blockLocation.getLength(); // 数据物理切块的实际大小
                String[] hosts = blockLocation.getHosts();// 每个物理切块的数据三个副本
                long offset = blockLocation.getOffset(); // 数据块的起始偏移量
                for (String host : hosts) {
                    System.out.println(name+"--"+length+":"+offset+":"+host);
                }
            }
 
           // System.out.println(path+":"+name+":"+len+":"+size+":"+number);
        }
    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值