hadoop的hdfs文件操作实现上传文件到hdfs

 更新时间:2014年03月14日 09:02:59   作者:  
这篇文章主要介绍了使用hadoop的API对HDFS上的文件访问,其中包括上传文件到HDFS上、从HDFS上下载文件和删除HDFS上的文件,需要的朋友可以参考下

hdfs文件操作操作示例,包括上传文件到HDFS上、从HDFS上下载文件和删除HDFS上的文件,大家参考使用吧

复制代码 代码如下:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.File;
import java.io.IOException;
public class HadoopFile {
    private Configuration conf =null;

    public HadoopFile(){
        conf =new Configuration();
        conf.addResource(new Path("/hadoop/etc/hadoop/core-site.xml"));
    }

    public HadoopFile(Configuration conf){
        this.conf =conf;
    }

    public boolean sendFile(String path,String localfile){
        File file=new File(localfile);
        if (!file.isFile()) {
            System.out.println(file.getName());
            return false;
        }
        try {
            FileSystem localFS =FileSystem.getLocal(conf);
            FileSystem hadoopFS =FileSystem.get(conf);
            Path hadPath=new Path(path);

            FSDataOutputStream fsOut=hadoopFS.create(new Path(path+"/"+file.getName()));
            FSDataInputStream fsIn=localFS.open(new Path(localfile));
            byte[] buf =new byte[1024];
            int readbytes=0;
            while ((readbytes=fsIn.read(buf))>0){
                fsOut.write(buf,0,readbytes);
            }
            fsIn.close();
            fsOut.close();

            FileStatus[] hadfiles= hadoopFS.listStatus(hadPath);
            for(FileStatus fs :hadfiles){
                System.out.println(fs.toString());
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean delFile(String hadfile){
        try {

            FileSystem hadoopFS =FileSystem.get(conf);
            Path hadPath=new Path(hadfile);
            Path p=hadPath.getParent();
            boolean rtnval= hadoopFS.delete(hadPath, true);

            FileStatus[] hadfiles= hadoopFS.listStatus(p);
            for(FileStatus fs :hadfiles){
                System.out.println(fs.toString());
            }
            return rtnval;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    public boolean downloadFile(String hadfile,String localPath){

        try {
            FileSystem localFS =FileSystem.getLocal(conf);
            FileSystem hadoopFS =FileSystem.get(conf);
            Path hadPath=new Path(hadfile);

            FSDataOutputStream fsOut=localFS.create(new Path(localPath+"/"+hadPath.getName()));
            FSDataInputStream fsIn=hadoopFS.open(hadPath);
            byte[] buf =new byte[1024];
            int readbytes=0;
            while ((readbytes=fsIn.read(buf))>0){
                fsOut.write(buf,0,readbytes);
            }
            fsIn.close();
            fsOut.close();

            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

相关文章

  • Springboot2.x+ShardingSphere实现分库分表的示例代码

    Springboot2.x+ShardingSphere实现分库分表的示例代码

    这篇文章主要介绍了Springboot2.x+ShardingSphere实现分库分表的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Java 无参数构造函数的应用

    Java 无参数构造函数的应用

    本篇文章主要介绍了Java 无参数构造函数的应用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Spring 缓存抽象示例详解

    Spring 缓存抽象示例详解

    Spring框架自身并没有实现缓存解决方案,但是从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口,提供对缓存功能的声明,能够与多种流行的缓存实现集成。这篇文章主要介绍了Spring 缓存抽象 ,需要的朋友可以参考下
    2018-09-09
  • windows下使用 intellij idea 编译 kafka 源码环境

    windows下使用 intellij idea 编译 kafka 源码环境

    这篇文章主要介绍了使用 intellij idea 编译 kafka 源码的环境,本文是基于windows下做的项目演示,需要的朋友可以参考下
    2021-10-10
  • SpringBoot下使用定时任务的方式全揭秘(6种)

    SpringBoot下使用定时任务的方式全揭秘(6种)

    这篇文章主要介绍了SpringBoot下使用定时任务的方式全揭秘(6种),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • java实现留言板功能实例

    java实现留言板功能实例

    这篇文章主要为大家详细介绍了JSP+JavaBean的留言板技术 ,JavaWeb登陆功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • Spring学习之Bean的装配多种方法

    Spring学习之Bean的装配多种方法

    本篇文章主要介绍了Spring学习之Bean的装配三种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • Java实现在PPT中创建SmartArt图形的示例代码

    Java实现在PPT中创建SmartArt图形的示例代码

    SmartArt其实就是一个文字的可视化工具,用户可在PowerPoint,Word,Excel中使用该特性创建各种图形图表。本文就将为您介绍如何通过Java应用程序在PPT中创建SmartArt图形,需要的可以参考一下
    2023-04-04
  • SpringCloud服务的发现与调用详解

    SpringCloud服务的发现与调用详解

    在Java微服务越来越火的今天。几乎什么公司都在搞微服务。而使用的比较多的就是Spring Cloud技术栈。今天就来研究一下Spring Cloud中服务发现与调用的基本原理
    2022-07-07
  • Triple协议支持Java异常回传设计实现详解

    Triple协议支持Java异常回传设计实现详解

    这篇文章主要为大家介绍了Triple协议支持Java异常回传设计实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12

最新评论