[Hadoop基础]--java操作hdfs(上传、下载、查询)

本文介绍了一个简单的HDFS操作示例,包括文件夹创建、文件上传、文件列表展示及使用SequenceFile进行多文件压缩等基本功能。

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

一、新建java project


二、导入hdfs的配置文件到src目录下

core-site.xml
hdfs-site.xml
mapred-site.xml
yarn-site.xml

三、导入相关jar(hadoop的所有jar包)

四、编写测试类

import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.io.SequenceFile.Reader;
import org.apache.hadoop.io.Text;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
 * 
* @ClassName: HdfsTest 
* @Description: 测试hdfs的上传和下载,以及
* @author 
* @date 2016年5月23日 下午1:29:18 
*
 */
public class HdfsTest {
private FileSystem fs ;
Configuration conf;
@Before
public void setup()throws Exception{
//读取classpath目录下所有hadoop的配置
conf =new Configuration();
fs =FileSystem.get(conf);
}

@After
public void end()throws Exception{
fs.close();
}


/**
* 
* @Title: mkdir 
* @Description: 测试在hdfs上创建文件夹
* @param @throws Exception    
* @return void   
* @throws
*/
@Test
public void mkdir()throws Exception{
Path dir =new Path("/usr/zs");
fs.mkdirs(dir);
}
/**
* 
* @Title: upload 
* @Description: 测试上传文件到hdfs上
* @param @throws Exception    
* @return void   
* @throws
*/
@Test
public void upload()throws Exception{
Path file =new Path("/usr/zs/11.jar");
FSDataOutputStream out = fs.create(file);
IOUtils.copyBytes(new FileInputStream("D:\\demo-step.jar"), out, conf);
}
/**
* 
* @Title: ls 
* @Description: 列出文件夹下的文件信息
* @param @throws Exception    
* @return void   
* @throws
*/
@Test
public void ls()throws Exception{
Path dir =new Path("/usr/zs");
FileStatus[] fss = fs.listStatus(dir);
for(FileStatus file :fss){
System.out.println(file.isDirectory());
System.out.println(file.getPath());
System.out.println(file.getModificationTime());
System.out.println(file.getLen());
}
}


/**
* SequenceFile:hdfs 中的序列文件,把多个文件合成(压缩)一个大文件
*   :每个小文件:key和value
* string:Text
* int:IntWritble
* long:LongWritble
* Map:MapWritble
* @throws Exception
*/
@Test
public void smallFiles()throws Exception{
Path big =new Path("/usr/zs/test");
SequenceFile.Writer writer =SequenceFile.createWriter(fs, conf, big, Text.class, Text.class, CompressionType.NONE);
for(File file:new File("E:\\data").listFiles()){
writer.append(new Text(file.getName()), new Text(FileUtils.readFileToString(file)));
}
writer.close();
}
/**
* 
* @Title: readSmallFiles 
* @Description: 读取文件内容
* @param @throws Exception    
* @return void   
* @throws
*/
@Test
public void readSmallFiles()throws Exception{
Path big =new Path("/usr/zs/test");
SequenceFile.Reader reader =new Reader(fs, big, conf);
while(reader.next(new Text("NOTICE.txt"))){
Text t =new Text();
reader.getCurrentValue(t);
System.out.println(t.toString());
}
}
}

五、备注

以上测试已经通过,可以直接修改后使用。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oo寻梦in记

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值