[Hadoop基础]--java操作hdfs(上传、下载、查询)
一、新建java project二、导入hdfs的配置文件到src目录下core-site.xmlhdfs-site.xmlmapred-site.xmlyarn-site.xml三、导入相关jar(hadoop的所有jar包)四、编写测试类import java.io.File;import java.io.FileInputStream;import org.apache.commons...
·
一、新建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());
}
}
}
五、备注
以上测试已经通过,可以直接修改后使用。

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)