HDFS文件系统输入输出



import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

    public class HDFSop {
        public static void main(String[] args) throws Exception {
//            hdfsWriteFile("E:\\hadoopstu\\resources\\helloworld.txt",
//                    "hdfs://192.168.111.131:9000/",
//                    "hdfs://192.168.111.131:9000/input/helloworld.txt");
//
            hdfsReadFile("hdfs://192.168.111.131:9000/input/helloworld.txt",
                "hdfs://192.168.111.131:9000/",
                "C:\\Users\\10169\\Desktop\\hw.txt");
        }

        public static void hdfsReadFile(String hdfsFile, String hdfsUrl, String fileName) throws Exception {
            Configuration cfg = new Configuration();
            cfg.set("fs.defaultFS", hdfsUrl);
            FileSystem fileSystem = FileSystem.get(cfg);

            if(! fileSystem.exists(new Path(hdfsFile))){
                throw new Exception("要下载的文件内容不存在");
            }

            try {
                // 读取操作,从hdfs指定的文件读出数据对象
                FSDataInputStream fsdiStream = fileSystem.open(new Path(hdfsFile));

                try {
                    // 将数据输出到本地文件的流对象
                    FileOutputStream fileOutputStream = new FileOutputStream(fileName);

                    try {
                        byte[] buffer = new byte[2048];
                        int count = fsdiStream.read(buffer, 0, 2048);
                        while (count>0){
                            fileOutputStream.write(buffer,0,count);
                            count = fsdiStream.read(buffer,0,2048);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        fileOutputStream.close();
                    }

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    fsdiStream.close();
                }


            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }

        public static void hdfsWriteFile(String fileName, String hdfsUrl, String hdfsFile) throws Exception {
            Configuration cfg = new Configuration();
            cfg.set("fs.defaultFS", hdfsUrl);
            FileSystem fileSystem = FileSystem.get(cfg);
            if (fileSystem.exists(new Path(hdfsFile))) {
                throw new Exception("目标文件已经存在!");
            }

            try {
                // 输出流对象,将数据输出到HDFS文件系统
                FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path(hdfsFile));

                try {
                    // 输入流对象,将本地要上传的文件读取到内存中
                    FileInputStream fileInputStream = new FileInputStream(fileName);   // alt +  shift + z

                    try {
                        byte[] buffer = new byte[2048];
                        int count = fileInputStream.read(buffer, 0, 2048);
                        while (count > 0) {
                            fsDataOutputStream.write(buffer, 0, count);  // 输出操作
                            count = fileInputStream.read(buffer, 0, 2048);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        fileInputStream.close();
                    }


                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    fsDataOutputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }

pom.xml

<?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>nj.zb.kb15</groupId>
  <artifactId>hadoopstu</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hadoopstu</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <hadoop-version>2.6.0</hadoop-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>${hadoop-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>${hadoop-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-core</artifactId>
      <version>${hadoop-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-common</artifactId>
      <version>${hadoop-version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值