您的位置:首页 > 编程语言 > Java开发

HDFS的JAVA API客户端基本操作

2018-02-28 17:26 726 查看

一:需要的jar包:

[html] view
plain copy

<?xml version="1.0" encoding="UTF-8"?>  

<project xmlns="http://maven.apache.org/POM/4.0.0"  

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  

    <modelVersion>4.0.0</modelVersion>  

  

    <groupId>com.cl</groupId>  

    <artifactId>hadoop</artifactId>  

    <version>1.0-SNAPSHOT</version>  

  

    <url>http://maven.apache.org</url>  

  

    <properties>  

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  

    </properties>  

  

    <dependencies>  

        <dependency>  

            <groupId>junit</groupId>  

            <artifactId>junit</artifactId>  

            <version>4.11</version>  

            <scope>test</scope>  

        </dependency>  

        <!-- hadoop 分布式文件系统类库 -->  

        <dependency>  

            <groupId>org.apache.hadoop</groupId>  

            <artifactId>hadoop-hdfs</artifactId>  

            <version>2.8.3</version>  

        </dependency>  

        <!-- hadoop 公共类库 -->  

        <dependency>  

            <groupId>org.apache.hadoop</groupId>  

            <artifactId>hadoop-common</artifactId>  

            <version>2.8.3</version>  

        </dependency>  

  

    </dependencies>  

</project>  

二:连接HDFS和客户端

[java] view
plain copy

public class HdfsUtil {    

    public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {    

//      构造一个配置参数封装对象    

        Configuration conf = new Configuration();    

//      构造一个hdfs的客户端    

        FileSystem fs=FileSystem.get(new URI("hdfs://192.168.10.252:9000"), conf, "root");    

//      用hdfs文件系统的客户端对象fs来操作文件,比如上传一个文件    

        fs.copyFromLocalFile(new Path("/Users/cl/Downloads/docker"), new Path("/"));    

        fs.close();    

    }    

}    

三:Java客户端基本操作:

[java] view
plain copy

import org.apache.hadoop.conf.Configuration;  

import org.apache.hadoop.fs.*;  

import org.junit.Before;  

import org.junit.Test;  

  

import java.io.FileNotFoundException;  

import java.io.IOException;  

import java.net.URI;  

import java.net.URISyntaxException;  

  

  

public class HDFSUtil {  

    FileSystem fs = null;  

  

    @Before  

    public void init() throws IOException, InterruptedException, URISyntaxException {  

//     构造一个配置参数封装对象  

        Configuration conf = new Configuration();  

//      构造一个hdfs的客户端  

        fs = FileSystem.get(new URI("http://192.168.10.252:9000"), conf, "root");  

    }  

  

    /* 

     * 从本地上传文件到hdfs中 

     */  

    @Test  

    public void testUpload() throws IllegalArgumentException, IOException {  

        fs.copyFromLocalFile(new Path("/Users/cl/Downloads/docker"), new Path("/"));  

        fs.close();  

    }  

  

    /* 

     * 从hdfs中下载文件到本地 

     */  

    @Test  

    public void testDownload() throws IllegalArgumentException, IOException {  

        fs.copyToLocalFile(false, new Path("/docker"), new Path("/Users/cl/Downloads/"), true);  

        fs.close();  

    }  

  

    /* 

     * 文件夹操作 

     */  

    @Test  

    public void testDir() throws IllegalArgumentException, IOException {  

        fs.mkdirs(new Path("/aaa"));  

        System.out.println("创建了一个文件夹:/aaa");  

  

        boolean exists = fs.exists(new Path("/aaa"));  

        System.out.println("/aaa文件夹存在否?" + exists);  

  

        fs.copyFromLocalFile(new Path("/Users/cl/Downloads/input.txt"), new Path("/aaa"));  

        System.out.println("成功上传了一个文件到/aaa目录下");  

  

        fs.delete(new Path("/aaa"), true);  

        System.out.println("已经将/aaa目录删除");  

  

        boolean exists2 = fs.exists(new Path("/aaa"));  

        System.out.println("/aaa文件夹存在否?" + exists2);  

        fs.close();  

    }  

  

    /* 

     * 文件信息查看 

     */  

    @Test  

    public void testFileStatus() 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());  

        }  

  

        System.out.println("-----------------------");  

        //能列出文件和文件夹信息  

        FileStatus[] listStatus = fs.listStatus(new Path("/"));  

        for (FileStatus f : listStatus) {  

            String type = "-";  

            if (f.isDirectory()) type = "d";  

            System.out.println(type + "\t" + f.getPath().getName());  

        }  

        fs.close();  

    }  

  

    @Test  

    public void testOthers() throws IllegalArgumentException, IOException {  

        //文件偏移量信息  

        BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(new Path("/docker"), 0, 143588167);  

        for (BlockLocation location : fileBlockLocations) {  

            System.out.println(location.getOffset());  

            System.out.println(location.getNames()[0]);  

        }  

  

        //修改文件名  

        fs.rename(new Path("/docker"), new Path("/docker.tgz"));  

  

        //修改一个文件的副本数量  

        fs.setReplication(new Path("/docker.tgz"), (short) 2);  

        fs.close();  

    }  

}  

四:Java客户端IO流操作:

[java] view
plain copy

import java.io.FileInputStream;  

import java.io.FileOutputStream;  

import java.io.IOException;  

import java.net.URI;  

import java.net.URISyntaxException;  

  

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.apache.hadoop.io.IOUtils;  

import org.junit.Before;  

import org.junit.Test;  

  

  

public class HdfsIO {  

    FileSystem fs=null;  

  

    @Before  

    public void init() throws IOException, InterruptedException, URISyntaxException{  

//       构造一个配置参数封装对象  

        Configuration conf = new Configuration();  

//          构造一个hdfs的客户端  

        fs=FileSystem.get(new URI("hdfs://192.168.10.252:9000"), conf, "root");  

    }  

  

    /* 

     * 下载文件 

     */  

    @Test  

    public void testDownload() throws IllegalArgumentException, IOException{  

        FSDataInputStream in = fs.open(new Path("/docker"));  

        FileOutputStream out=new FileOutputStream("/Users/cl/Downloads/docker");  

        IOUtils.copyBytes(in,out,new Configuration());  

        IOUtils.closeStream(in);  

        IOUtils.closeStream(out);  

        fs.close();  

    }  

  

    /* 

     * 上传文件 

     */  

    @Test  

    public void testUpload() throws IllegalArgumentException, IOException{  

        FileInputStream in=new FileInputStream("/Users/cl/Downloads/docker");  

        FSDataOutputStream out = fs.create(new Path("/docker"));  

        IOUtils.copyBytes(in, out, new Configuration());  

        IOUtils.closeStream(in);  

        IOUtils.closeStream(out);  

        fs.close();  

    }  

  

    /* 

     * 从指定偏移量读取hdfs中的文件数据 

     * 在分布式数据处理时,可以将数据分片来分配给不同的节点处理 

     */  

    @Test  

    public void testSeek() throws IllegalArgumentException, IOException{  

        FSDataInputStream in = fs.open(new Path("/docker"));  

        in.seek(6);//定位,设置起始偏移量  

        FileOutputStream out=new FileOutputStream("/Users/cl/Downloads/docker");  

        IOUtils.copyBytes(in, out, new Configuration());  

        IOUtils.closeStream(in);  

        IOUtils.closeStream(out);  

        fs.close();  

    }  

}    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: