java操作hdfs(上传、下载、查询)

来源:互联网 发布:算法统宗阅读 编辑:程序博客网 时间:2024/05/01 16:18

一、新建java project


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

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

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

四、编写测试类

内容如下:

package com.test;


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());
}
}
}

五、备注

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


1 0
原创粉丝点击