使用封装好的方法上传文件

package week01;

import java.io.FileInputStream;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;

public class HdfsUtil {

	
	FileSystem fs = null;
	
	@Before
	public void init() throws IOException, InterruptedException, URISyntaxException{
		
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://week01:9000");
		
		//fs = FileSystem.get(conf);
		fs = FileSystem.get(new URI("hdfs://week01:9000"), conf, "hadoop");

	}
	
	/**
	 * 使用封装好的方法  上传文件
	 * @throws IllegalArgumentException
	 * @throws IOException
	 */
	@Test
	public void upload2() throws IllegalArgumentException, IOException{
		fs.copyFromLocalFile(new Path("c:/test.txt"), new Path("hdfs://week01:9000/aa/test2.txt"));
		
	}
	

}

你可能感兴趣的:(Hadoop)