windows下eclipse+hadoop2
2020-12-13 04:51
标签:des style blog http java 使用 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 Path path = new
Path(hdfsPath); ArrayList 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,搜素材,soscw.com windows下eclipse+hadoop2 标签:des style blog http java 使用 原文地址:http://www.cnblogs.com/macula/p/3853496.html