zoukankan      html  css  js  c++  java
  • 使用Java API操作hdfs

    使用Java Api 操作HDFS

     

    如题 我就是一个标题党  就是使用JavaApi操作HDFS,使用的是MAVEN,操作的环境是Linux  

    首先要配置好Maven环境,我使用的是已经有的仓库,如果你下载的jar包 速度慢,可以改变Maven 下载jar包的镜像站改为 阿里云。

    贴一下  pom.xml

    使用到的jar包

    复制代码
    <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
            </dependency>
            
            <!-- hadoop Client -->
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-client</artifactId>
                <version>${hadoop.version}</version>
            </dependency>
            
    </dependencies>
    复制代码

    然后就是操作HDFS的代码

    复制代码
    package com.zuoyan.hadoop.hdfs;
    
    import java.io.File;
    import java.io.FileInputStream;
    
    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 org.apache.hadoop.io.IOUtils;
    
    /**
     * use java api operate hdfs
     * 
     * @author beifeng
     * 
     */
    public class HdfsApp {
    
        // get FileSystem
        public static FileSystem getFileSystem() throws Exception {
            Configuration conf = new Configuration();
            FileSystem fileSystem = FileSystem.get(conf);
            return fileSystem;
        }
    
        public static void read(String fileName) throws Exception {
    
            FileSystem fileSystem = getFileSystem();
    
            // read Path
            Path readPath = new Path(fileName);
    
            FSDataInputStream inStream = fileSystem.open(readPath);
    
            try {
    
                IOUtils.copyBytes(inStream, System.out, 4096, false);
    
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            } finally {
                // if Exception close Stream
                IOUtils.closeStream(inStream);
            }
        }
    
        public static void main(String[] args) throws Exception{
                
                //String fileName = "/user/beifeng/mapreduce/wordcount/input/wc.input";
                //read(fileName);
            
                FileSystem fileSystem = getFileSystem();
                //write path
                String putFileName = "/user/beifeng/put-wc.input";//文件系统目录
                
                Path writePath = new Path(putFileName);
                
                FSDataOutputStream outputStream = fileSystem.create(writePath);
                
                FileInputStream inputStream = new FileInputStream(
                        new File("/opt/modules/hadoop-2.5.0/wc.input"));//本地系统目录
                
                try {
                    IOUtils.copyBytes(inputStream, outputStream, 4096,false);
                } catch (Exception e) {
                    // TODO: handle exception
                    inputStream.close();
                    outputStream.close();
                }
                
        }
    
    }

    IOUtils.copyBytes()方法:
    IOUtils.copyBytes(in, out, 4096, false)
    --in:是FSDataInputStream类的对象,是有关读取文件的类,也就是所谓“输入流”
    --out:是FSDataOutputStream类的对象,是有关文件写入的类,也就是“输出流”
    --4096表示用来拷贝的buffer大小(buffer是缓冲区)--缓冲区大小
    --// true - 是否关闭数据流,如果是false,就在finally里关掉

    思路 可以使用Java操作hdfs的api  制作一个基于HDFS的   云盘  ,可以对文件进行  上传 、删除、移动目录 、查看目录,但是不可以对文件的内容进行修改!

    作者:Zuoyan

    出处:https://www.cnblogs.com/kangxinxin/p/9682844.html

    本站使用「署名 4.0 国际」创作共享协议,转载请在文章明显位置注明作者及出处。

  • 相关阅读:
    H3CNE学习2,3 TCP-IP模型
    H3CNE学习1 课程简介
    TCPDUMP抓包学习
    Kubernetes 学习26 基于kubernetes的Paas概述
    Kubernetes 学习25 创建自定义chart及部署efk日志系统
    Linux https认证原理
    Kubernetes 学习24 helm入门
    【MySQL基础总结】索引的使用
    【MySQL基础总结】常用函数库
    【MySQL基础总结】运算符的使用
  • 原文地址:https://www.cnblogs.com/drunkPullBreeze/p/11597055.html
Copyright © 2011-2022 走看看