大三学习进度3
//java对hadoop操作示例
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 WriteFile {
public static void main(String[] args) throws IOException {
FileSystem fs = FileSystem.get(new URI("hdfs://linux1:9000"),
new Configuration(),"root");
Path path = new Path("/user/hadoop/data/write.txt");
FSDataOutputStream out = fs.create(path);
out.writeUTF("da jia hao,cai shi zhen de hao!");
fs.close();
}
}
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class GetAllChildFile {
static Configuration conf = new Configuration();
public static void main(String[] args)throws IOException {
FileSystem fs = FileSystem.get("hdfs://linux1:9000"),
new Configuration(),"root");
Path path = new Path("/user/hadoop");
getFile(path,fs);
//fs.close();
}
public static void getFile(Path path,FileSystem fs) throws IOException {
FileStatus[] fileStatus = fs.listStatus(path);
for(int i=0;i<fileStatus.length;i++){
if(fileStatus[i].isDir()){
Path p = new Path(fileStatus[i].getPath().toString());
getFile(p,fs);
}else{
System.out.println(fileStatus[i].getPath().toString());
}
}
}
}