在eclipse下进行hadoop项目的创建
1,将我们集群上关于hadoop的所有目录下的文件进行复制,然后首先将hadoop-eclipse-kepler-plugin-2.2.0,hadoop-eclipse-plugin-2.4.0,这两个文件放在eclipse的dropins目录下。这样我们就可以在eclipse的
图一
如图一:我们就会看到mapreduce这个选项,然后再对他进行相应的配置,
如图二:
将图中的选项进行配置,此处应该注意,将你自己的电脑主机名改为root(window下),重启生效。
这个样子,我们的hadoop就已经可以在hadoop上运行了。
在这个例子中,我们先将hadoop中所有的文件中的jar包全部拷到我们在eclipse中所建立的webproject目录下。
首先我们来写一个文件的上传
/**
* 将文件从hdfs中下载到本地文件中
*
*/
public static void uploadhdfsFile2local(String srcPath,String desPath){
Configuration config=new Configuration();
config.addResource("core-site.xml");
config.set("fs.default.name", "hdfs://192.168.10.222:9001");
try {
FileSystem fs=FileSystem.get(config);
Path src=new Path(srcPath);
Path dst=new Path(desPath);
//实现从hdfs到local的下载
fs.copyToLocalFile(src, dst);
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 将文件从hdfs中下载到本地文件中
*
*/
public static void uploadhdfsFile2local(String srcPath,String desPath){
Configuration config=new Configuration();
config.addResource("core-site.xml");
config.set("fs.default.name", "hdfs://192.168.10.222:9001");
try {
FileSystem fs=FileSystem.get(config);
Path src=new Path(srcPath);
Path dst=new Path(desPath);
//实现从hdfs到local的下载
fs.copyToLocalFile(src, dst);
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建文件并追加内容
* @param createFilePath
* @param content
* @throws IOException
*/
public static void createAddFile(String createFilePath,String content) throws IOException{
Configuration config=new Configuration();
config.set("fs.default.name", "hdfs://192.168.10.222:9000");
FileSystem fs=FileSystem.get(config);
//用到一个io类
//创建文件并获取输出流
FSDataOutputStream os=fs.create(new Path(createFilePath));
os.write(content.getBytes("UTF-8"));
os.close();
fs.close();
}
/**
* 删除文件
* @param dstPath
* @return
* @throws IOException
*/
public static boolean deleteHdfsFile(String dstPath) throws IOException{
Configuration config=new Configuration();
config.set("fs.default.name", "hdfs://192.168.10.222:9000");
FileSystem fs=FileSystem.get(config);
boolean flag=fs.delete(new Path(dstPath));
fs.close();
return flag;
}
/**
* 读取文件
* @param dstPath
* @return
* @throws IOException
*/
public static byte[] readHdfsFile(String dstPath) throws IOException{
Configuration config=new Configuration();
config.set("fs.default.name", "hdfs://192.168.10.222:9000");
FileSystem fs=FileSystem.get(config);
Path path=new Path(dstPath);
long strings = 0;
byte[] buffer = null;
//判断文件是否存在
if(fs.exists(path)){
FSDataInputStream is=fs.open(path);
//获取文件信息
FileStatus status=fs.getFileLinkStatus(path);
//创建缓冲数组
//先转为string,在转为int
System.out.println("开始读取文件");
strings=status.getLen();
buffer=new byte[Integer.parseInt(String.valueOf(status.getLen()))];
//开始读取,使用readFully
is.readFully(0,buffer);
is.close();
fs.close();
}
//System.out.println("**读取");
//System.out.println("读取出的"+buffer.toString());
System.out.println(strings);
return buffer;
}
/**
* 创建目录
* @throws IOException
*/
public static void mkdir(String dstPath) throws IOException{
Configuration config=new Configuration();
config.set("fs.default.name", "hdfs://192.168.10.222:9000");
FileSystem fs=FileSystem.get(config);
fs.mkdirs(new Path(dstPath));
fs.close();
}
/**
* 读取某个目录下所有文件
*
*/
public static void listAllFile(String dstPath){
Configuration config=new Configuration();
config.set("fs.default.name", "hdfs://192.168.10.222:9000");
try {
FileSystem fs=FileSystem.get(config);
//filestatus包含文件所有的信息,不仅仅时状态
FileStatus[] status=fs.listStatus(new Path(dstPath));
//遍历
for(int i=0;i<status.length;i++){
if (status[i].isFile()) {
System.out.println(status[i].getPath()+"是一个文件");
}if (status[i].isDirectory()) {
String dirPath=status[i].getPath().toString();
listAllFile(dirPath);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//删除hadoop-2.4.0目录下的所有文件
public static boolean deleteAllFile(){
return false;
}
public static void main(String[] args) throws IOException {
//mkdir("/testyf2");
// deleteHdfsFile("/testyf2");
//listAllFile("/yangfei");
// createAddFile("/testyangfei", "杨飞");
//createAddFile("/yangfei/testyf3.txt", "15154192519841");
//readHdfsFile("/testyangfei");
// readHdfsFile("test2");
// uploadLocalFile2Hdfs("/C:/Users/Administrator/Desktop/tools/hadoop-2.4.0", "/yangfei");
uploadhdfsFile2local("/shengwei/shen.txt", "/D:");
}
}