用java实现Hadoop中HDFS的函数调用

来源:互联网 发布:火星15洲际导弹 知乎 编辑:程序博客网 时间:2024/06/03 21:02

以下是使用JAVA实现的HDFS的指令调用。


主方法如下:

import java.net.URI;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;public class hdfs {    public static void main(String[] args) throws Exception {        // 创建一个BigDataOper 对象        BigDataOper oper = new BigDataOper();        // 调用下载方法        oper.upload();        oper.download();        // 测试目录方法        oper.testDir();        // 获得文件的相关信息        oper.TestFileStatus();        // 获得文件的块信息        oper.testOthers();        // 关闭对象流        oper.close();    }}// 内部类class BigDataOper {    // 文件系统的操作对象    private FileSystem fs = null;    // 对操作对象进行初始值设置    public BigDataOper() throws Exception {        fs = FileSystem.get(new URI("hdfs://192.168.100.144:9000"), new Configuration(), "root");    }    // 关闭文件系统的连接    public void close() throws Exception {        fs.close();    }    // 上传测试方法    public void upload() throws Exception {        fs.copyFromLocalFile(new Path("/home/bigdata/abc.txt"), new Path("/"));        //给予上传路径和存放路径(绝对路径)        System.out.println("upload is sucessful!");    }    // 下载测试方法    public void download() throws Exception {        fs.copyToLocalFile(new Path("/abc.txt"), new Path("/home/bigdata"));        //给予下载路径和存放路径(绝对路径)        System.out.println("download is sucessful!");    }    // 其他方法测试    public void testDir() throws Exception {        // 创建一个目录        fs.mkdirs(new Path("/newDir"));        // 判断文件或目录是否存在        boolean bool = fs.exists(new Path("/newDir"));        System.out.println("file is " + bool);        // 删除目录        fs.delete(new Path("/newDir2"), true);        boolean bool2 = fs.exists(new Path("/newDir"));        System.out.println("newDir2 exist is " + bool2);    }    // 获得块存储信息    public void testOthers() throws Exception {        BlockLocation[] blk = fs.getFileBlockLocations(new Path("/eclv.tar.gz"), 0, 100000000);        // 遍历块的信息        for (int i = 0; i < blk.length; i++) {            System.out.println(blk[i].getOffset());        }        fs.setReplication(new Path("/eclv.tar.gz"), (short) 2);//需要强转类型    }    // 查看文件状态    public void TestFileStatus() throws Exception {        FileStatus[] list = fs.listStatus(new Path("/"));        // 遍历集合        for (int i = 0; i < list.length; i++) {            // 文件类型判断            String type = "-";            if (list[i].isDirectory()) {                type = "d";            }            System.out.println(type + "\t" + list[i].getPath().getName());        }        // 文件状态迭代        RemoteIterator<LocatedFileStatus> localfile = fs.listFiles(new Path("/"), true);        while (localfile.hasNext()) {            LocatedFileStatus local = localfile.next();            // 获得文件的名字            System.out.println(local.getPath().getName());        }    }}
1 0