Java实现远程HDFS的文件操作(新建、上传、下载、删除)

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;


public class HDFSTest01 {

public static boolean CreatDir(String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem dhfs = FileSystem.get(conf);
       dhfs.mkdirs(dstPath);
}
catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}
 

public static boolean putToHDFS(String src , String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem hdfs = dstPath.getFileSystem(conf) ;
hdfs.copyFromLocalFile(false, new Path(src), dstPath) ;
}
catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}


public static boolean getFromHDFS(String src , String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem dhfs = dstPath.getFileSystem(conf) ;
dhfs.copyToLocalFile(false, new Path(src), dstPath) ;
}catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}

 
public static boolean checkAndDel(final String path , Configuration conf){
Path dstPath = new Path(path) ;
try{
FileSystem dhfs = dstPath.getFileSystem(conf) ;
if(dhfs.exists(dstPath)){
dhfs.delete(dstPath, true) ;
}else{
return false ;
}
}catch(IOException ie ){
ie.printStackTrace() ;
return false ;
}
return true ;
}



public static void main(String[] args) {

boolean status = false ;
String dst1 = "hdfs://192.168.97.135:9000/test/new" ;
Configuration conf = new Configuration() ;
 
//java.lang.IllegalArgumentException: Wrong FS:            hdfs://192.168.1.225:9000/EBLearn_data/hello.txt, expected: file:///
    //解决这个错误的两个方案:
//方案1:下面这条命令必须加上,否则出现上面这个错误
conf.set("fs.default.name", "hdfs://192.168.97.135:9000"); // "hdfs://master:9000"  
    //方案2: 将core-site.xml 和hdfs-site.xml放入当前工程中
   status = CreatDir( dst1 ,  conf) ;
   System.out.println("status="+status) ;

   String dst = "hdfs://192.168.97.135:9000/EBLearn_data" ;
String src = "I:/hello.txt" ;

   status = putToHDFS( src ,  dst ,  conf) ;
System.out.println("status="+status) ;
    
src = "hdfs://192.168.97.135:9000/EBLearn_data/hello.txt" ;
dst = "I:/hadoop_need/" ;
status = getFromHDFS( src ,  dst ,  conf) ;
System.out.println("status="+status) ;
 
dst = "hdfs://192.168.97.135:9000/EBLearn_data/hello.txt" ;
status = checkAndDel( dst ,  conf) ;
System.out.println("status="+status) ;
}

}



项目搭建

这里只是做一个很简单的演示,就是在Web页面提供一个上传按钮,使用户可以将本地文件上传至Hadoop集群平台。

pom.xml

首先看下pom文件的依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.infosys.hadoop</groupId>
    <artifactId>upload</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>upload</name>

    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <hadoop.version>2.6.5</hadoop.version>

    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>


        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.mrunit</groupId>
            <artifactId>mrunit</artifactId>
            <version>1.1.0</version>
            <classifier>hadoop2</classifier>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-minicluster</artifactId>
            <version>${hadoop.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-archetype-plugin</artifactId>
                <version>2.2</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>

                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <outputDirectory>${basedir}</outputDirectory>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121

我们就是添加了一个SpringBootHadoop Client的依赖。其他的是一些测试相关的。关于这个Hadoop Client它提供了一些开发Hadoop应用所需的所有依赖,可以参考之前的一篇博客:Hadoop 2.x Maven开发环境搭建

首页

首页界面就只是提供一个上传表单按钮: 
index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Upload</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
    <p>
        文件:<input type="file" name="file">
    </p>
    <p>
        <input type="submit" value="上传">
    </p>
</form>
</body>
</html>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

然后在Controller提供一个接口进行访问首页: 
HomeController.Java

@Controller
@RequestMapping(value = "/")
public class HomeController {

    public ModelAndView home() {
        return new ModelAndView("index");
    }

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

上传

上传的逻辑也很简单,就是使用SpringBoot上传文件的形式先将文件接收到后台,然后调用Hadoop提供的接口API执行上传。 
上传接口UploadController.java

@Controller
public class UploadController {

    @PostMapping("/upload")
    @ResponseBody
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            try {
                String originalFilename = file.getOriginalFilename();

                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(
                                new File(originalFilename)
                        )
                );

                out.write(file.getBytes());

                out.flush();
                out.close();

                String destFileName = "/user/hadoop/" + originalFilename;

                Upload.main(new String[]{originalFilename, destFileName});

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败, " + e.getMessage();
            }


            return "上传成功";

        } else {
            return "上传失败,文件为空。";
        }

    }

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

