Terry's blog

Focus on bigdata and cloud.

博客园 首页 新随笔 联系 订阅 管理
 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);
  
  
}
}
posted on 2017-11-07 17:40  王晓成  阅读(1322)  评论(0编辑  收藏  举报