Eclipse配置
新建Maven项目
配置pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.com.git</groupId> <artifactId>HDFSWeb</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>HDFSWeb Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.6.0</version> </dependency> </dependencies> <build> <finalName>HDFSWeb</finalName> </build> </project>
代码开发
编写HDFSUtil
上传文件
/** * 用输入流上传文件到HDFS * * @param is * @param remoteName * @return * @throws Exception */ public int upload(InputStream is, String remoteName) throws Exception { FileSystem fs = null; try { fs = FileSystem.get(URI.create(HDFS), conf); OutputStream outStream = fs.create(new Path(remoteName), new Progressable() { public void progress() { System.out.print('.'); } }); logger.info("开始上传: " + remoteName); IOUtils.copyBytes(is, outStream, 4096000, true); logger.info("上传结束!"); is.close(); return 0; } catch (IOException e) { is.close(); e.printStackTrace(); return -1; } } /** * 上传文件到HDFS * * @param localName * @param remoteName * @return * @throws Exception */ public int upload(String localName, String remoteName) throws Exception { InputStream is = new BufferedInputStream(new FileInputStream(localName)); return upload(is, remoteName); }
下载文件
/** * HDFS上下载文件并返回文件流 * * @param hadoopFile * @return * @throws Exception */ public FSDataInputStream download(String hadoopFile) throws Exception { FSDataInputStream iStream = null; FileSystem fs = null; fs = FileSystem.get(URI.create(HDFS), conf); Path path = new Path(hadoopFile); iStream = fs.open(path); return iStream; }
删除文件
/** * 删除HDFS文件 * * @param hadoopFile * @return */ public boolean deleteFile(String hadoopFile) { try { FileSystem fs = FileSystem.get(URI.create(HDFS), conf); Path path = new Path(hadoopFile); fs.delete(path, true); fs.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
显示文件夹文件列表
/** * 显示文件夹中文件列表 * * @param folder * @return 文件列表对象 * @throws Exception */ public FileStatus[] list(String folder) throws Exception { Path path = new Path(folder); FileSystem fs = FileSystem.get(URI.create(HDFS), conf); return fs.listStatus(path); } /** * 显示文件夹中文件列表并打印 * * @param folder * @throws Exception */ public void ls(String folder) throws Exception { Path path = new Path(folder); FileSystem fs = FileSystem.get(URI.create(HDFS), conf); FileStatus[] list = fs.listStatus(path); for (FileStatus f : list) { System.out.printf("name: %s, folder: %s, size: %d\n", f.getPath(), f.isDir(), f.getLen()); } }
前端页面
Web部署
应用打包成war
Eclipse>File>Export>Web>war file>HDFSWeb.war
部署到weblogic 11g
Weblogic Console>部署>new>upload war>finish>激活更改>启动HDFSWeb
查看部署结果
测试HDFS上传和下载
测试