HDFS文件接口

命令基本格式:

hadoop fs -cmd < args >


  1. ls

hadoop fs -ls /

列出hdfs文件系统根目录下的目录和文件

hadoop fs -ls -R /

列出hdfs文件系统所有的目录和文件


  1. put

hadoop fs -put < local file > < hdfs file >

hdfs file的父目录一定要存在,否则命令不会执行

hadoop fs -put < local file or dir >...< hdfs dir >

hdfs dir 一定要存在,否则命令不会执行

hadoop fs -put - < hdsf file>

从键盘读取输入到hdfs file中,按Ctrl+D结束输入,hdfs file不能存在,否则命令不会执行

  1. moveFromLocal

hadoop fs -moveFromLocal < local src > ... < hdfs dst >

与put相类似,命令执行后源文件 local src 被删除,也可以从从键盘读取输入到hdfs file中

  1. copyFromLocal

hadoop fs -copyFromLocal < local src > ... < hdfs dst >

与put相类似,也可以从从键盘读取输入到hdfs file中


  1. get

hadoop fs -get < hdfs file > < local file or dir>

local file不能和 hdfs file名字不能相同,否则会提示文件已存在,没有重名的文件会复制到本地

hadoop fs -get < hdfs file or dir > ... < local dir >

拷贝多个文件或目录到本地时,本地要为文件夹路径

注意:如果用户不是root, local 路径要为用户文件夹下的路径,否则会出现权限问题,

  1. copyToLocal

hadoop fs -copyToLocal < local src > ... < hdfs dst >

与get相类似


  1. rm

hadoop fs -rm < hdfs file > ...

hadoop fs -rm -r < hdfs dir>...

每次可以删除多个文件或目录


  1. mkdir

hadoop fs -mkdir < hdfs path>

只能一级一级的建目录,父目录不存在的话使用这个命令会报错

hadoop fs -mkdir -p < hdfs path>

所创建的目录如果父目录不存在就创建该父目录


  1. getmerge

hadoop fs -getmerge < hdfs dir > < local file >

将hdfs指定目录下所有文件排序后合并到local指定的文件中,文件不存在时会自动创建,文件存在时会覆盖里面的内容

hadoop fs -getmerge -nl < hdfs dir > < local file >

加上nl后,合并到local file中的hdfs文件之间会空出一行


  1. cp

hadoop fs -cp < hdfs file > < hdfs file >

目标文件不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件还存在

hadoop fs -cp < hdfs file or dir >... < hdfs dir >

目标文件夹要存在,否则命令不能执行


  1. mv

hadoop fs -mv < hdfs file > < hdfs file >

目标文件不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件不存在

hadoop fs -mv < hdfs file or dir >... < hdfs dir >

源路径有多个时,目标路径必须为目录,且必须存在。

注意:跨文件系统的移动(local到hdfs或者反过来)都是不允许的


  1. count

hadoop fs -count < hdfs path >

统计hdfs对应路径下的目录个数,文件个数,文件总计大小

显示为目录个数,文件个数,文件总计大小,输入路径

HDFS JavaAPI

package Hdfs;

