window下hadoop实现文件追加,上传,下载



在eclipse下进行hadoop项目的创建

1,将我们集群上关于hadoop的所有目录下的文件进行复制,然后首先将hadoop-eclipse-kepler-plugin-2.2.0,hadoop-eclipse-plugin-2.4.0,这两个文件放在eclipse的dropins目录下。这样我们就可以在eclipse的

                   图一

如图一:我们就会看到mapreduce这个选项,然后再对他进行相应的配置,

如图二:

将图中的选项进行配置,此处应该注意,将你自己的电脑主机名改为root(window下),重启生效。

这个样子,我们的hadoop就已经可以在hadoop上运行了。

在这个例子中,我们先将hadoop中所有的文件中的jar包全部拷到我们在eclipse中所建立的webproject目录下。

首先我们来写一个文件的上传

  /**

      * 将文件从hdfs中下载到本地文件中

      *

      */

     public static void uploadhdfsFile2local(String srcPath,String desPath){

      Configuration config=new Configuration();

      config.addResource("core-site.xml");

      config.set("fs.default.name""hdfs://192.168.10.222:9001");

      try {

FileSystem fs=FileSystem.get(config);

Path src=new Path(srcPath);

Path dst=new Path(desPath);

//实现从hdfs到local的下载

fs.copyToLocalFile(srcdst);

fs.close();

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    

     }

/**

      * 将文件从hdfs中下载到本地文件中

      *

      */

     public static void uploadhdfsFile2local(String srcPath,String desPath){

      Configuration config=new Configuration();

      config.addResource("core-site.xml");

      config.set("fs.default.name""hdfs://192.168.10.222:9001");

      try {

FileSystem fs=FileSystem.get(config);

Path src=new Path(srcPath);

Path dst=new Path(desPath);

//实现从hdfs到local的下载

fs.copyToLocalFile(srcdst);

fs.close();

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    

     }

/**

      * 创建文件并追加内容

      * @param createFilePath

      * @param content

     * @throws IOException 

      */

     public static void createAddFile(String createFilePath,String contentthrows IOException{

      Configuration config=new Configuration();

    

      config.set("fs.default.name""hdfs://192.168.10.222:9000");

      FileSystem fs=FileSystem.get(config);

      //用到一个io

      //创建文件并获取输出流

      FSDataOutputStream os=fs.create(new Path(createFilePath));

      os.write(content.getBytes("UTF-8"));

      os.close();

      fs.close();

      

     }

     /**

      * 删除文件

      * @param dstPath

      * @return

      * @throws IOException

      */

     public static boolean deleteHdfsFile(String dstPaththrows IOException{

      Configuration config=new Configuration();

    

      config.set("fs.default.name""hdfs://192.168.10.222:9000");

      FileSystem fs=FileSystem.get(config);

      boolean flag=fs.delete(new Path(dstPath));

      fs.close();

      return flag;

     }

     /**

      * 读取文件

      * @param dstPath

      * @return

      * @throws IOException

      */

     public static byte[] readHdfsFile(String dstPaththrows IOException{

      Configuration config=new Configuration();

      config.set("fs.default.name""hdfs://192.168.10.222:9000");

      FileSystem fs=FileSystem.get(config);

      Path path=new Path(dstPath);

      long strings = 0;

      byte[] buffer = null;

      //判断文件是否存在

      if(fs.exists(path)){

      FSDataInputStream is=fs.open(path);

      //获取文件信息

      FileStatus status=fs.getFileLinkStatus(path);

      //创建缓冲数组

      //先转为string,在转为int

      System.out.println("开始读取文件");

      strings=status.getLen();

      buffer=new byte[Integer.parseInt(String.valueOf(status.getLen()))];

      //开始读取,使用readFully

      is.readFully(0,buffer);

      is.close();

      fs.close();

      }

      //System.out.println("**读取");

      //System.out.println("读取出的"+buffer.toString());

      System.out.println(strings);

      return buffer;

     }

     /**

      * 创建目录

     * @throws IOException 

      */

     public static void mkdir(String dstPaththrows IOException{

      Configuration config=new Configuration();

      config.set("fs.default.name""hdfs://192.168.10.222:9000");

      FileSystem fs=FileSystem.get(config);

      fs.mkdirs(new Path(dstPath));

      fs.close();

     }

     /**

      * 读取某个目录下所有文件

      * 

      */

     public static void listAllFile(String dstPath){

      Configuration config=new Configuration();

      config.set("fs.default.name""hdfs://192.168.10.222:9000");

      try {

FileSystem fs=FileSystem.get(config);

//filestatus包含文件所有的信息,不仅仅时状态

FileStatus[] status=fs.listStatus(new Path(dstPath));

//遍历

for(int i=0;i<status.length;i++){

if (status[i].isFile()) {

System.out.println(status[i].getPath()+"是一个文件");

}if (status[i].isDirectory()) {

String dirPath=status[i].getPath().toString();

listAllFile(dirPath);

}

}

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

      

     }

     

     //删除hadoop-2.4.0目录下的所有文件

     public static boolean  deleteAllFile(){

return false;

      

     }

     public static void main(String[] argsthrows IOException {

//mkdir("/testyf2");

     // deleteHdfsFile("/testyf2");

//listAllFile("/yangfei");

     // createAddFile("/testyangfei", "杨飞");

      //createAddFile("/yangfei/testyf3.txt", "15154192519841");

      //readHdfsFile("/testyangfei");

     // readHdfsFile("test2");

    // uploadLocalFile2Hdfs("/C:/Users/Administrator/Desktop/tools/hadoop-2.4.0", "/yangfei");

     uploadhdfsFile2local("/shengwei/shen.txt""/D:");

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值