新建Java Project;
1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar);
2,做一下项目关联,关联之前用eclipse看源码的那个项目,这样没什么其他的作用,就是为了要看源码,可以直


接点过来。 右击项目,属性,Java Build Path,Projects,Add 选择看之前看源码的项目;
3,用Java对HDFS进行操作:新建好类,
在shell里新建一个文本,拷贝到hadoop根目录下
[root@hadoop Downloads]# vi hello (在里面写点内容)
[root@hadoop Downloads]# hadoop fs -put hello / (将文件上传到hadoop根目录)

private static final String HDFS_PATH = "hdfs://hadoop:9000/hello";
	public static void main(String[] args) throws Exception {
		URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
		URL url = new URL(HDFS_PATH);
		InputStream in = url.openStream();
		/**
		 * @param in 输入流
		 * @param out 输出流
		 * @param buffSize 缓冲区大小
		 * @param close 是否关闭流
		 */
		IOUtils.copyBytes(in, System.out, 1024, true);
	}



3.1使用FileSystem操作:

public class Test2 {
	private static final String HDFS_PATH = "hdfs://hadoop:9000";
	public static void main(String[] args) throws Exception {
		FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH), new Configuration());
		//创建文件夹
		//makeDirectory(fileSystem);
		//上传文件
		//uploadData(fileSystem);
		//下载文件
		//downData(fileSystem);
		//删除文件(夹)
		deleteFile(fileSystem);

	}

	private static void deleteFile(FileSystem fileSystem) throws IOException {
		fileSystem.delete(new Path("/d12/f1"), true);
	}

	private static void downData(FileSystem fileSystem) throws IOException {
		FSDataInputStream in = fileSystem.open(new Path("/d12/f1"));
		IOUtils.copyBytes(in, System.out, 1024, true);
	}

	private static void uploadData(FileSystem fileSystem) throws IOException, 

FileNotFoundException {
		FSDataOutputStream out = fileSystem.create(new Path("/d12/f1"));
		//在F盘下新建一个log.txt
		FileInputStream in = new FileInputStream("f:/log.txt");
		//将F:/log.txt上传到 "/d12/f1"
		IOUtils.copyBytes(in, out, 1024, true);
	}

	private static void makeDirectory(FileSystem fileSystem) throws IOException {
		fileSystem.mkdirs(new Path("/d12")); //没/默认上传到User/用户目录下,有"/"上传到根

目录
	}
}