Hadoop HDFS 的 FileSystemAPI 操作文件
代码如下:
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class FileSystemDemo1 {
public static void main(String[] args)throws
IOException,URISyntaxException{
//本地文件路径
String source
="/home/wu/hadoop_file/hadoop_music_file/music3.txt";
//hdfs
文件路径
String dest="hdfs://192.168.128.130:9000/user/hadoop/music/";
copyFromLocal(source,dest);
}
public static void copyFromLocal(String source,String dest)throws
IOException,URISyntaxException{
//读取 hadoop
文件系统的配置
Configuration conf = new Configuration();
URI uri = new URI("hdfs://192.168.128.130:9000");
//FileSystem 是用户操作 HDFS 的核心类,他获得 URI 对应的 HDFS 文件系统
FileSystem fileSystem=FileSystem.get(uri, conf);
//源文件路径
Path srcPath = new Path(source);
//目的路径
Path dstPath = new Path(dest);
//查看目录路径是否存在
if(!(fileSystem.exists(dstPath))){
//如果路径不存在,即刻创建
fileSystem.mkdirs(dstPath);
}
//得到本地文件名称
String filename = source.substring(source.lastIndexOf('/')
+1,source.length());
try{
评论5