hdfs基本操作

1. hdfs的操作(图形界面)

1.1 hdfs的启动流程
  1. 进入安全模式
  2. 加载fsimage
  3. 加载edits
  4. 保存检查点(融合fsimage和edits文件,生成新的fsimage)
  5. 退出安全模式
1.2 通过浏览器访问
http://namenode:50070

2. hdfs的操作(shell操作)

hdfs dfs
hadoop fs
hdfs dfs -put text01.txt /text01.txt   --上传文件
hdfs dfs -copyFromLocal text01.txt /test01
hdfs dfs -get /text02.txt /root/test01.txt  -- 下载文件
hdfs dfs -copyToLocal /test01 /test03


hdfs dfs -rm /text01.txt               -- 删除文件
hdfs dfs -rm -r /input/out             --删除目录
hdfs dfs -mkdir -p /input/out          --创建文件夹
hdfs dfs -ls /input                    --查看文件

hdfs dfs -mv /text02.txt /input --移动文件

hdfs dfs -appendToFile text01.txt  /input/text02.txt -本地文件追加到hdfs上

hdfs dfs -text /input/text02.txt  --查看远程文件

hdfs dfs -df  --查看文件系统

3. hdfs的操作(API操作)

3.1 依赖POM
<dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>2.6.4</version>
    </dependency>
3.2 hdfs读写文件
    import org.apache.commons.compress.utils.IOUtils;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FSDataOutputStream;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.junit.Test;
    
    
    public class TestHdfs {
    
        @Test
        public void testWrite() throws Exception {
            //创建配置对象
            Configuration con = new Configuration();
            con.set("fs.defaultFS","hdfs://mini:8020");
            //创建文件系统对象
            FileSystem fs = FileSystem.get(con);
           //文件路径
            Path path = new Path("/test02");
            //创建文件输出流
            FSDataOutputStream fsd = fs.create(path, true);
            //写文件
            fsd.write("hello".getBytes());
            //刷新关闭
            fsd.flush();
            fsd.close();
        }
        @Test
        public void testRead() throws Exception {
            //创建配置对象
            Configuration con = new Configuration();
            con.set("fs.defaultFS","hdfs://mini:8020");
            //创建文件系统对象
            FileSystem fs = FileSystem.get(con);
           //文件路径
            Path path = new Path("/test02");
            //创建文件输入流
            FSDataInputStream fsd = fs.open(path);
    
            IOUtils.copy(fsd,System.out);
        }
        @Test
        public void testUpload() throws Exception {
            //创建配置对象
            Configuration con = new Configuration();
            con.set("fs.defaultFS","hdfs://mini:8020");
            //创建文件系统对象
            FileSystem fs = FileSystem.get(con);
            //目标文件路径
            Path toPath = new Path("/t.txt");
            //源文件路径
            Path fromPath = new Path("d://t.txt");
            fs.copyFromLocalFile(false,fromPath,toPath);//fasle 表示保留源文件
        }
    
        @Test
        public void testDownload() throws Exception {
            //创建配置对象
            Configuration con = new Configuration();
            con.set("fs.defaultFS","hdfs://mini:8020");
            //创建文件系统对象
            FileSystem fs = FileSystem.get(con);
            //远程文件路径
            Path fromPath = new Path("/t.txt");
            //本地文件路径
            Path toPath = new Path("file:///e:/t.txt");
            fs.copyToLocalFile(false, fromPath, toPath,true);
        }
    
        //删除文件
        @Test
        public void testDelete() throws Exception {
            //创建配置对象
            Configuration con = new Configuration();
            con.set("fs.defaultFS","hdfs://mini:8020");
            //创建文件系统对象
            FileSystem fs = FileSystem.get(con);
            //远程文件路径
            Path fromPath = new Path("/t.txt");
           // public boolean delete(Path f, Boolean recursive)
        //       只有recursive=true时,一个非空目录及其内容才会被删除
            fs.delete(fromPath,false);
         }
         
          //block信息
        @Test
        public void testBlock() throws Exception {
            //创建配置对象
            Configuration con = new Configuration();
            con.set("fs.defaultFS","hdfs://mini:8020");
            //创建文件系统对象
            FileSystem fs = FileSystem.get(con);
            //文件路径
            Path fromPath = new Path("/test02");
            //获得block信息
            BlockLocation[] lo = fs.getFileBlockLocations(fromPath, 0, 20);
        }
    
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值