前面写了Linux的Hadoop集群搭建,现在需要把Lniux与eclipes连接起来。完成一些简单常用的api。

搭建外部客户端:

1、配置环境变量

1.1 配置jdk的环境变量

1.2 配置hadoop的环境变量

变量名:HADOOP_HOME

路径:

搭建hdfs api环境 hdfs开发接口_配置文件


1.3修改Path

%HADOOP_HOME%\bin

1.4修改系统用户名

在系统变量中新建HADOOP_USER_NAME

值为root

搭建hdfs api环境 hdfs开发接口_hadoop_02


2、配置eclipse环境

2.1在eclipse启动之前,将hadoop-eclipse-plugin-2.6.0.jar复制到eclipse的安装目录下的plugins

2.2启动eclipse

跟图操作走起来!!!

搭建hdfs api环境 hdfs开发接口_配置文件_03


搭建hdfs api环境 hdfs开发接口_配置文件_04


搭建hdfs api环境 hdfs开发接口_hadoop_05


需要注意画出的这几点要填写

location name

暂时用不到 DFS Master 把勾选取掉

Host : 集群ip

port : 需要查看集群配置文件的设置,笔者呢,默认设置9000或者启动集群查看,以下附图

第一种:

搭建hdfs api环境 hdfs开发接口_hadoop_06


第二种:

搭建hdfs api环境 hdfs开发接口_hadoop_07


3、操作HDFS文件系统

①创建目录 hdfs dfs -mkdir -p /user/root

②上传文件 hdfs dfs -D dfs.blocksize=1048576 -put

hdfs dfs 是默认格式

上传完成在网页上查看

搭建hdfs api环境 hdfs开发接口_搭建hdfs api环境_08


4、创建项目

4.1windows-perferences

HADOOP Map/Reduce ----放置hadoop-2.6.5

搭建hdfs api环境 hdfs开发接口_Test_09


java–Build path —User Librarice ===导入依赖包

搭建hdfs api环境 hdfs开发接口_配置文件_10


4.2创建一个项目

右键项目,选peoperties

添加User Library

搭建hdfs api环境 hdfs开发接口_搭建hdfs api环境_11


4.3项目创建一个Folder

将配置文件从linux集群hadoop的配置文件复制到这里。将集群与eclipes进行连接

搭建hdfs api环境 hdfs开发接口_hadoop_12


注意: 一定让文件生效!!!
一定让文件生效!!!
一定让文件生效
!!!

右键包 -----build path ---- 生效

4.4api

public class TestHdfs {

//引入配置文件
	Configuration conf=null;
	//创建文件流----引用的是hadoop内部封装的方法
	FileSystem fs=null;	
	
	@Before
	public void conn() throws Exception{
		conf=new Configuration(true);//设置是否读取配置信息
		fs=FileSystem.get(conf);
	}
	@After
	public void close() throws Exception{
		fs.close();
	}
	
	//创建、删除、重命名、判断是否存在
	
	//创建文件
	@Test
	public void mkdir() throws Exception{
		Path f=new Path("/aaa");
		//判断是否存在
		if(fs.exists(f)){
			//删除
			fs.delete(f);
		}
		//创建
		fs.mkdirs(f);
		
	}
	//自己补充完成
	public void exist(){
		
	}
	//重命名
	@Test
	public void rn() throws Exception{
		Path p1 = new Path("/user/root/profile");
		Path p2 = new Path("/user/root/haha.txt");
		boolean rename = fs.rename(p1, p2);
		System.out.println(rename);
	}
	
	
	

	//上传文件
	@Test
	public void uploadFile() throws Exception{
		//输入位置,相当于文件内容的输入
		InputStream input=new BufferedInputStream(new FileInputStream(new File("d:\\kk.txt")));
		
		//输出位置
		Path inputFile=new Path("/root/haha.txt");
		//相当于文件内容的输出
		FSDataOutputStream output = fs.create(inputFile);
		
		
		IOUtils.copyBytes(input, output, conf, true);
		
	}
	//下载文件
	@Test
	public void downloadFile() throws Exception{
		

		//上传文件到HDFS
		Path src = new Path("/root/haha.txt");
		//输入源:将我集群中的文件作为输入
		FSDataInputStream input=fs.open(src);
		//输出位置
		FileOutputStream output=new FileOutputStream("F://aa.txt");
		

		IOUtils.copyBytes(input, output, conf, true);
	}
	//上传下载

}

报错问题

1.找不到net.host

在windows系统配置

c盘 -----windows------system32-----drivers-------etc------hosts

搭建hdfs api环境 hdfs开发接口_配置文件_13