HDFS API基本操作

对HDFS API基本操作都是通过 org.apache.hadoop.fs.FileSystem类进行的,以下是一些常见的操作:

package HdfsAPI;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;

import junit.framework.TestCase;


public class HDFSAPI extends TestCase{  
    public static String hdfsUrl = "hdfs://master:8020";
    //创建文件夹
    @Test
    public void testHDFSmkdir() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
        Path path = new Path("/liguodong/hdfs");
        fs.mkdirs(path);
    }

    //创建文件
    @Test
    public void testHDFSmkFile() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
        Path path = new Path("/liguodong/hdfs/liguodong.txt");
        FSDataOutputStream fdos = fs.create(path); 
        fdos.write("hello hadoop".getBytes());
    }
    //重命名
    @Test
    public void testHDFSRenameFile() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
        Path path = new Path("/liguodong/hdfs/liguodong.txt");
        Path Renamepath = new Path("/liguodong/hdfs/love.txt");
        System.out.println(fs.rename(path, Renamepath));
    }
    //上传一个本地文件
    @Test
    public void testHDFSPutLocalFile1() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
        //因为在windows上面调试,所以选择的是windows的文件夹,
        //假设在Linxu,须要Linux文件夹。
        Path srcpath = new Path("g:/liguodong.txt");
        Path destpath = new Path("/liguodong/hdfs");
        fs.copyFromLocalFile(srcpath, destpath);
    }

    @Test
    public void testHDFSPutLocalFile2() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
        //因为在windows上面调试,所以选择的是windows的文件夹,
        //假设在Linxu,须要Linux文件夹。
        String srcpath ="g:/oncemore.txt";
        Path destpath = new Path("/liguodong/hdfs/kkk.txt");
        InputStream is = new BufferedInputStream(
                new FileInputStream(new File(srcpath)));
        FSDataOutputStream fdos = fs.create(destpath);
        IOUtils.copyBytes(is, fdos, 4094);
    }

    //查看某个文件夹以下的全部文件
    @Test
    public void testHDFSListFiles() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
        Path path = new Path("/liguodong/hdfs");
        FileStatus[] files = fs.listStatus(path);

        for (FileStatus file : files) {
            System.out.println(file.getPath().toString());
        }
    }

    //查看某个文件的数据块信息
    @Test
    public void testHDFSGetBlockInfo() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
        Path path = new Path("/liguodong/hdfs/kkk.txt");
        FileStatus filestatus = fs.getFileStatus(path);
        BlockLocation[] blkLoc = fs.getFileBlockLocations
                (filestatus, 0, filestatus.getLen());
        for (BlockLocation loc : blkLoc) {
            for (int i = 0; i < loc.getHosts().length; i++) {
                //获取数据块在哪些主机上
                System.out.println(loc.getHosts()[i]);//获取文件块的主机名
                //因为这个文件仅仅有一个块。所以输出结果为:slave2、slave1、slave5
            }
        }
    }   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值