关于hadoop中HDFS与本地文件系统的一些操作
FileSystem是HDFS文件系统,LocalFileSystem是本地文件系统
LocalFileSystem local = FileSystem.getLocal(conf);
对FileSystem进行读写操作 文件在本地有HDFS文件系统之间进行copy
注意:在windows上使用FileOutputStream创建文件,FileInputStream读取,
而在HDFS中,用FileSystem.create(path)进行创建,用FileSystem.open(path)读取文件
//将HDFS的文件合并后copy到本地文件系统
private static void copyFiles2Local() throws IOException,
URISyntaxException, FileNotFoundException
{
String hdfs = "hdfs://hadoop:9000/merg";
String local = "/root/file";
Configuration conf = new Configuration();
FileSystem filesystem = FileSystem.get(new URI(hdfs),conf);
FileOutputStream out = new FileOutputStream(local);
//获取HDFS路径下的文件集合
FileStatus[] listStatus = filesystem.listStatus(new Path(hdfs));
for(FileStatus fs:listStatus){
//fs.getPath()获取文件路径 读取文件
FSDataInputStream in = filesystem.open(fs.getPath());
//将输入流输出到同一输出流中,进行文件的合并
IOUtils.copyBytes(in,out,4096,false);
in.close();
}
out.close();
}
//将hdfs上的文件copy到本地文件中(单个文件)
private static void CopyHfile2Local(String[] args) throws IOException,
FileNotFoundException
{
String hdfs=args[0];
String local=args[1];
// String file="hdfs://hadoop:9000/web.log";//hdfs文件 地址
Configuration config=new Configuration();
FileSystem fs=FileSystem.get(URI.create(hdfs),config);//构建FileSystem
InputStream is=fs.open(new Path(hdfs));//读取文件
IOUtils.copyBytes(is, new FileOutputStream(new File(local)),4096, true);
//保存到本地 最后 关闭输入输出流
IOUtils.closeStream(is);
}
//将本地文件读取到HDFS上
private static void local2Hdfs() throws IOException,
FileNotFoundException
{
String path = "/root/file/log.txt";
String des = "hdfs://hadoop:9000/syslog";
Configuration conf = new Configuration();
FileSystem filesystem = FileSystem.get(URI.create(des),conf);
InputStream in = new FSDataInputStream(path);
//FSDataOutputStream out = fileSystem.create(new Path(des));
process表示记录是否已经被处理过
OutputStream out=fileSystem.create(new Path(des), new Progressable() {
@Override
public void progress() {
System.out.println(".");
}
});
IOUtils.copyBytes(in, out, 4096, false);
IOUtils.closeStream(out);
IOUtils.closeStream(in);
}
//读取hdfs 上的文件内容
private static void write4HdfsFile() throws IOException,
URISyntaxException, Exception
{
String path = "hdfs://hadoop:9000/http.dat";
Configuration conf = new Configuration();
FileSystem filesystem = FileSystem.get(new URI(path),conf);
InputStream in = null;
try{
//读取指定路径文件
in = filesystem.open(new Path(path));
//将输入流in中的内容写到输出流out中
IOUtils.copyBytes(in,System.out,conf,false);
}catch(Exception e){
throw new Exception(e);
}finally{
IOUtils.closeStream(in);
}
}
FileSystem是HDFS文件系统,LocalFileSystem是本地文件系统
LocalFileSystem local = FileSystem.getLocal(conf);
对FileSystem进行读写操作 文件在本地有HDFS文件系统之间进行copy
注意:在windows上使用FileOutputStream创建文件,FileInputStream读取,
而在HDFS中,用FileSystem.create(path)进行创建,用FileSystem.open(path)读取文件
//将HDFS的文件合并后copy到本地文件系统
private static void copyFiles2Local() throws IOException,
URISyntaxException, FileNotFoundException
{
String hdfs = "hdfs://hadoop:9000/merg";
String local = "/root/file";
Configuration conf = new Configuration();
FileSystem filesystem = FileSystem.get(new URI(hdfs),conf);
FileOutputStream out = new FileOutputStream(local);
//获取HDFS路径下的文件集合
FileStatus[] listStatus = filesystem.listStatus(new Path(hdfs));
for(FileStatus fs:listStatus){
//fs.getPath()获取文件路径 读取文件
FSDataInputStream in = filesystem.open(fs.getPath());
//将输入流输出到同一输出流中,进行文件的合并
IOUtils.copyBytes(in,out,4096,false);
in.close();
}
out.close();
}
//将hdfs上的文件copy到本地文件中(单个文件)
private static void CopyHfile2Local(String[] args) throws IOException,
FileNotFoundException
{
String hdfs=args[0];
String local=args[1];
// String file="hdfs://hadoop:9000/web.log";//hdfs文件 地址
Configuration config=new Configuration();
FileSystem fs=FileSystem.get(URI.create(hdfs),config);//构建FileSystem
InputStream is=fs.open(new Path(hdfs));//读取文件
IOUtils.copyBytes(is, new FileOutputStream(new File(local)),4096, true);
//保存到本地 最后 关闭输入输出流
IOUtils.closeStream(is);
}
//将本地文件读取到HDFS上
private static void local2Hdfs() throws IOException,
FileNotFoundException
{
String path = "/root/file/log.txt";
String des = "hdfs://hadoop:9000/syslog";
Configuration conf = new Configuration();
FileSystem filesystem = FileSystem.get(URI.create(des),conf);
InputStream in = new FSDataInputStream(path);
//FSDataOutputStream out = fileSystem.create(new Path(des));
process表示记录是否已经被处理过
OutputStream out=fileSystem.create(new Path(des), new Progressable() {
@Override
public void progress() {
System.out.println(".");
}
});
IOUtils.copyBytes(in, out, 4096, false);
IOUtils.closeStream(out);
IOUtils.closeStream(in);
}
//读取hdfs 上的文件内容
private static void write4HdfsFile() throws IOException,
URISyntaxException, Exception
{
String path = "hdfs://hadoop:9000/http.dat";
Configuration conf = new Configuration();
FileSystem filesystem = FileSystem.get(new URI(path),conf);
InputStream in = null;
try{
//读取指定路径文件
in = filesystem.open(new Path(path));
//将输入流in中的内容写到输出流out中
IOUtils.copyBytes(in,System.out,conf,false);
}catch(Exception e){
throw new Exception(e);
}finally{
IOUtils.closeStream(in);
}
}