本地多级文件 原样上传到hdfs
2016-09-13 14:21
288 查看
package com.hdfs;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocalFileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import com.beicai.utils.MyUtils;
/**
*
* @described 本地多级文件 原样上传到hdfs
*/
public class HdfsWork {
public static void main(String[] args) throws Exception {
myMerge();
System.out.println("ok");
}
public static void myWrite(Path path,LocalFileSystem lfs,FileSystem fs) throws Exception{
FileStatus[] fst = lfs.listStatus(path);
for(int i=0;i<fst.length;i++){
Path dir = fst[i].getPath();//获取路径
//file:/D:/data/a
dir = new Path(dir.toString().split(":")[2]);//截取相对本地磁盘D盘的绝对路径
if(fst[i].isDirectory()){//是文件夹
myWrite(fst[i].getPath(),lfs,fs);//调用本身
fs.mkdirs(dir);//创建里面没有文件的文件夹(空文件夹)
} else {
FSDataInputStream fsdis = lfs.open(fst[i].getPath()); //打开文件输入流
FSDataOutputStream fsdos = fs.create(dir);//打开文件输出流流
int read = 0;
byte[] buffer = new byte[255];
while((read=fsdis.read(buffer))>0){
fsdos.write(buffer, 0, read);
}
IOUtils.closeStream(fsdis);//关闭流
IOUtils.closeStream(fsdos);
}
}
}
public static void myMerge() throws Exception{
FileSystem fs = MyUtils.getFileSystem();
LocalFileSystem lfs = MyUtils.getLocalFileSystem();
Path localPath = new Path("D:/datas");//本地路径
myWrite(localPath,lfs,fs);//调用方法
}
}
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocalFileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import com.beicai.utils.MyUtils;
/**
*
* @described 本地多级文件 原样上传到hdfs
*/
public class HdfsWork {
public static void main(String[] args) throws Exception {
myMerge();
System.out.println("ok");
}
public static void myWrite(Path path,LocalFileSystem lfs,FileSystem fs) throws Exception{
FileStatus[] fst = lfs.listStatus(path);
for(int i=0;i<fst.length;i++){
Path dir = fst[i].getPath();//获取路径
//file:/D:/data/a
dir = new Path(dir.toString().split(":")[2]);//截取相对本地磁盘D盘的绝对路径
if(fst[i].isDirectory()){//是文件夹
myWrite(fst[i].getPath(),lfs,fs);//调用本身
fs.mkdirs(dir);//创建里面没有文件的文件夹(空文件夹)
} else {
FSDataInputStream fsdis = lfs.open(fst[i].getPath()); //打开文件输入流
FSDataOutputStream fsdos = fs.create(dir);//打开文件输出流流
int read = 0;
byte[] buffer = new byte[255];
while((read=fsdis.read(buffer))>0){
fsdos.write(buffer, 0, read);
}
IOUtils.closeStream(fsdis);//关闭流
IOUtils.closeStream(fsdos);
}
}
}
public static void myMerge() throws Exception{
FileSystem fs = MyUtils.getFileSystem();
LocalFileSystem lfs = MyUtils.getLocalFileSystem();
Path localPath = new Path("D:/datas");//本地路径
myWrite(localPath,lfs,fs);//调用方法
}
}
相关文章推荐
- 本地多级文件 合并上传到hdfs(递归上传)
- 向hdfs上传本地文件-Hello World
- 上传本地文件到HDFS
- 上传本地文件到HDFS spark可以直接使用
- talend 将本地文件或者mysql文件上传到hadoop/hdfs
- 利用java API实现本地文件上传至hdfs
- spark 本地文件上传hdfs
- Hadoop HDFS编程 API入门系列之从本地上传文件到HDFS(一)
- hdfs有多级目录 合并文件下载到本地(递归下载)
- Spark之本地文件上传至HDFS
- Hadoop——上传本地文件到hdfs
- hadoop生态系统学习之路(三)java实现上传文件(本地或ftp)至hdfs
- 解决从本地文件系统上传到HDFS时的权限问题
- 上传本地文件到hdfs
- JAVA实现:将文件从本地上传到HDFS上、从HDFS上读取等操作
- 上传本地文件到HDFS
- 合并本地文件并上传到hdfs
- 把本地文件夹下的所有文件上传到hdfs上并合并成一个文件
- java上传本地文件到HDFS简单demo
- JAVA实现批量上传本地文件至HDFS