windows下eclipse+hadoop2.4开发手册

1.解压下载的hadoop2.4,到任意盘符,例如D:\hadoop-2.4.0。

2.设置环境变量

①新建系统变量,如下所示。

②将新建的HADOOP_HOME变量“%HADOOP_HOME%\bin;”加入到PATH变量里,如下图。

3.将hadoop服务器下的hadoop目录下etc/hadoop目录下的以下四个文件拷贝到自己开发的电脑相应目录下,如下图所示。

4.如果hadoop服务器中上述四个文件配置的是机器名,请在开发的电脑中改为ip地址,例如下图。

5.将hadoop目录下的所有jar包拷贝到自己的项目中,例如下图所示。

①将“D:\hadoop-2.4.0\share\hadoop”目录下及其子目录中所有jar以及子目录下的lib目录下的jar拷贝到自己的项目中。

②我一共拷贝了117个jar,如下图所示。

6.将hadoop服务器上的hadoop目录下的etc/hadoop目录下的以下两个文件拷贝到项目中的src目录下,同样将文件内容中的机器名改为ip。

7.HDFS操作类

其中hdfspath例如:"hdfs://192.168.1.103:9000/input/";//要保证你的hdfs空间中有此路径   

import
java.io.FileNotFoundException;

import
java.io.FileOutputStream;

import
java.io.IOException;

import
java.io.InputStream;

import
java.io.OutputStream;

import
java.net.URI;

import java.util.ArrayList;

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.Path;

import
org.apache.hadoop.io.IOUtils;

import
org.apache.hadoop.util.Progressable;

public class
HDFSOperation {

private     Configuration
conf;

private FileSystem fs;

/**

* @Title: HDFSOperation

* @Description 初始化配置

* @author 
cpthack

* @see    
初始化配置

* @return 
对参数的说明

* @param  
对方法中某参数的说明

* @example 方法使用例子

* */

public HDFSOperation() throws
IOException{

conf = new Configuration();

fs = FileSystem.get(conf);

}

/**

* @Title: upLoad

* @Description 上传文件

* @author 
cpthack

* @see    
上传文件

* @return 
对参数的说明

* @param  
in:文件输入流;hdfsPath:保存在云端的文件路径

* @example 方法使用例子

* */

public boolean upLoad(InputStream in,
String hdfsPath){

Path p = new Path(hdfsPath);

try{

if(fs.exists(p)){

System.out.println("文件已经存在");

return
false;

}

//获得hadoop系统的连接

FileSystem fs =
FileSystem.get(URI.create(hdfsPath),conf);

//out对应的是Hadoop文件系统中的目录

OutputStream out = fs.create(new
Path(hdfsPath));

IOUtils.copyBytes(in, out, 4096,true);//4096是4k字节

in.close();

}catch(Exception e){

e.printStackTrace();

}

return true;

}

/**

* @Title: upLoad

* @Description 下载文件

* @author 
cpthack

* @see    
下载文件

* @return 
对参数的说明

* @param  
localPath:文件保存在本地的路径;hdfsPath:文件存在云端的路径

* @example 方法使用例子

* */

@SuppressWarnings("resource")

public boolean downLoad(String
hdfsPath,String localPath ){

Path path = new
Path(hdfsPath);

try {

if(!fs.exists(path)){

System.out.println("云端文件不存在");

return
false;

}

FileSystem hdfs =
FileSystem.get(conf);

Path dstPath = new
Path(localPath);

hdfs.copyToLocalFile(true,path,
dstPath);

} catch (Exception e) {

e.printStackTrace();

return false;

}

return true;

}

public boolean downFromCloud(String
hdfsPath,String srcFileName){

// 实例化一个文件系统

FileSystem fs;

try {

fs =
FileSystem.get(URI.create(hdfsPath), conf);

// 读出流

FSDataInputStream
HDFS_IN = fs.open(new Path(hdfsPath));

// 写入流

OutputStream
OutToLOCAL = new FileOutputStream(srcFileName);

// 将InputStrteam 中的内容通过IOUtils的copyBytes方法复制到OutToLOCAL中

IOUtils.copyBytes(HDFS_IN,
OutToLOCAL, 1024, true);

return true;

} catch (IOException
e) {

e.printStackTrace();

return
false;

}

}

/**

* @Title: deletePath

* @Description 删除文件

* @author 
cpthack

* @see    
删除文件

* @return 
对参数的说明

* @param  
hdfsPath:文件存在云端的路径

* @example 方法使用例子

* */

public boolean deletePath(String
hdfsPath){

try {

fs.delete(new
Path(hdfsPath), true);

} catch (IOException e) {

e.printStackTrace();

return false;

}

return true;

}

/**

* @Title: getFileList

* @Description 获取某个目录下所有文件

* @author 
cpthack

* @see   
 获取某个目录下所有文件

* @return 
对参数的说明

* @param  
hdfsPath:存在云端的文件夹

* @example 方法使用例子

* */

public ArrayList<FileBean>
getFileList(String hdfsPath){

Path path = new
Path(hdfsPath);

ArrayList<FileBean>
fileList = new ArrayList<FileBean>();

FileStatus[] status;

try {

status =
fs.listStatus(path);

for(FileStatus fs :
status){

fileList.add(new
FileBean(fs));

}

} catch (Exception e) {

e.printStackTrace();

}

return fileList;

}

//创建文件夹

public boolean  mkdir(String dir){

FileSystem fs;

try {

fs =
FileSystem.get(conf);

fs.mkdirs(new
Path(dir));

fs.close();

} catch (IOException e) {

e.printStackTrace();

return false;

}

return true;

}

/*删除文件夹*/

@SuppressWarnings("deprecation")

public boolean deleteDir(String dir){

FileSystem fs;

try {

fs =
FileSystem.get(conf);

fs.delete(new
Path(dir));

fs.close();

} catch (IOException e) {

e.printStackTrace();

return false;

}

return true;

}

}

