下载在Windows Eclipse上需要的一些额外的文件:hadoop-eclipse-plugin-2.6.0.jar
在eclipse安装目录的dropins目录下新建plugin文件目录,把上面下载的jar包放入plugin文件夹下。
注意本机安装的hadoop运行环境要和自己搭建的集群一致,如果不一致,则会出现异常,当然如果版本不一致,也可以覆盖原来下载解压的bin目录。
HADOOP_HOME=D:\hadoop\hadoop-common-2.2.0-bin-master
Path=D:\hadoop\hadoop-common-2.2.0-bin-master\bin
正常:点击如下田字格图标,出现“小象”Map/Reduce的图标:
异常:找不到“小象”Map/Reduce的图标,重复启动eclipse依然无效:
解决参考:
这种现象一般由于安装在eclipse\plugins下的插件没有导入的问题。解决方法:把eclipse\configuration\org.eclipse.update 删除掉。出现这种情况的原因是在你安装新的插件以前你启动过 eclipse ,在 org.eclipse.update 文件夹下记录了插件的历史更新情况,它只记忆了以前的插件更新情况,而你新安装的插件它并不记录。
根据上面的思路,我将\Eclipse\java-photon\eclipse\configuration\org.eclipse.update\下原先的文件platform.xml删掉,然后重启Eclipse(File->Restart),这次小象的图标就出现了,而且在Window–>Preferences下也出现了Hadoop Map/Reduce。
当然也有说法是eclipse版本问题,也可以重装eclipse,但是不建议使用(太麻烦)。
右击小象图标,添加连接,配置如图所示信息
如图可以配置成集群的主机名,也可以配置成集群的ip,但要注意自己的端口号(与配置文件一致)
若是配置成主机ip,要记得修改本机的host文件,做好主机名与ip的映射
package com.hpe.wangpan.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
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.FileUtil;
import org.apache.hadoop.fs.HdfsBlockLocation;
import org.apache.hadoop.fs.Path;
import com.sun.xml.bind.v2.schemagen.xmlschema.List;
/**
* 1、查看文件
* 2、创建新文件夹
* 3、上传文件
* 4、下载文件
* 5、删除文件
* 6、内部移动
* 7、内部复制
* 8、重命名
* 9、创建新的文件
* 10、写文件
* 11、读文件内容
* @author eversec
*
*/
public class AppTest {
public static void main(String[] args) throws IOException {
//操作HDFS之前得先创建配置对象
Configuration conf = new Configuration(true);
//创建操作HDFS的对象
FileSystem fs = FileSystem.get(conf);
//查看文件系统的内容
// List list = listFileSystem(fs,"/");
//创建文件夹
//createDir(fs,"/test");
//上传文件
// uploadFileToHDFS(fs,"d:/wc","/test/abc/");
//下载文件
// downLoadFileFromHDFS(fs,"/test.txt","d:/");
//删除.....
//重命名
// renameFile(fs,"/test/abc/wc","/test/abc/Angelababy");
//内部移动 内部复制
// innerCopyAndMoveFile(fs,conf,"/test/abc/Angelababy","/");
//创建一个新文件
// createNewFile(fs,"/test/abc/hanhong");
//写文件
writeToHDFSFile(fs,"/qq.txt","hello world");
//追加写
// appendToHDFSFile(fs,"/qq.txt","\nhello world");
//读文件内容
//readFromHDFSFile(fs,"/test.txt");
//获取数据的位置
//getFileLocation(fs,"/install.log");
}
private static void getFileLocation(FileSystem fs, String string) throws IOException {
FileStatus fileStatus = fs.getFileStatus(new Path(string));
long len = fileStatus.getLen();
BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus, 0, len);
String[] hosts = fileBlockLocations[0].getHosts();
for (String string2 : hosts) {
System.out.println(string2);
}
HdfsBlockLocation blockLocation = (HdfsBlockLocation)fileBlockLocations[0];
long blockId = blockLocation.getLocatedBlock().getBlock().getBlockId();
System.out.println(blockId);
}
private static void readFromHDFSFile(FileSystem fs, String string) throws IllegalArgumentException, IOException {
FSDataInputStream inputStream = fs.open(new Path(string));
FileStatus fileStatus = fs.getFileStatus(new Path(string));
//long len = fileStatus.getLen();
byte[] b = new byte[1024];
int read = inputStream.read(b);
while(inputStream.read(b) != -1){
System.out.println(new String(b));
}
}
private static void appendToHDFSFile(FileSystem fs, String filePath, String content) throws IllegalArgumentException, IOException {
FSDataOutputStream append = fs.append(new Path(filePath));
append.write(content.getBytes("UTF-8"));
append.flush();
append.close();
}
private static void writeToHDFSFile(FileSystem fs, String filePath, String content) throws IllegalArgumentException, IOException {
FSDataOutputStream outputStream = fs.create(new Path(filePath));
outputStream.write(content.getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
}
private static void createNewFile(FileSystem fs, String string) throws IllegalArgumentException, IOException {
fs.createNewFile(new Path(string));
}
private static void innerCopyAndMoveFile(FileSystem fs, Configuration conf,String src, String dest) throws IOException {
Path srcPath = new Path(src);
Path destPath = new Path(dest);
//内部拷贝
// FileUtil.copy(srcPath.getFileSystem(conf), srcPath, destPath.getFileSystem(conf), destPath,false, conf);
//内部移动
FileUtil.copy(srcPath.getFileSystem(conf), srcPath, destPath.getFileSystem(conf), destPath,true, conf);
}
private static void renameFile(FileSystem fs, String src, String dest) throws IOException {
Path srcPath = new Path(src);
Path destPath = new Path(dest);
fs.rename(srcPath, destPath);
}
private static void downLoadFileFromHDFS(FileSystem fs, String src, String dest) throws IOException {
Path srcPath = new Path(src);
Path destPath = new Path(dest);
//copyToLocal
// fs.copyToLocalFile(srcPath, destPath);
//moveToLocal
fs.copyToLocalFile(false,srcPath, destPath,true);
}
private static void uploadFileToHDFS(FileSystem fs, String src, String dest) throws IOException {
Path srcPath = new Path(src);
Path destPath = new Path(dest);
//copyFromLocal
fs.copyFromLocalFile(srcPath, destPath);
//moveFromLocal
//fs.copyFromLocalFile(true,srcPath, destPath);
}
private static void createDir(FileSystem fs, String string) throws IllegalArgumentException, IOException {
Path path = new Path(string);
if(fs.exists(path)){
fs.delete(path, true);
}
fs.mkdirs(path);
}
private static List listFileSystem(FileSystem fs, String path) throws FileNotFoundException, IOException {
Path ppath = new Path(path);
FileStatus[] listStatus = fs.listStatus(ppath);
for (FileStatus fileStatus : listStatus) {
System.out.println(fileStatus.getPath());
}
return null;
}
}
1, 在Eclipse可以手动创建连接,但是不能上传,下载,重命名文件?
原因:1,没有配置本机host文件
2,解决方案:修改本机的host文件(c:\windows\system32\drivers\etc),添加主机名和ip映射(和集群配置相同)。
2, 权限问题
原因:由于我们本机一般都是window系统,用户名一般都是Administrator,而我们hdfs创建的文件权限依赖于linux系统,一般是root权限,所以无法访问,报告权限错误。
解决方案: ①更改本机的用户名(小人模式,不建议使用)
②更改HDFS根目录的权限(设置为777)
Hdfs dfs –chmod –R 777 /
3, 可以上传文件,但数据丢失,文件大小显示0,而且无法打开查看。
原因:①防火墙没有全部关闭
②所用的hadoop-common-2.2.0-bin-master版本与集群的hadoop版本不一致
③依旧是权限问题
4, 导入hadoop-eclipse-plugin-2.6.0.jar后,重启eclipse,一直出现异常,且关闭不了。
原因:项目中导入的api架包冲突
解决方案,导入精简版的jar包