hdfs测试
今天做了一个hdfs测试,就是把文件上传到hdfs
直接放源码吧:
1.mainclass.java
package org.example; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class MainClass { public static void main(String[] args) throws IOException { Configuration conf=new Configuration(); System.setProperty("HADOOP_USER_NAME", "root"); conf.set("fs.defaultFS", "hdfs://node1.itcast.cn:8020"); Path inFile =new Path("y/hdfstest1.txt"); FileSystem hdfs=FileSystem.get(conf); FSDataOutputStream outputStream=hdfs.create(inFile); outputStream.writeUTF("信2005-2班20204186叶佳旺HDFS课堂测试"); outputStream.flush(); outputStream.close(); } }
2.read.java
package org.example; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class Read { public static void main(String[] args) throws IOException { Configuration conf=new Configuration(); System.setProperty("HADOOP_USER_NAME", "root"); conf.set("fs.defaultFS", "hdfs://node1.itcast.cn:8020"); Path inFile =new Path("/user/root/y/hdfstest1.txt"); Path newFile =new Path("/user/root/y/hdfstest2.txt"); FileSystem hdfs=FileSystem.get(conf); FSDataOutputStream outputStream=hdfs.create(newFile); FSDataInputStream inputStream=hdfs.open(inFile); String str = inputStream.readUTF(); inputStream.close(); outputStream.writeUTF(str); outputStream.flush(); outputStream.close(); inputStream=hdfs.open(newFile); System.out.println(inputStream.readUTF()); } }