zoukankan      html  css  js  c++  java
  • Hadoop Api 基本操作

     hadoop环境配置好后,直接可以在window上进行调试。话不多说,直接上源码。

    package cn.terry;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URI;
    import java.net.URISyntaxException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IOUtils;
    import org.junit.Before;
    import org.junit.Test;
    public class hdfsdemo {
      FileSystem fs=null;
      
      @Before
      public void init() throws IOException, InterruptedException, URISyntaxException
      {
    fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration(), "root");
      }
      //upload
      @Test
      public void upload() throws IllegalArgumentException, IOException
      {
      InputStream in= new FileInputStream("c:/ab.txt");
      OutputStream out= fs.create(new Path("/ab.txt"));
      IOUtils.copyBytes(in, out, 4096,true);
      }
      
      @Test
      public void makeDirectory() throws IllegalArgumentException, IOException
      {
    boolean ret=  fs.mkdirs(new Path("/code/a"));
      System.out.print(ret);
      }
      
      @Test
      public void testDelete() throws IOException
      {
    @SuppressWarnings("deprecation")
    boolean ret=  fs.delete(new Path("/code/a"),true);
    System.out.print(ret);
      }
    /**
    * @param args
    * @throws URISyntaxException 
    * @throws IOException 
    */
    public static void main(String[] args) throws IOException, URISyntaxException {
    // TODO Auto-generated method stub
    //download
      FileSystem fs=FileSystem.get(new URI("hdfs://master:9000"),new Configuration());
      InputStream in= fs.open(new Path("/input/a.txt"));
      OutputStream out =new FileOutputStream("c:/ab.txt");
      IOUtils.copyBytes(in, out, 4096,true);
      
      
    }
    }
  • 相关阅读:
    SE知识整理——泛型
    IDEA 运行 SpringMVC 项目分发控制器出现404解决方案。
    快速幂/欧拉降幂
    Leetcode(双指针专题)
    剑指offer
    ns3参考
    网络知识1:最后一公里/WiMax / 4G
    备份2
    shell入门
    ns3_gdb:协议里的函数是怎么被调用的
  • 原文地址:https://www.cnblogs.com/abcdwxc/p/7800141.html
Copyright © 2011-2022 走看看