欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

hadoop的hdfs文件操作實(shí)現(xiàn)上傳文件到hdfs

 更新時(shí)間:2014年03月14日 09:02:59   作者:  
這篇文章主要介紹了使用hadoop的API對(duì)HDFS上的文件訪問,其中包括上傳文件到HDFS上、從HDFS上下載文件和刪除HDFS上的文件,需要的朋友可以參考下

hdfs文件操作操作示例,包括上傳文件到HDFS上、從HDFS上下載文件和刪除HDFS上的文件,大家參考使用吧

復(fù)制代碼 代碼如下:

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;
    }
}

相關(guān)文章

  • Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫(kù)分表的示例代碼

    Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫(kù)分表的示例代碼

    這篇文章主要介紹了Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫(kù)分表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java 無參數(shù)構(gòu)造函數(shù)的應(yīng)用

    Java 無參數(shù)構(gòu)造函數(shù)的應(yīng)用

    本篇文章主要介紹了Java 無參數(shù)構(gòu)造函數(shù)的應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • Spring 緩存抽象示例詳解

    Spring 緩存抽象示例詳解

    Spring框架自身并沒有實(shí)現(xiàn)緩存解決方案,但是從3.1開始定義了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口,提供對(duì)緩存功能的聲明,能夠與多種流行的緩存實(shí)現(xiàn)集成。這篇文章主要介紹了Spring 緩存抽象 ,需要的朋友可以參考下
    2018-09-09
  • windows下使用 intellij idea 編譯 kafka 源碼環(huán)境

    windows下使用 intellij idea 編譯 kafka 源碼環(huán)境

    這篇文章主要介紹了使用 intellij idea 編譯 kafka 源碼的環(huán)境,本文是基于windows下做的項(xiàng)目演示,需要的朋友可以參考下
    2021-10-10
  • SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種)

    SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種)

    這篇文章主要介紹了SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-02-02
  • java實(shí)現(xiàn)留言板功能實(shí)例

    java實(shí)現(xiàn)留言板功能實(shí)例

    這篇文章主要為大家詳細(xì)介紹了JSP+JavaBean的留言板技術(shù) ,JavaWeb登陸功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Spring學(xué)習(xí)之Bean的裝配多種方法

    Spring學(xué)習(xí)之Bean的裝配多種方法

    本篇文章主要介紹了Spring學(xué)習(xí)之Bean的裝配三種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • Java實(shí)現(xiàn)在PPT中創(chuàng)建SmartArt圖形的示例代碼

    Java實(shí)現(xiàn)在PPT中創(chuàng)建SmartArt圖形的示例代碼

    SmartArt其實(shí)就是一個(gè)文字的可視化工具,用戶可在PowerPoint,Word,Excel中使用該特性創(chuàng)建各種圖形圖表。本文就將為您介紹如何通過Java應(yīng)用程序在PPT中創(chuàng)建SmartArt圖形,需要的可以參考一下
    2023-04-04
  • SpringCloud服務(wù)的發(fā)現(xiàn)與調(diào)用詳解

    SpringCloud服務(wù)的發(fā)現(xiàn)與調(diào)用詳解

    在Java微服務(wù)越來越火的今天。幾乎什么公司都在搞微服務(wù)。而使用的比較多的就是Spring?Cloud技術(shù)棧。今天就來研究一下Spring?Cloud中服務(wù)發(fā)現(xiàn)與調(diào)用的基本原理
    2022-07-07
  • Triple協(xié)議支持Java異?;貍髟O(shè)計(jì)實(shí)現(xiàn)詳解

    Triple協(xié)議支持Java異常回傳設(shè)計(jì)實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Triple協(xié)議支持Java異?;貍髟O(shè)計(jì)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評(píng)論