windows下eclipse+hadoop2的更多相关文章

  1. [b0007] windows 下 eclipse 开发 hdfs程序样例

    目的: 学习使用hdfs 的java命令操作 相关: 进化: [b0010] windows 下 eclipse 开发 hdfs程序样例 (二) [b0011] windows 下 eclipse 开 ...

  2. windows下Eclipse安装Perl插件教程

    windows下Eclipse安装Perl插件教程 想用eclipse编写perl.网上看了很多资料.但EPIC插件的下载连接都失效了.无奈,只好自己动手写个教程记录一下. 准备工作: 安装好Ecli ...

  3. windows下Eclipse操作MapReduce例子报错:Failed to set permissions of path: \tmp\hadoop-Jerome\mapred\staging\

    windows下Eclipse操作MapReduce例子报错: 14/05/18 22:05:29 WARN util.NativeCodeLoader: Unable to load native- ...

  4. windows下eclipse远程连接hadoop集群开发mapreduce

    转载请注明出处,谢谢 2017-10-22 17:14:09  之前都是用python开发maprduce程序的,今天试了在windows下通过eclipse java开发,在开发前先搭建开发环境.在 ...

  5. [b0011] windows 下 eclipse 开发 hdfs程序样例 (三)

    目的: 学习windows 开发hadoop程序的配置. [b0007] windows 下 eclipse 开发 hdfs程序样例 太麻烦 [b0010] windows 下 eclipse 开发 ...

  6. [b0010] windows 下 eclipse 开发 hdfs程序样例 (二)

    目的: 学习windows 开发hadoop程序的配置 相关: [b0007] windows 下 eclipse 开发 hdfs程序样例 环境: 基于以下环境配置好后. [b0008] Window ...

  7. Windows下Eclipse提交MR程序到HadoopCluster

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 欢迎转载,转载请注明出处. 以前Eclipse上写好的MapReduce项目经常是打好包上传到Hadoop测试集 ...

  8. Windows下Eclipse连接hadoop

    2015-3-27 参考: http://www.cnblogs.com/baixl/p/4154429.html http://blog.csdn.net/u010911997/article/de ...

  9. windows下eclipse远程连接hadoop错误“Exception in thread"main"java.io.IOException: Call to Master.Hadoop/172.20.145.22:9000 failed ”

    在VMware虚拟机下搭建了hadoop集群,ubuntu-12.04,一台master,三台slave.hadoop-0.20.2版本.在 master机器上利用eclipse-3.3连接hadoo ...

随机推荐

  1. h.264 scanning process for transform coefficients

    宏块在经过变换.量化后,得到大小为4x4或者8x8的矩阵,矩阵中的数据被称为transform coefficient levels.这些level在后面会被用于熵编码,因此我们需要把矩阵按照一定顺序 ...

  2. c# 函数练习;结构体、枚举类型

       * 结构体 1.就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样. 注意:枚举类型和结构体都属于值类型. 2.定义的方法: struct student { public in ...

  3. Android 用代码来实现selector

    众所周知,android可以通过XML文件来创建selector,以Drawable对象的形式安装到组件上,以提供统一的风格设置.但是在某些时候,我们需要通过代码的形式来实现相同的功能,例如组件数量非 ...

  4. java 集合框架图

    Java平台提供了一个全新的集合框架.“集合框架”主要由一组用来操作对象的接口组成.不同接口描述一组不同数据类型. Java 2集合框架图集合接口:6个接口(短虚线表示),表示不同集合类型,是集合框架 ...

  5. 别再说“我已经努力了”,你的“努力”一文不值!

    有次,让一个研究生男收集一份资料,快下班了问结果,竟然毛也没有.见我要怒,他慷慨激昂地说:"我已经很努力找了,但真的查不到." 作为主管,"我已经努力"这话我不 ...

  6. css3倒影

    使用CSS3制作倒影 img { -webkit-box-reflect: below 0px -webkit-gradient(linear, left top, left bottom, from ...

  7. (转载)PHP json_encode() 函数介绍

    (转载) 在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它. 这个函数的功能是将数值转换成json数据存 ...

  8. dotfuscator使用方法

    dotfuscator如何对.net程序进行混淆保护对于程序代码的保护,网上有很多资料,有的说混淆,有的说加密,我比较支持混淆的方法,这样可以让反编译劳工,头晕一阵子,哈哈开玩笑.对于加密如果不是不得 ...

  9. 利用腾讯企业邮箱开放API获取账户未读邮件数初探

    公司一直使用腾讯提供的免费企业邮箱服务,今天用管理员帐户登录后发现,原来现在腾讯的企业邮箱也开放了部分API 你可以通过开放接口实现以下功能: 数据同步 数据同步可以帮助你同步部门成员信息,你还可以创 ...

  10. Unity3d shader之卡通着色Toon Shading

    卡通着色的目的是为了让被着色物体显得过渡的不那么好,明暗交界线很明显,等等卡通风格的一系列特征, 也叫Non-photorealisticrendering非真实渲染 重点要做到两点: 1.    描 ...