常见的HDFS API编程

package com.wyg.hdfs;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.LocalFileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Test;

public class HDFSAPIdemo {
    
    
	//小文件的合并
	@Test
	public void mergefile() throws IOException, URISyntaxException, InterruptedException {
    
    
		System.setProperty("HADOOP_USER_NAME", "root");
		//1、获取filesystem(分布式文件系统)
		FileSystem fileSystem=FileSystem.get(new URI("hdfs://192.168.2.101:9000"),new Configuration(),"root");
		//2、获取hdfs大文件的输出流
		FSDataOutputStream outputStream=fileSystem.create(new Path("/big_txt.txt"));
		//3、获取一个本地文件系统
		LocalFileSystem localFileSystem=fileSystem.getLocal(new Configuration());
		//4、获取本地文件夹下所有的文件详情
		FileStatus[] fileStatuses=localFileSystem.listStatus(new Path("E://input.txt"));
		//5、遍历每个文件,获取每个文件的输入流
		for(FileStatus filrStatus:fileStatuses) {
    
    
			FSDataInputStream inputStream=localFileSystem.open(filrStatus.getPath());
			//6、将小文件复制到大文件
			IOUtils.copy(inputStream, outputStream);
			IOUtils.closeQuietly(inputStream);
		}
		//7、关闭流
		IOUtils.closeQuietly(outputStream);
		localFileSystem.close();
		fileSystem.close();
	}
	
	
	
	//文件的上传
	@Test
	public void uploadfile() throws IOException, URISyntaxException {
    
    
		//防止操作被拒绝可以指定用户名
	System.setProperty("HADOOP_USER_NAME", "root");
		//文件实例化
	FileSystem fileSystem=FileSystem.get(new URI("hdfs://192.168.2.101:9000"), new Configuration());
		//调用方法
	fileSystem.copyFromLocalFile(new Path("E://数据分析/wechart.xls"),new Path("/"));
		//关闭流
	fileSystem.close();
	}
	
	
	
	//实现文件的下载
	@Test
	public void downloadFile() throws IOException, URISyntaxException {
    
    
	//1、实例化文件对象
	FileSystem fileSystem=FileSystem.get(new URI("hdfs://192.168.2.101:9000"), new Configuration());
	//2、获取hdfs的输入流
	FSDataInputStream inputStream=fileSystem.open(new Path("/xiaohua.txt"));
	//3、获取本地路径的输出流
	FileOutputStream outputStream=new FileOutputStream("e://x.txt");
	//4、文件的拷贝
	IOUtils.copy(inputStream, outputStream);
	//5、关闭流
	IOUtils.closeQuietly(inputStream);
	IOUtils.closeQuietly(outputStream);
	fileSystem.close();
	}
	
	
	// hdfs创建文件夹
	@Test
	public void mkdirtest() throws IOException, URISyntaxException {
    
    
		//防止操作被拒绝可以指定用户名
		System.setProperty("HADOOP_USER_NAME", "root");
		// 实例化filesystem对象
		FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.2.101:9000"), new Configuration());

		// 创建文件夹
		boolean b1 = fileSystem.mkdirs(new Path("/AAA/BBB/CCC"));
		System.out.println(b1);
		//创建文件
		fileSystem.create(new Path("/AAA/BBB/CCC/A.txt"));
		// 关闭流
		fileSystem.close();

	}

	// hdfs文件的遍历
	@Test
	public void listfiles() throws IOException, URISyntaxException {
    
    
		// 实例化FileSystem对象
		FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.2.101:9000"), new Configuration());
		// 调用方法listfile 获取根目录下所以的文件信息
		RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/"), true);
		// 遍历迭代器
		while (iterator.hasNext()) {
    
    
			LocatedFileStatus fileStatus = iterator.next();

			// 获取文件的绝对路径:hdfs:192.168.2.101:9000
			System.out.println(fileStatus.getPath() + "======" + fileStatus.getPath().getName());

			// 文件的block信息
			BlockLocation[] blockLocations = fileStatus.getBlockLocations();
			System.out.println("block数量:" + blockLocations.length);
		}
	}

	// 文件系统访问 方式一
	@Test
	public void getFileSystem1() throws IOException {
    
    
		// 1.实例化对象
		Configuration conf = new Configuration();
		// 2、设置文件系统类型
		conf.set("fs.defaultFS", "hdfs://192.168.2.101:9000");
		// 3、获取指定的文件系统
		FileSystem fileSystem = FileSystem.get(conf);
		// 4、输出
		System.out.println(fileSystem);
	}

	// 方式二
	@Test
	public void getFileSystem2() throws IOException {
    
    
		FileSystem fileSystem = FileSystem.get(new Configuration());
		System.out.println(fileSystem);
	}

	// 方式三
	@Test
	public void getFileSystem3() throws IOException {
    
    
		// 实例化文件对象
		Configuration conf = new Configuration();
		// 指定文件系统类型
		FileSystem fileSystem = FileSystem.newInstance(conf);

		System.out.println(fileSystem);
	}

	// 方式四
	@Test
	public void getFileSystem4() throws IOException, URISyntaxException {
    
    
		FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://192.168.2.101:9000"), new Configuration());
		System.out.println(fileSystem);
	}

	// url访问 方式三
	@Test
	public void urlHdfs() throws IOException {
    
    
		// 注册url
		URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
		// 获取hdfs文件的输入流
		InputStream inputStream = new URL("hdfs://192.168.2.101:9000/xiaohua.txt").openStream();
		// 获取本地文件的输出流
		FileOutputStream outputStream = new FileOutputStream(new File("E:\\banzhang.txt"));
		// 实现文件的拷贝
		IOUtils.copy(inputStream, outputStream);
		// 关系流
		IOUtils.closeQuietly(inputStream);
		IOUtils.closeQuietly(outputStream);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_46457946/article/details/113835160