HDFS基本操作的API

一、从hdfs下载文件到windows本地:

package com.css.hdfs01;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/**
 * 从hdfs下载文件到windows本地
 *    
 * 注意:
 * 1.需要配置hadoop环境变量
 * 2.需要编译好的winutils包
 */
public class HdfsClientDemo02 {
    public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
        // 1.加载配置
        Configuration conf = new Configuration();
        // 2.设置副本数
        conf.set("dfs.replication", "2");
        // 3.设置块大小
        conf.set("dfs.blocksize", "64m");
        // 4.构造客户端
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.146.132:9000"), conf, "root");
        // 5.hdfs数据下载到windows本地
        fs.copyToLocalFile(new Path("/hdfs-site.xml"), new Path("c:/"));
        // 6.关闭资源
        fs.close();
    }
}

二、hdfs常用的API:

package com.css.hdfs02;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import org.apache.hadoop.conf.Configuration;
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;

/**
 * hdfs常用的API
 */
public class HdfsClientTest {
    
    FileSystem fs =null;
    
    @Before
    public  void init() throws IOException, InterruptedException, URISyntaxException {
        // 1.加载配置
        Configuration conf = new Configuration();
        // 2.设置副本数
        conf.set("dfs.replication", "2");
        // 3.设置块大小
        conf.set("dfs.blocksize", "64m");
        // 4.构造客户端
        fs = FileSystem.get(new URI("hdfs://192.168.146.132:9000/"), conf, "root");
    }
    
    /**
     * 在hdfs中创建文件夹
     * hdfs dfs -mkdir /文件夹名
     */
    @Test
    public void hdfsMkdir() throws IllegalArgumentException, IOException{
        // 1.调用创建文件夹方法
        fs.mkdirs(new Path("/hello"));
        // 2.关闭资源
        fs.close();
    }
    
    /**
     * 在hdfs中 移动/修改文件
     * hdfs dfs -mv /hdfs路径 /hdfs路径
     * hdfs dfs -cp /hdfs路径 /hdfs路径        
     */
    @Test
    public void hdfsRename() throws IllegalArgumentException, IOException{
        // 1.调用移动修改文件方法
        fs.rename(new Path("/aa.txt"), new Path("/hello/aa.txt"));
        // 2.关闭资源
        fs.close();
    }
    
    /**
     * 在hdfs中 删除文件/文件夹
     * hdfs dfs -rm /文件名
     * hdfs dfs -rm -r /文件夹名
     */
    @Test
    public void hdfsRm() throws IllegalArgumentException, IOException{
        // 1.调用删除文件方法
        // 下面的一个参数的方法已弃用
        // fs.delete(new Path("/aaaa.txt"));
        // 参数1:要删除的路径  参数2:是否递归删除
        fs.delete(new Path("/aaa111.txt"), true);
        // 2.关闭资源
        fs.close();
    }
    
    /**
     * 查询hdfs下指定的目录信息
     */
    @Test
    public void hdfsLs() throws IllegalArgumentException, IOException{
        // 1.调用方法,返回远程迭代器    
        RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path("/"), true);
        // 2.取迭代器数据
        while (iter.hasNext()) {
            // 拿数据
            LocatedFileStatus status = iter.next();
            System.out.println("文件的路径为:" + status.getPath());
            System.out.println("块大小为:" + status.getBlockSize());
            System.out.println("文件长度为:" + status.getLen());
            System.out.println("副本数量为:" + status.getReplication());
            System.out.println("块信息为:" + Arrays.toString(status.getBlockLocations()));
            System.out.println("===============================");
        }
        // 3.关闭资源
        fs.close();
    }
    
    /**
     * 判断文件还是文件夹
     */
    @Test
    public void hdfsFile() throws IllegalArgumentException, IOException{
        // 1.展示状态信息
        FileStatus[] listStatus = fs.listStatus(new Path("/"));
        // 2.遍历所有文件
        for(FileStatus ls:listStatus){
            if (ls.isFile()) {
                // 文件
                System.out.println("文件-----f-----" + ls.getPath().getName());
            }else {
                // 文件夹
                System.out.println("文件夹-----d-----" + ls.getPath().getName());
            }
        }
    }    
}

三、hdfs读写文件:

package com.css.hdfs03;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
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;

/**
 * hdfs读写文件
 */
public class ReadData {
    
    FileSystem fs =null;
    @Before
    public  void init() throws IOException, InterruptedException, URISyntaxException {
        // 1.加载配置
        Configuration conf = new Configuration();
        // 2.构造客户端
        fs = FileSystem.get(new URI("hdfs://192.168.146.132:9000/"), conf, "root");
    }
    
    /**
     * 读数据方式一
     */
    @Test
    public void testReadData1() throws IllegalArgumentException, IOException{
        // 1.拿到流
        FSDataInputStream in = fs.open(new Path("/a.txt"));
        byte[] buf = new byte[1024];
        in.read(buf);
        System.out.println(new String(buf));
        // 2.关闭资源
        in.close();
        fs.close();
    }
    
    /**
     * 读数据方式二
     */
    @Test
    public void testReadData2() throws IllegalArgumentException, IOException{
        // 1.拿到流
        FSDataInputStream in = fs.open(new Path("/hdfs-site.xml"));
        // 2.缓冲流
        BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        // 3.按行读取
        String line = null;
        // 4.读数据
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        // 5.关闭资源
        br.close();
        in.close();
        fs.close();
    }
    
    /**
     * 读取hdfs中指定偏移量
     */
    @Test
    public void testRandomRead() throws IllegalArgumentException, IOException{
        // 1.拿到流
        FSDataInputStream in = fs.open(new Path("/hdfs-site.xml"));
        // 2.移动文件读取指针到指定位置
        in.seek(14);
        byte[] b = new byte[5];
        // 3.从指针位置开始读取数组b的长度个字节
        in.read(b);
        System.out.println(new String(b));
        // 4.关闭资源
        in.close();
    }
    
    /**
     * 在hdfs中写数据方式一
     */
    @Test
    public void testWriteData() throws IllegalArgumentException, IOException{
        // 1.输出流
        FSDataOutputStream out = fs.create(new Path("/windows.txt"), false);
        // 2.输入流
        FileInputStream in = new FileInputStream("C:\Users\Administrator\Desktop\1012.txt");
        byte[] buf = new byte[1024];
        int read = 0;
        while ((read = in.read(buf)) != -1) {
            out.write(buf, 0, read);
        }
        // 3.关闭资源
        in.close();
        out.close();
        fs.close();
    }
    
    /**
     * 在hdfs中写数据方式二
     */
    @Test
    public void testWriteData1() throws IllegalArgumentException, IOException{
        // 1.创建输出流
        FSDataOutputStream out = fs.create(new Path("/love"));
        // 2.写数据
        out.write("Areyouokmylove".getBytes());
        // 3.关闭资源
        IOUtils.closeStream(out);
        fs.close();
    }
}
原文地址:https://www.cnblogs.com/areyouready/p/9795436.html