HDFS相关JAVA API

package com.bpf.hdfs;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
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.junit.Before;
import org.junit.Test;

public class HdfsClient {
    
    FileSystem fs = null;
    Configuration conf = null;
    
    //初始化,建立与HDFS系统的连接
    @Before
    public void init() throws Exception {
        conf = new Configuration();
        fs = FileSystem.get(new URI("hdfs://Master:9000"), conf);
    }
    
    //上传文件
    @Test
    public void testUpload() throws IllegalArgumentException, IOException {
        fs.copyFromLocalFile(new Path("E:/QQPCmgr/Desktop/c.docx"), new Path("/c_copy.dox"));     
    }
    
    //下载文件
    @Test
    public void testDownload() throws IllegalArgumentException, IOException {
        fs.copyToLocalFile(new Path("/c_copy.dox"), new Path("E:/QQPCmgr/Desktop/b.docx"));
    } 
    
    //获取并显示相关配置信息
    @Test
    public void testConf() {
        Iterator<Entry<String, String>> iterator = conf.iterator();
        while(iterator.hasNext()) {
            Entry<String, String> entry = iterator.next();
            System.out.println(entry.getKey()+" : "+entry.getValue());
        }
    }
    
    //创建文件夹
    @Test
    public void testMkdir() throws IllegalArgumentException, IOException {
        fs.mkdirs(new Path("/666/222"));
    }
    
    //删除文件夹
    @Test
    public void testDelete() throws IllegalArgumentException, IOException {
        fs.delete(new Path("/666/222"),true);
    }
    
    //显示HDFS内的相关文件存储信息
    @Test
    public void testLS1() throws FileNotFoundException, IllegalArgumentException, IOException {
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while(listFiles.hasNext()) {
            LocatedFileStatus fileStatus = listFiles.next();
            System.out.println(fileStatus.getPath().getName());
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            for (BlockLocation blockLocation : blockLocations) {
                System.out.println("块起始偏移量:" + blockLocation.getOffset());
                System.out.println("块长度:" + blockLocation.getLength());
                String[] hosts = blockLocation.getHosts();
                for (String string : hosts) {
                    System.out.println("datanode:" + string);
                }
            }
        }
    }
    
    //同上
    @Test
    public void testLS2() throws FileNotFoundException, IllegalArgumentException, IOException {
        FileStatus[] listStatus = fs.listStatus(new Path("/"));
        
        for (FileStatus fileStatus : listStatus) {
 
            System.out.println(fileStatus.toString());
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值