1、配置环境变量HADOOPHOME,不然会报错。
2、windows上的权限系统和linux上的权限系统,测试期间为了简单起见可以关闭权限检查 在namenode的hdfs-site.xml上,添加配置:
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
3、这里需要引入Hadoop的lib,引入有2种方式:
HADOOPHOME\share\hadoop\hdfs下的jar,和lib下的jar包统一导入,这里面HDFS必要的jar包;另外还需要
H
A
D
O
O
P
H
O
M
E
\share
\hadoop
\hdfs
下
的
j
a
r
,
和
l
i
b
下
的
j
a
r
包
统
一
导
入
,
这
里
面
H
D
F
S
必
要
的
j
a
r
包
;
另
外
还
需
要
{HADOOP_HOME}\share\hadoop\common下的jar,和lib下的jar包,这个通用jar包的支持。
根据自己的版本,在http://mvnrepository.com/ maven仓库找到自己自己的版本引入,这里使用的是2.7.3,根据自己需要改变版本,pom引入在文章结尾。
测试代码:
package learn.hadoop.hdsf.test;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* @author CX
*/
public class HdfsTest {
Configuration conf = null;
FileSystem fs = null;
@org.junit.Before
public void conn () throws Exception{
conf =new Configuration(false);
conf.set("fs.defaultFS", "hdfs://cx:8020");
conf.set("dfs.replication", "1");
conf.set("dfs.block.size", "3145728");
conf.set("dfs.client.use.datanode.hostname", "true");
fs = FileSystem.get(conf);
}
@org.junit.After
public void close() throws Exception{
fs.close();
}
/**
* 链接测试
*/
@Test
public void testCon(){
System.out.println(conf.get("fs.defaultFS"));
}
/**
* 创建目录
* @throws Exception
*/
@Test
public void mkdir () throws Exception{
Path tmp = new Path("/temp");
if(!fs.exists(tmp)){
fs.mkdirs(tmp );
}
}
/**
* 上传文件
* @throws Exception
*/
@Test
public void uploadFile() throws Exception{
Path file = new Path("/hello.txt");
FSDataOutputStream output = fs.create(file );
InputStream input = new BufferedInputStream(new FileInputStream(new File("D:\\IDEA\\myself\\src\\main\\java\\learn\\hadoop\\hdsf\\test\\hello.txt")));
IOUtils.copyBytes(input, output, conf, true);
}
/**
* 删除文件
* @throws Exception
*/
@Test
public void delete() throws Exception {
Path p =new Path("/input.txt");
if(fs.exists(p)){
fs.delete(p);
}
}
/**
* 读取文件 并打印文件
* @throws Exception
*/
@Test
public void read() throws Exception{
Path f=new Path("/hello.txt");
FileStatus file= fs.getFileStatus(f);
BlockLocation[] bls = fs.getFileBlockLocations(file, 0, file.getLen());
for (BlockLocation bl : bls) {
System.err.println(bl);
}
FSDataInputStream in = fs.open(f);
/*input.seek(1048578);
System.out.println((char)input.readByte());*/
IOUtils.copyBytes(in,System.out,4096,true);
}
}
pom文件,这里的${hadoop.version} 配置的是 :
<properties>
<hadoop.version>2.7.3</hadoop.version>
</properties>
所有依赖:
<dependencies>
<!--Hadoop-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-annotations</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-minicluster</artifactId>
<version>${hadoop.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>