您的位置:首页 > 编程语言 > Java开发

【HDFS篇】eclipse下开发hadoop配置相关及测试HDFSDemo

2018-01-31 08:11 465 查看
安装前准备

1.将bin目录下bin目录下的的文件替换到hadoop下的bin。然后将hadoop.dll 他的核心扩展库放在windows system32下。

2.创建jar库 在hadoop的部署包下的涉及到HDFS、MapReduce、common、yarn、tools的jar都放在新建的hadoop-lib下。

3.配置环境变量

新建,HADOOP_HOME

新建HADOOP_USER_NAME,设置成root,原因是在开发HDFS客户端的时候,会告诉HDFS我登陆的用户名称,如果不配置,windows的用户名是什么就会传什么,保证以后方便,不该windows的用户名,直接把用户名配置成环境变量就行。



4.eclipse放入hadopp插件。放入plugins中。然后在视图中可选择hadoop的图标。



5.配置路径



6.新建hadoop





7.新建i项目,注意新建hadoop-lib的jar库。

==================================================

测试demo:

放入基于HA搭建后的hadoop的core-site.xml和hdfs-site.xml 文件

将配置文件的父目录加入source folder。



因为使用hadoop读配置文件的configuration类是在根目录读取配置文件的。所以必须加进来不然没法读取。

测试代码:

public class TestHDFS {
Configuration conf = null;
FileSystem fs = null;

@Before
public void conn() throws Exception{
conf = new Configuration(true);
fs = FileSystem.get(conf);
}

@After
public void close() throws Exception{

4000
fs.close();
}

@Test
public void mkdir() throws Exception{
Path input = new Path("/000");
if(fs.exists(input)){
fs.delete(input, true);
}
fs.mkdirs(input);
}

@Test
public void upload() throws Exception{

Path file = new Path("/000/cx.txt");
FSDataOutputStream out = fs.create(file);
InputStream in = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\jw.txt")));
IOUtils.copyBytes(in, out, conf, true);
}

@Test
public void download() throws Exception{

Path file = new Path("/000/cx.txt");
FSDataInputStream in = fs.open(file);

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("E:\\chenxiao.txt"));
IOUtils.copyBytes(in, out, conf);
}

@Test
public void blks() throws Exception{
Path f = new Path("/user/root/chenxiao.txt");
FileStatus file = fs.getFileStatus(f );
BlockLocation[] blks = fs.getFileBlockLocations(file, 0, file.getLen());

for(BlockLocation blk : blks){
System.out.println(blk.toString());
}

FSDataInputStream in = fs.open(f);
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
in.seek(1048576);
System.out.println((char)in.readByte());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: