Hadoop好友推荐系统-HDFS的文件上传和下载
程序员文章站
2024-03-22 13:43:28
...
项目总目录:基于Hadoop的好友推荐系统项目综述
一、文件上传到HDFS的实现
1、前端展示
- jsp页面
<table>
<tr>
<td><label for="name">输入路径:</label>
</td>
<td><input class="easyui-validatebox" type="text"
id="localFileId" data-options="required:true" style="width:300px" />
</td>
</tr>
<tr>
<td></td>
<td><a id="uploadId" class="easyui-linkbutton"
data-options="iconCls:'icon-door_in'">上传</a></td>
</tr>
</table>
这里需要我们手动输入文件存储的完整路径。
- js逻辑
// =====uploadId,数据上传button绑定 click方法
$('#uploadId').bind('click', function(){
var input_i=$('#localFileId').val();
// 弹出进度框
popupProgressbar('数据上传','数据上传中...',1000);
// ajax 异步提交任务
callByAJax('cloud/cloud_upload.action',{input:input_i});
});
这里通过callByAJax这个自定义方法实现任务提交。该方法定义如下:
// 调用ajax异步提交
// 任务返回成功,则提示成功,否则提示失败的信息
function callByAJax(url,data_){
$.ajax({
url : url,
data: data_,
async:true,
dataType:"json",
context : document.body,
success : function(data) {
// $.messager.progress('close');
closeProgressbar();
console.info("data.flag:"+data.flag);
var retMsg;
if("true"==data.flag){
retMsg='操作成功!';
}else{
retMsg='操作失败!失败原因:'+data.msg;
}
$.messager.show({
title : '提示',
msg : retMsg
});
if("true"==data.flag&&"true"==data.monitor){// 添加监控页面
// 使用单独Tab的方式
layout_center_addTabFun({
title : 'MR算法监控',
closable : true,
// iconCls : node.iconCls,
href : 'cluster/monitor_one.jsp'
});
}
}
});
}
2、后台实现
action对应的URL从这里获得:callByAJax(‘cloud/cloud_upload.action’,{input:input_i})
/**
* 上传文件
*/
public void upload(){
Map<String,Object> map = HUtils.upload(input, HUtils.getHDFSPath(HUtils.SOURCEFILE));
//HUtils.SOURCEFILE是文件上传到linux上的文件路径+文件名
//getHDFSPath则获得相应的url路径
Utils.write2PrintWriter(JSON.toJSONString(map));
return ;
}
其中涉及的HUtils中方法如下:
/**
* 上传本地文件到HFDS
*
* @param localPath
* @param hdfsPath
* @return
*/
public static Map<String, Object> upload(String localPath, String hdfsPath) {
Map<String, Object> ret = new HashMap<String, Object>();
FileSystem fs = getFs();
Path src = new Path(localPath);
Path dst = new Path(hdfsPath);
try {
fs.copyFromLocalFile(src, dst);//上传到HDFS
Utils.simpleLog(localPath+"上传至"+hdfsPath+"成功");
} catch (Exception e) {
ret.put("flag", "false");
ret.put("msg", e.getMessage());
e.printStackTrace();
return ret;
}
ret.put("flag", "true");
return ret;
}
/**
* 返回HDFS路径
*
* @param url
* @return fs.defaultFs+url
*/
public static String getHDFSPath(String url) {
return Utils.getKey("fs.defaultFS", flag) + url;
}
运行截图
首先指定输入文件路径:E:\HadoopProject\Hadoop_FindFriend\src\main\resources\ask_ubuntu_users.xml
点击上传按钮,上传完毕后查看HDFS目录如下:
二、HDFS文件的下载实现
1、前端展示
- jsp页面
<table>
<tr>
<td><label for="name">HDFS路径:</label>
</td>
<td><input class="easyui-validatebox" type="text"
id="fromHDFSFileId" data-options="required:true"
style="width:300px" value="/user/root/_filter/deduplicate" /></td>
</tr>
<tr>
<td><label for="name">本地路径:</label>
</td>
<td><input class="easyui-validatebox" type="text"
id="tolocalFileId" data-options="required:true" style="width:300px"
value="WEB-INF/classes/deduplicate_users" /></td>
</tr>
<tr>
<td></td>
<td><a id="downloadId" href="" class="easyui-linkbutton"
data-options="iconCls:'icon-page_white_put'">下载</a></td>
</tr>
</table>
这里设定默认的HDFS输入文件夹是/user/root/_filter/deduplicate;本地的输出文件夹是当前项目目录下的”WEB-INF/classes/deduplicate_users。
- js逻辑
// ===== 数据下载
$('#downloadId').bind('click', function(){
var input_=$('#fromHDFSFileId').val();
var output_=$('#tolocalFileId').val();
// 弹出进度框
popupProgressbar('数据下载','数据下载中...',1000);
// ajax 异步提交任务
callByAJax('cloud/cloud_download.action',{input:input_,output:output_});
});
这里依然是通过callByAJax这个函数来实现任务参数的提交,对应的action从这里获取:cloud/cloud_download.action。
2、后台逻辑
action层对应的URL是cloud/cloud_download.action
/**
* 下载文件到本地文件夹
*/
public void download(){
// output 应该和HUtils.DEDUPLICATE_LOCAL保持一致
Map<String,Object> map = HUtils.downLoad(input, Utils.getRootPathBasedPath(output));
Utils.write2PrintWriter(JSON.toJSONString(map));
return ;
}
其中涉及Utils的downLoad和getRootPathBasedPath的定义如下:
/**
* 下载文件
* @param hdfsPath
* @param localPath
* ,本地文件夹
* @return
*/
public static Map<String, Object> downLoad(String hdfsPath, String localPath) {
Map<String, Object> ret = new HashMap<String, Object>();
FileSystem fs = getFs();
Path src = new Path(hdfsPath);
Path dst = new Path(localPath);
try {
RemoteIterator<LocatedFileStatus> fss = fs.listFiles(src, true);
int i = 0;
while (fss.hasNext()) {
LocatedFileStatus file = fss.next();
if (file.isFile() && file.toString().contains("part")) {//只下载文件名包含“part”的文件,也就是结果文件
// 使用这个才能下载成功
fs.copyToLocalFile(false, file.getPath(), new Path(dst,
"hdfs_" + (i++) + HUtils.DOWNLOAD_EXTENSION), true);
}
}
} catch (Exception e) {
ret.put("flag", "false");
ret.put("msg", e.getMessage());
e.printStackTrace();
return ret;
}
ret.put("flag", "true");
return ret;
}
/**
* 获得根路径下面的subPath路径
* @param subPath
* @return
*/
public static String getRootPathBasedPath(String subPath){
return getRootPath()+subPath;
}
/**
* 获得根路径
* @return
*/
private static String getRootPath(){
return ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");
}