hdfs-javaAPI

1.新建普通java项目
2.导入hadoop包
hdfs-javaAPI
导包步骤
hdfs-javaAPI
3.导入两个核心配置文件
hdfs-javaAPI
注意hdfs-site.xml文件中配置权限信息,否则一会容易发生权限不够,拒绝写入。
hdfs-javaAPI
4.编写从hdfs读取数据和上传数据

public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS","hdfs://master:9000");
		conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
		FileSystem fs = FileSystem.get(conf);
		Path file = new Path("/Demo/b.txt"); 
		FSDataInputStream open = fs.open(file);
		BufferedReader d = new BufferedReader(new InputStreamReader(open));
		String line = d.readLine();
		System.out.println(line);
		d.close();
		fs.close();
	}
public static void main(String[] args) throws IOException {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS","hdfs://master:9000");
		conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path("/Demo/b.txt");
		FSDataOutputStream append = fs.append(path);
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(append));
		writer.write("0320");
		writer.close();
		fs.close();
	}