手机
当前位置:查字典教程网 >编程开发 >Java >hadoop的hdfs文件操作实现上传文件到hdfs
hadoop的hdfs文件操作实现上传文件到hdfs
摘要:hdfs文件操作操作示例,包括上传文件到HDFS上、从HDFS上下载文件和删除HDFS上的文件,大家参考使用吧复制代码代码如下:import...

hdfs文件操作操作示例,包括上传文件到HDFS上、从HDFS上下载文件和删除HDFS上的文件,大家参考使用吧

复制代码 代码如下:

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.*;

import java.io.File;

import java.io.IOException;

public class HadoopFile {

private Configuration conf =null;

public HadoopFile(){

conf =new Configuration();

conf.addResource(new Path("/hadoop/etc/hadoop/core-site.xml"));

}

public HadoopFile(Configuration conf){

this.conf =conf;

}

public boolean sendFile(String path,String localfile){

File file=new File(localfile);

if (!file.isFile()) {

System.out.println(file.getName());

return false;

}

try {

FileSystem localFS =FileSystem.getLocal(conf);

FileSystem hadoopFS =FileSystem.get(conf);

Path hadPath=new Path(path);

FSDataOutputStream fsOut=hadoopFS.create(new Path(path+"/"+file.getName()));

FSDataInputStream fsIn=localFS.open(new Path(localfile));

byte[] buf =new byte[1024];

int readbytes=0;

while ((readbytes=fsIn.read(buf))>0){

fsOut.write(buf,0,readbytes);

}

fsIn.close();

fsOut.close();

FileStatus[] hadfiles= hadoopFS.listStatus(hadPath);

for(FileStatus fs :hadfiles){

System.out.println(fs.toString());

}

return true;

} catch (IOException e) {

e.printStackTrace();

}

return false;

}

public boolean delFile(String hadfile){

try {

FileSystem hadoopFS =FileSystem.get(conf);

Path hadPath=new Path(hadfile);

Path p=hadPath.getParent();

boolean rtnval= hadoopFS.delete(hadPath, true);

FileStatus[] hadfiles= hadoopFS.listStatus(p);

for(FileStatus fs :hadfiles){

System.out.println(fs.toString());

}

return rtnval;

} catch (IOException e) {

e.printStackTrace();

}

return false;

}

public boolean downloadFile(String hadfile,String localPath){

try {

FileSystem localFS =FileSystem.getLocal(conf);

FileSystem hadoopFS =FileSystem.get(conf);

Path hadPath=new Path(hadfile);

FSDataOutputStream fsOut=localFS.create(new Path(localPath+"/"+hadPath.getName()));

FSDataInputStream fsIn=hadoopFS.open(hadPath);

byte[] buf =new byte[1024];

int readbytes=0;

while ((readbytes=fsIn.read(buf))>0){

fsOut.write(buf,0,readbytes);

}

fsIn.close();

fsOut.close();

return true;

} catch (IOException e) {

e.printStackTrace();

}

return false;

}

}

【hadoop的hdfs文件操作实现上传文件到hdfs】相关文章:

Java Annotation(Java 注解)的实现代码

java中vector与hashtable操作实例分享

Java IO文件编码转换实现代码

java向文件末尾添加内容示例分享

java 实现文件复制和格式更改的实例

java list去重操作实现方式

Java用文件流下载网络文件示例代码

struts2中实现多个文件同时上传代码

java web项目实现文件下载实例代码

Java 的 FileFilter文件过滤与readline读行操作实例代码

精品推荐
分类导航