条件准备
下载部署
下载Hadoop-2.7.3.tar.gz包,可以去官网下载。也可以下载源码编译:点击打开链接
然后部署在Linux上,可以参考点击打开链接
win下eclipse开发配置
通常习惯,我们会在win的eclipse下开发,那么下载下来的是基于Linux的编译后的包,包下的bin文件夹不兼容windows,那么在调用某些API时会报错:参考后文的报错。
解决方法:
1、我们需要获取基于windows下编译的bin文件夹替换掉Hadoop-2.7.3.tar.gz解压后中的bin文件(可以下载源码包在windows下编译获取bin文件夹,通常我们嫌麻烦,所以直接找资源下载下来:点击打开链接)。
2、配置环境变量,增加变量HADOOP_HOME,值是下载的zip包解压的目录,然后在系统变量path里增加%HADOOP_HOME%\bin 。
3、压缩包里的hadoop.dll,并拷贝到c:\windows\system32目录中。
报错:
如果下载的版本与hadoop不对应,或者windows环境没配置好,会报错:
1、java.io.IOException: (null) entry in command string: null chmod 0644
2、java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO
如果是在Linux中的eclipse开发可以忽略上面的配置步骤。
Demo开发
1.新建工程,然后添加必要的依赖:
将Hadoop的common模块所需要的依赖(公用模块)在eclipse中打成自定义library :Hadoop-common
将Hadoop的hdfs模块所需要的依赖(HDFS文件系统模块)在eclipse中打成自定义library :Hadoop-hdfs
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;
public class HdfsUtil {
private FileSystem fs = null;
/**
* 创建连接
* @throws Exception
*/
@Before
public void init() throws Exception{
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://server201:9000/");
fs = FileSystem.get(new URI("hdfs://server201:9000/"),conf,"root");
}
/**
* 快捷上传文件的API
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testUpload() throws IllegalArgumentException, IOException{
fs.copyFromLocalFile(new Path("G:/demo.ktr"), new Path("/demo.ktr"));
}
/**
* 快捷下载文件的API
* (win下调用此API如何不配置本地haoop环境就会报异常:
* null chmod 0644
* )
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testDownload() throws IllegalArgumentException, IOException{
fs.copyToLocalFile(new Path("/demo.ktr"), new Path("G:/demo123.ktr"));
}
/**
* 创建目录
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testMkdir() throws IllegalArgumentException, IOException{
fs.mkdirs(new Path("/aaa/bbb/ccc"));
}
/**
* 删除目录或者目录
* 方法的第二个参数 代表是否递归删除,只有删除目录时这个参数有效
* 如果是删除文件则该参数无效
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testRm() throws IllegalArgumentException, IOException{
fs.delete(new Path("/user"), true);
}
/**
* 重命名
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testRename() throws IllegalArgumentException, IOException{
fs.rename(new Path("/hadoop-2.4.1.tar.gz"), new Path("/hadoop.tar.gz"));
}
/**
* 遍历hdfs文件和目录
* @throws FileNotFoundException
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testList() throws FileNotFoundException, IllegalArgumentException, IOException{
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while(listFiles.hasNext()){
LocatedFileStatus file = listFiles.next();
System.out.println(file.getPath());
}
System.out.println("----------------------------------");
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus fileStatus: listStatus){
System.out.println((fileStatus.isDirectory()? "-d- ":"-f- ") + fileStatus.getPath());
}
}
/**
* 通过声明流来下载文件
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testGet() throws IllegalArgumentException, IOException{
FSDataInputStream is = fs.open(new Path("/demo111.ktr"));
FileOutputStream os = new FileOutputStream("G:/demo112.ktr");
IOUtils.copy(is, os);
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://server201:9000/");
FileSystem fs = FileSystem.get(new URI("hdfs://server201:9000/"),conf,"root");
FSDataOutputStream fsDataOutputStream = fs.create(new Path("/demo.ktr"));
FileInputStream fileInputStream = new FileInputStream("G:/demo.ktr");
IOUtils.copy(fileInputStream, fsDataOutputStream);
}
}