Hadoop文件系统较普通的文件系统差异性主要在于其容错性,普通文件系统不能直接查看hadoop的hdfs对应的文件信息。文件存储起来之后,我们是需要可以访问才能够体现它的价值,hadoop提供了FileSystem API来进行hadoop的文件读写。
本节我是对照hadoop的API中的FileSystem类写的demo,包含了一些主要的方法,更多的需要从相关api中进行查询编写。
- package org.apache.hadoop.wyg;
-
- import java.io.IOException;
- import java.net.URI;
-
- 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.junit.Test;
-
- public class FileSystemAPI {
- private static final String BASE_URL = "hdfs://192.168.88.128:9000";
-
-
-
-
- @Test
- public void testAppend() throws IOException{
- Configuration conf= new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL+"/1.txt"),conf);
- fs.append(new Path(BASE_URL+"/user/root/input/2.txt"));
- System.out.println();
- }
-
-
-
- @Test
- public void testCopyFromLocalFile() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- fs.copyFromLocalFile(new Path("C:\Users\lenovo\Desktop\3.txt"), new Path(BASE_URL+"/user/root/input"));
- }
-
-
-
- @Test
- public void testCopyToLocalFile() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- fs.copyToLocalFile(new Path(BASE_URL+"/user/root/input/3.txt"), new Path("C:\Users\lenovo\Desktop\4.txt"));
- }
-
-
-
- @Test
- public void testCreate() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- Path path = new Path("/user/root/input/4.txt");
- FSDataOutputStream out = fs.create(path,true);
- out.write("hello hadoop".getBytes());
- }
-
-
-
- @Test
- public void testCreateNewFile() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- Path path = new Path("/user/root/input/5.txt");
- fs.createNewFile(path);
- }
-
-
-
- @Test
- public void testDelete() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- Path path = new Path("/user/root/input/5.txt");
- fs.delete(path);
- }
-
-
-
- @Test
- public void testExists() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- Path path = new Path("/user/root/input/5.txt");
- System.out.println(fs.exists(path));
- }
-
-
-
- @Test
- public void testGetFileStatus() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- Path path = new Path("/user/root/input/1.txt");
- FileStatus status = fs.getFileStatus(path);
- System.out.println(status.getModificationTime());
- }
-
-
-
- @Test
- public void testListFile() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- Path path = new Path("/user/root/input");
- FileStatus[] status = fs.listStatus(path);
- for (FileStatus fileStatus : status) {
- System.out.println(fileStatus.getPath());
- }
- }
-
-
-
-
- @Test
- public void testMkdirs() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- Path path = new Path("/user/root/input/sub");
- fs.mkdirs(path);
- }
-
-
-
- @Test
- public void testMoveFromLocalFile() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- Path path = new Path("/user/root/input/sub");
- fs.moveFromLocalFile(new Path("C:\Users\lenovo\Desktop\4.txt"), path);
- }
-
-
-
- @Test
- public void testRename() throws IOException{
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(BASE_URL), conf);
- Path path = new Path("/user/root/input/sub/4.txt");
- Path dest = new Path("/user/root/input/sub/4_1.txt");
- fs.rename(path, dest);
- }
- }
源代码下载地址:http://download.csdn.net/detail/wuyinggui10000/8954567
创建Java项目,File->New->Java Project,命名为TestHDFS
采用单元测试做实验,加入单元测试依赖包,项目导航栏里右键Build Path->Add Libraries->JUnit,以上操作完成如下:
引入hadoop相关外部jar包,Build Path->Add External Archives,jar包包括:
hadoop/lib/*.jar,hadoop/hadoop-core-1.2.1.jar
创建一个java类,TestHDFS.java,继承junit.framework.TestCase,代码开始写:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
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.junit.Test;
import junit.framework.TestCase;
public class TestHDFS extends TestCase {
public static String hdfsUrl = "hdfs://192.168.1.106:8020";
@Test
public void testHDFSMkdir() throws Exception{
//create HDFS folder 创建一个文件夹
Configuration conf= new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
Path path = new Path("/test");
fs.mkdirs(path);
}
@Test
public void testCreateFile() throws Exception{
//create a file 创建一个文件
Configuration conf= new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
Path path = new Path("/test/a.txt");
FSDataOutputStream out = fs.create(path);
out.write("hello hadoop".getBytes());
}
@Test
public void testRenameFile() throws Exception{
//rename a file 重命名
Configuration conf= new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
Path path = new Path("/test/a.txt");
Path newPath = new Path("/test/b.txt");
System.out.println(fs.rename(path, newPath));
}
@Test
public void testUploadLocalFile1() throws Exception{
//upload a local file 上传文件
Configuration conf= new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
Path src = new Path("/home/hadoop/hadoop-1.2.1/bin/rcc");
Path dst = new Path("/test");
fs.copyFromLocalFile(src, dst);
}
@Test
public void testUploadLocalFile2() throws Exception{
//upload a local file 上传文件
Configuration conf= new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
Path src = new Path("/home/hadoop/hadoop-1.2.1/bin/rcc");
Path dst = new Path("/test");
InputStream in = new BufferedInputStream(new FileInputStream(new File("/home/hadoop/hadoop-1.2.1/bin/rcc")));
FSDataOutputStream out = fs.create(new Path("/test/rcc1"));
IOUtils.copyBytes(in, out, 4096);
}
@Test
public void testListFIles() throws Exception{
//list files under folder 列出文件
Configuration conf= new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
Path dst = new Path("/test");
FileStatus[] files = fs.listStatus(dst);
for(FileStatus file: files){
System.out.println(file.getPath().toString());
}
}
@Test
public void testGetBlockInfo() throws Exception{
//list block info of file
查找文件所在的数据块
Configuration conf= new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
Path dst = new Path("/test/rcc");
FileStatus fileStatus = fs.getFileStatus(dst);
BlockLocation[] blkloc=fs.getFileBlockLocations(fileStatus,0,fileStatus.getLen());
//查找文件所在数据块
for(BlockLocation loc: blkloc){
for(int i=0;i < loc.getHosts().length;i++)
System.out.println(loc.getHosts()[i]);
}
}
}
还有很多函数,慢慢使用体会吧。。。
单元测试过程:
Eclipse左侧窗口,展开java类的方法,对某个需要进行单元测试的类右键,选择JUnit Test,如图所示。
先到这里吧。。。
API说明:
http://hadoop.apache.org/docs/current/api/
发表评论 取消回复