HDFS 常用shell 命令和 java api 操作文件上传下载删除

HDFS shell 常用命令

1、 查看命令的用法和参数

    hadoop fs -help   命令, 
    如查看rm 命令的用法 :  hadoop fs -help  rm

2、 显示目录

   hadoop fs -ls  /

3、 创建目录

  hadoop fs  -mkdir  -p   /test

4、上传

 hadoop  fs   -copyFromLocal    本地文件     hdfs路径
 hadoop  fs  -put   本地文件     hdfs路径
 都支持文件夹上传,并且保留本地文件

hadoop fs -moveFromLocal 本地文件 hdfs路径
上传文件或文件夹,但是本地不保留原始文件

5、下载

hadoop  fs   -copyToLocal    hdfs路径    本地路径

hadoop  fs   -get  hdfs路径    本地路径

hadoop  fs   -getmerge  hdfs路径    本地文件

6、移动

 hadoop  fs  -mv   src  dst

7、 复制

hadoop  fs  -cp   src  dst

8 、 删除

hadoop  fs -rm    文件
hadoop fs  -rm  -r   文件夹

9、查看大小

hadoop  fs -du -h  文件
可以查看文件或者文件夹的大小

Java Api 操作文件

1、maven 依赖

 <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoopclient -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs
       -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.9.2</version>
        </dependency>

    </dependencies>

    <!--maven打包插件 -->
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin </artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>

                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HadoopClientTest   {
    static Configuration conf ;
    static FileSystem fs ;

    public  static void   init() throws Exception{
        System.setProperty("hadoop.home.dir", "D:\\dev_soft\\hadoop-2.9.2");
        conf = new Configuration();
        //conf.set("dfs.replication","1");
        fs = FileSystem.get(new URI("hdfs://192.168.21.10:9000"),conf,"root");
    }
    public static void main(String[] args) throws  Exception{
        init();
       //mkdir();
       // upLoadFile();
       // downLoadFile();
        // delFile();
        listDir();

    }

    /**
     *  创建目录
     * */
    public static void  mkdir() throws URISyntaxException , IOException,InterruptedException {
        fs.mkdirs(new Path("/java_api_test2"));
        fs.close();
    }

    /**
     *  上传文件
     * */
    public  static  void  upLoadFile() throws Exception{
        //   只能上传文件
        Path srcPath = new Path("D:\\code\\hadoop\\src\\main\\java\\HadoopClientTest.java");
        Path dfsPath = new Path("/java_api_test2");
        fs.copyFromLocalFile(srcPath,dfsPath);
    }

    /**
     * 下载文件
     * */
    public static  void downLoadFile() throws Exception{
        Path hdfsSrc = new Path("/java_api_test/HadoopClientTest.java");
        Path localSrc = new Path("E:\\hdfs_test_dir");
        /**
         *  bool  是否删除原文件
         * */
        fs.copyToLocalFile(false,hdfsSrc,localSrc);
    }

   /**
   * 删除文件
   * */
    public static void  delFile()  throws Exception{
        fs.delete(new Path("/java_api_test"),true);
    }

    public static void listDir() throws  Exception{
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"),
                true);
        while(listFiles.hasNext()){
            LocatedFileStatus status = listFiles.next();
            // 输出详情
            // ⽂件名称
            System.out.println(status.getPath());
            // ⻓度
            System.out.println(status.getLen());
           // 5 ⽂件夹判断
            // 权限
            System.out.println(status.getPermission());
            // 分组
            System.out.println(status.getGroup());

            // 获取存储的块信息
            BlockLocation[] blockLocations = status.getBlockLocations();

            for (BlockLocation blockLocation : blockLocations) {

                // 获取块存储的主机节点
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
            System.out.println("-----------华丽的分割线----------");
        }
        // 3 关闭资源
        fs.close();

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值