用java api(用流的方式)对HDFS进行增删改查

本文详细介绍如何使用Java API操作Hadoop分布式文件系统(HDFS),包括文件上传、下载、目录创建、删除、列表显示等基本操作,并展示了如何通过流方式读写HDFS文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.Map.Entry;

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;
import org.junit.Before;
import org.junit.Test;
/**
 * 
 * 客户端去操作hdfs时,是有一个用户身份的
 * 默认情况下,hdfs客户端api会从jvm中获取一个参数来作为自己的用户身份:-DHADOOP_USER_NAME=hadoop
 * 
 * 也可以在构造客户端fs对象时,通过参数传递进去
 * @author
 *
 */
public class HdfsClientDemo {
	FileSystem fs = null;
	Configuration conf = null;
	@Before
	public void init() throws Exception{
		
		conf = new Configuration();
//		conf.set("fs.defaultFS", "hdfs://mini1:9000");
		conf.set("dfs.replication", "5");
		
		//拿到一个文件系统操作的客户端实例对象
		fs = FileSystem.get(conf);
		//可以直接传入 uri和用户身份
		fs = FileSystem.get(new URI("hdfs://mini1:9000"),conf,"hadoop");
	}

	/**
	 * 上传文件
	 * @throws Exception
	 */
	@Test
	public void testUpload() throws Exception {
		
		fs.copyFromLocalFile(new Path("c:/access.log"), new Path("/access.log.copy"));
		fs.close();
	}
	
	
	/**
	 * 下载文件
	 * @throws Exception
	 */
	@Test
	public void testDownload() throws Exception {
		
		fs.copyToLocalFile(new Path("/access.log.copy"), new Path("d:/"));
	}
	
	
	/**
	 * 打印参数
	 */
	@Test
	public void testConf(){
		
		Iterator<Entry<String, String>> it = conf.iterator();
		while(it.hasNext()){
			Entry<String, String> ent = it.next();
			System.out.println(ent.getKey() + " : " + ent.getValue());
			
		}
		
	}
	
	
	@Test
	public void testMkdir() throws Exception {
		boolean mkdirs = fs.mkdirs(new Path("/testmkdir/aaa/bbb"));
		System.out.println(mkdirs);
		
	}
	
	
	@Test
	public void testDelete() throws Exception {
		
		boolean flag = fs.delete(new Path("/testmkdir/aaa"), true);
		System.out.println(flag);
		
	}
	
	
	/**
	 * 递归列出指定目录下所有子文件夹中的文件
	 * @throws Exception
	 */
	@Test
	public void testLs() throws Exception {
		
		RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
		
		while(listFiles.hasNext()){
			LocatedFileStatus fileStatus = listFiles.next();
			System.out.println("blocksize: " +fileStatus.getBlockSize());
			System.out.println("owner: " +fileStatus.getOwner());
			System.out.println("Replication: " +fileStatus.getReplication());
			System.out.println("Permission: " +fileStatus.getPermission());
			System.out.println("Name: " +fileStatus.getPath().getName());
			System.out.println("------------------");
			BlockLocation[] blockLocations = fileStatus.getBlockLocations();
			for(BlockLocation b:blockLocations){
				System.out.println("块起始偏移量: " +b.getOffset());
				System.out.println("块长度:" + b.getLength());
				//块所在的datanode节点
				String[] datanodes = b.getHosts();
				for(String dn:datanodes){
				System.out.println("datanode:" + dn);
				}
			}
			
			
			
		}
		
	}
	
	
	
	@Test
	public void testLs2() throws Exception {
		
		FileStatus[] listStatus = fs.listStatus(new Path("/"));
		for(FileStatus file :listStatus){
			
			System.out.println("name: " + file.getPath().getName());
			System.out.println((file.isFile()?"file":"directory"));
			
		}
		
	}
	
	
	
	
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://mini1:9000");
		//拿到一个文件系统操作的客户端实例对象
		FileSystem fs = FileSystem.get(conf);
		
		fs.copyFromLocalFile(new Path("c:/access.log"), new Path("/access.log.copy"));
		fs.close();
	}
	

	
	
	
}

用流的方式进行操作hdfs

package com.ycit.hdfs;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

import org.apache.commons.*;
import org.apache.commons.compress.utils.IOUtils;
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.junit.Before;
import org.junit.Test;

public class HdfsStreamAcess {
	
	Configuration conf = null;
	FileSystem fs  = null;
	
	@Before
	public void init() throws Exception{
		 conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://mini1:9000");
		fs = FileSystem.get(new URI("hdfs://mini1:9000"), conf, "hadoop");
	}
	/**
	 * 
	 * 通过流的方式上传文件到HDFS
	 * @throws Exception
	 */
	@Test
	public void testUpload() throws Exception{
		FSDataOutputStream outputStream = fs.create(new Path("/china.mylove"), true);
		FileInputStream inputStream = new FileInputStream("D:/china.mylove");
		IOUtils.copy(inputStream, outputStream);
	}
	/**
	 * 向Hdfs服务器取数据
	 * @throws Exception 
	 * @throws IllegalArgumentException 
	 * 
	 */
	@Test
	public void testDownload() throws  Exception{
		FSDataInputStream input = fs.open(new Path("/china.mylove"));
		FileOutputStream output = new FileOutputStream("C:/china.mylove");
		IOUtils.copy(input, output);
	}
	/**
	 * 用流的方式向HDFS服务器取数据可以设置偏移量
	 * @throws Exception 
	 * @throws IllegalArgumentException 
	 * 
	 */
	@Test
	public void testRandomAcess() throws  Exception{
		FSDataInputStream input = fs.open(new Path("/china.mylove"));
		input.seek(12);
		FileOutputStream output = new FileOutputStream("C:/china.mylove");
		IOUtils.copy(input, output);
	}
	/**
	 * 将取到的内容打印到控制台
	 * @throws Exception 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void testCat() throws  Exception{
		FSDataInputStream input = fs.open(new Path("/china.mylove"));
		IOUtils.copy(input,System.out);

	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值