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);
  
  
}
}
上一篇:Django中的信号及其用法


下一篇:【Python】 多线程并发threading & 任务队列Queue