import com.sun.xml.internal.ws.api.ha.StickyFeature;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.hdfs.util.IOUtilsClient;
import org.apache.hadoop.io.IOUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HdfsTest {

    private static Configuration conf = new Configuration();

    static   {
        //部署在远程服务器上的集群
        conf.set("fs.defaultFS", "172.18.74.236:9000");

    }


    //创建新文件
    public static void createFile(String dst, byte[] contents) throws IOException
    {
        FileSystem fs = FileSystem.get(conf);
        Path dstPath = new Path(dst);//目标路径
        //打开一个输出流
        FSDataOutputStream outputStream = fs.create(dstPath);
        outputStream.write(contents);
        outputStream.close();
        fs.close();
        System.out.println("文件创建成功");
    }

    //将本地文件导入到Hdfs中
    public static void uploadFile(String src,String dst) throws IOException{
        //Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        Path srcPath = new Path(src); //本地上传文件路径
        Path dstPath = new Path(dst); //hdfs目标路径
        //调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
        fs.copyFromLocalFile(false, srcPath, dstPath);

        //打印文件路径
        System.out.println("Upload to "+conf.get("fs.default.name"));
        System.out.println("------------list files------------"+"\n");
        FileStatus [] fileStatus = fs.listStatus(dstPath);
        for (FileStatus file : fileStatus)
        {
            System.out.println(file.getPath());
        }
        fs.close();
    }

    public static void upload(String src,String dst) throws IOException{
        FileSystem fs = FileSystem.get(conf);

        Path dstPath = new Path(dst); //hdfs目标路径
        FSDataOutputStream os = fs.create(dstPath);
        FileInputStream is = new FileInputStream(src);

        org.apache.commons.io.IOUtils.copy(is,os);
    }

    //文件重命名
    public static void rename(String oldName, String newName) throws IOException{
        FileSystem fs = FileSystem.get(conf);

        Path oldPath = new Path(oldName);
        Path newPath = new Path(newName);
        boolean isok = fs.rename(oldPath, newPath);
        if(isok){
            System.out.println("rename ok!");

        }
        else {
            System.out.println("rename failure");
        }

        fs.close();
    }

    //删除文件
    public static void delete(String filePath) throws IOException{
        FileSystem fs = FileSystem.get(conf);

        Path path = new Path(filePath);
        boolean isok = fs.deleteOnExit(path);
        if(isok){
            System.out.println("delect ok");
        }
        else {

            System.out.println("delect failure");
        }

        fs.close();
    }

    //创建目录
    public static void mkdir(String path) throws IOException{

        FileSystem fs = FileSystem.get(conf);

        Path srcPath = new Path(path);
        boolean isok = fs.mkdirs(srcPath);
        if (isok){
            System.out.println("create " + path + " dir ok !");

        }
        else{
            System.out.println("create "+ path +" dir failure!");
        }
        fs.close();
    }

    //读取文件中的内容
    public static void readFile(String filePath) throws IOException{
        FileSystem fs = FileSystem.get(conf);

        Path fielPath = new Path(filePath);
        InputStream in =null;
        try{
            in =fs.open(fielPath);
            IOUtils.copyBytes(in, System.out, 4096, false);
        }
        finally {
            IOUtils.closeStream(in);
        }
    }

    /**
     * 遍历制定目录下的所有文件
     */
    public static void getDiretoryFromHdfs(String direPath){


        try {
            FileSystem fs = FileSystem.get(conf);
            FileStatus[] filelist = fs.listStatus(new Path(direPath));
            for (int i = 0; i < filelist.length; i++){
                System.out.println("______" + direPath + "目录下的所有文件_________");
                FileStatus fileStatus = filelist[i];
                System.out.println("Name: "+fileStatus.getPath().getName());
                System.out.println("Size: "+fileStatus.getLen());
                System.out.println("Path: "+ fileStatus.getPath());

            }
            fs.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  
    public static void main(String[] args) throws IOException{

        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        String localFilePath = "C:\\Users\\Charon\\Desktop\\To do.txt";
        String hdfsFilePath = "/Test" +today.substring(0,7) + "/upload_date=" + today + "/";


        //1. 遍历根目录下的所有文件
        getDiretoryFromHdfs("/");

        //2. 新建目录
//        mkdir(hdfsFilePath);

        //3. 上传文件
//        uploadFile(localFilePath,hdfsFilePath);
//        getDiretoryFromHdfs(hdfsFilePath);

        //4. 读取文件
//        readFile("hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/To do.txt");

        //5. 重命名
//        rename("hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/To do.txt","hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/Test.txt");

        //6. 创建文件,并向文件写入内容
//        byte[] contents = "\n2019年5月26日20:22:37 添加写入内容\n".getBytes();
//        createFile("hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/Test1.txt",contents);
//        readFile("hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/Test1.txt");

        //7. 删除文件
        delect(hdfsFilePath);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值