Hadoop做文件服务器POC<2>服务开发

Eclipse配置

  1. 新建Maven项目
    选择webapp项目
    配置项目

  2. 配置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>
    

代码开发

  1. 编写HDFSUtil
    新建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());
                 }
             }    
    
  2. 前端页面
    WebContent目录

    Web页面

Web部署

  1. 应用打包成war

     Eclipse>File>Export>Web>war file>HDFSWeb.war
    
  2. 部署到weblogic 11g

     Weblogic Console>部署>new>upload war>finish>激活更改>启动HDFSWeb    
    
  3. 查看部署结果
    Web页面

测试HDFS上传和下载

测试
测试上传
测试下载