最后我们在提供一个类来操作Hadoop接口。 
Upload.java

public class Upload {

    public static final String FS_DEFAULT_FS = "fs.defaultFS";
    public static final String HDFS_HOST = "hdfs://192.168.1.2:9000";
    public static final String CROSS_PLATFORM = "mapreduce.app-submission.cross-platform";


    public static void main(String[] args) throws IOException {

        Configuration conf = new Configuration();

        conf.setBoolean(CROSS_PLATFORM, true);
        conf.set(FS_DEFAULT_FS, HDFS_HOST);

        GenericOptionsParser optionsParser = new GenericOptionsParser(conf, args);

        String[] remainingArgs = optionsParser.getRemainingArgs();
        if (remainingArgs.length < 2) {
            System.err.println("Usage: upload <source> <dest>");
            System.exit(2);
        }

        Path source = new Path(args[0]);
        Path dest = new Path(args[1]);

        FileSystem fs = FileSystem.get(conf);

        fs.copyFromLocalFile(true, false, source, dest);
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

其中的fs.defaultFS属性需要与集群Master NameNode节点中配置的一直。该属性配置一般在etc/hadoop/core-site.xml文件中进行定义。 
可以看到我们实际的操作很简单,就只是调用Hadoop的FileSystem接口中的copyFromLocalFile方法,该方法参数说明:

  • 第一个参数:表示是否删除本地的源文件,也就是上传文件后是否保留原文件,这里为了避免后续文件越来越多,就直接采用上传成功就删除的方式。
  • 第二个参数:表示是否覆盖已存在的文件,这里false表示不覆盖,如果HDFS集群中已存在该文件,就提示上传失败。
  • 第三个参数:源文件路径
  • 第四个参数:上传到HDFS指定的路径

您好,要在Java下载HDFS文件文件夹,可以使用Hadoop API中的FileSystem类。 以下是下载HDFS文件的示例代码: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import java.io.IOException; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; public class HDFSFileDownload { public static void main(String[] args) throws IOException, URISyntaxException { String hdfsUri = "hdfs://localhost:9000"; String filePath = "/user/hadoop/example.txt"; String localPath = "example.txt"; Configuration config = new Configuration(); // 设置HDFS的URI config.set("fs.defaultFS", hdfsUri); // 创建HDFS文件系统对象 FileSystem fs = FileSystem.get(new URI(hdfsUri), config); // 创建HDFS文件路径对象 Path hdfsPath = new Path(filePath); // 创建本地文件路径对象 Path localPathObj = new Path(localPath); // 打开HDFS文件输入流 OutputStream outputStream = fs.create(localPathObj); // 从HDFS文件中复制数据到本地文件 fs.copyToLocalFile(hdfsPath, localPathObj); // 关闭输出流和文件系统 outputStream.close(); fs.close(); } } ``` 要下载HDFS文件夹,可以使用递归函数来遍历文件夹中的所有文件下载它们。以下是示例代码: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; public class HDFSFolderDownload { public static void main(String[] args) throws IOException, URISyntaxException { String hdfsUri = "hdfs://localhost:9000"; String folderPath = "/user/hadoop/example"; String localPath = "example"; Configuration config = new Configuration(); // 设置HDFS的URI config.set("fs.defaultFS", hdfsUri); // 创建HDFS文件系统对象 FileSystem fs = FileSystem.get(new URI(hdfsUri), config); // 创建HDFS文件夹路径对象 Path hdfsFolderPath = new Path(folderPath); // 创建本地文件夹路径对象 Path localFolderPath = new Path(localPath); // 如果本地文件夹不存在,则创建它 if (!localFolderPath.exists()) { localFolderPath.mkdir(); } // 遍历HDFS文件夹并下载所有文件 downloadFolder(fs, hdfsFolderPath, localFolderPath); // 关闭文件系统 fs.close(); } public static void downloadFolder(FileSystem fs, Path hdfsFolderPath, Path localFolderPath) throws IOException { // 获取HDFS文件系统中的所有文件文件夹 Path[] pathList = fs.listPaths(hdfsFolderPath); // 遍历文件文件夹 for (Path path : pathList) { if (fs.isDirectory(path)) { // 如果是文件夹,则递归下载它 Path localPath = new Path(localFolderPath, path.getName()); downloadFolder(fs, path, localPath); } else { // 如果是文件,则下载它 Path localPath = new Path(localFolderPath, path.getName()); fs.copyToLocalFile(path, localPath); } } } } ``` 希望这些代码能够帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值