简单通过一个示例来使用一下HDFS的Java接口。
package cn.zhoucy.Hadoop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
public class HDFSDemo {
private FileSystem fs = null;
@Before
public void init() throws IOException, URISyntaxException, InterruptedException{
fs = FileSystem.get(new URI("hdfs://123.206.xxx.xxx:9000"), new Configuration(),"root");
}
@Test
public void testDel() throws IllegalArgumentException, IOException{
boolean flag = fs.delete(new Path("/words.txt"), true);
System.out.println(flag);
}
@Test
public void testMkdir() throws IllegalArgumentException, IOException{
boolean flag = fs.mkdirs(new Path("/itcast88888888"));
System.out.println(flag);
}
@Test
public void testUpload() throws IllegalArgumentException, IOException{
FSDataOutputStream out = fs.create(new Path("/words.txt"));
FileInputStream in = new FileInputStream(new File("e:/w.txt"));
IOUtils.copyBytes(in, out, 2048, true);
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://123.206.xxx.xxx:9000");
conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
//工具类FileSystem
FileSystem fs = FileSystem.get(conf);
System.out.println("meiwentoi");
InputStream in = fs.open(new Path("/woo"));//下载,从hdfs下载下来
System.out.println("chucuo");
FileOutputStream out = new FileOutputStream(new File("/home/woo"));
IOUtils.copyBytes(in, out, 2048, true);
}
}
本用例测试的是从HDFS上下载文件存储到Linux文件系统里。在伪分布式的部署下,可以在eclipse中直接运行代码,用junit进行测试,main方法里面就测试下载方法。在分布式部署的情况下,运行会有个报错,可能是哪里配置的不对,打成jar包放到服务器上是可以运行的。这里就采用打成可运行的jar的方式,名字就叫Hadoop。在服务器上使用 java -jar Hadoop.jar,即可完成下载功能。