如何用java程序把本地java读取hdfs文件内容拷贝到hdfs上并显示

查看: 6654|回复: 6
JAVA 如何上传本地文件到HDFS????
主题听众收听
高级会员, 积分 1030, 距离下一级还需 3970 积分
高级会员, 积分 1030, 距离下一级还需 3970 积分
JAVA 如何上传本地文件到HDFS???? 谁可以提供个例子
主题听众收听
中级会员, 积分 308, 距离下一级还需 692 积分
中级会员, 积分 308, 距离下一级还需 692 积分
呵呵,客户端用jsp上传文件.tomcat做服务器,用一个简单的 servlet就可以了
主题听众收听
中级会员, 积分 424, 距离下一级还需 576 积分
中级会员, 积分 424, 距离下一级还需 576 积分
两片文章确实不错,但是其中的文件操作都是基于hadoop环境的,如果一台机器没有hadoop的各种jar包,如何使用 ...
jfttcjl 发表于
& & 用java搭个客户端,这样就可以了。是不是还需要tomcat呢。这个我就还没有专门了解~~
主题听众收听
中级会员, 积分 339, 距离下一级还需 661 积分
中级会员, 积分 339, 距离下一级还需 661 积分
两片文章确实不错,但是其中的文件操作都是基于hadoop环境的,如果一台机器没有hadoop的各种jar包,如何使用hadoop的api呢,如何实现文件的上传下载呢,是不是非要搭建一个第三方的服务来起到一个中转的作用呢,如果是,那么是不是必须搭载在服务器的其中一个节点上呢吗?吗?请指点···················
主题听众收听
高级会员, 积分 3888, 距离下一级还需 1112 积分
高级会员, 积分 3888, 距离下一级还需 1112 积分
这两篇都很有用,谢谢:)
主题听众收听
中级会员, 积分 424, 距离下一级还需 576 积分
中级会员, 积分 424, 距离下一级还需 576 积分
这个应该对你有用
主题听众收听
中级会员, 积分 284, 距离下一级还需 716 积分
中级会员, 积分 284, 距离下一级还需 716 积分
用数据流,
您需要登录后才可以回帖
回帖并转播
积极上进,爱好学习
经常参与各类话题的讨论,发帖内容较有主见
经常帮助其他会员答疑
站长推荐 /4
会员注册不成功的原因
新手获取积分方法
hadoop3.0学习:零基础安装部署hadoop集群
about云课程:大数据日志实时分析
Powered by4.9k 人阅读
标签:至少1个,最多5个
之前实现了使用流来讲http和ftp的文件下载到本地,也实现了将本地文件上传到hdfs上,那现在就可以做到将
ftp和http的文件转移到hdfs上了,而不用先将ftp和http的文件拷贝到本地再上传到hdfs上了。其实这个东西的原理
很简单,就是使用流,将ftp或http的文件读入到流中,然后将流中的内容传送到hdfs上,这样子就不用让数据存到
本地的硬盘上了,只是让内存来完成这个转移的过程,希望这个工具,能够帮到有这样需求的同学~
这里先附上之前的几个工具的链接:
代码如下:
import java.io.InputS
import java.io.OutputS
import java.io.IOE
public class FileTrans {
private String head = "";
private String hostname = "";
private String FilePath = "";
private String hdfsFilePath = "";
private HDFSUtil hdfsutil =
private FtpC
private HttpU
public void setFilePath(String FilePath){
this.FilePath = FileP
public String getFilePath(String FilePath){
return this.FileP
public void sethdfsFilePath(String hdfsFilePath){
this.hdfsFilePath = hdfsFileP
public String gethdfsFilePath(String hdfsFilePath){
return this.hdfsFileP
public void setHostName(String hostname){
this.hostname =
public String getHostName(){
return this.
public void setHead(String head){
this.head =
public String getHead(){
return this.
public FileTrans(String head, String hostname, String filepath, String hdfsnode,String hdfsFilepath){
this.head =
this.hostname =
this.FilePath =
this.hdfsFilePath = hdfsF
if (head.equals("ftp") && hostname != ""){
this.ftp = new FtpClient(this.hostname);
if ((head.equals("http") || head .equals("https")) && hostname != ""){
String httpurl = head + "://" + hostname + "/" +
this.http = new HttpUtil(httpurl);
if (hdfsnode != ""){
this.hdfsutil = new HDFSUtil(hdfsnode);
this.hdfsutil.setHdfsPath(this.hdfsFilePath);
this.hdfsutil.setFilePath(hdfsutil.getHdfsNode()+hdfsutil.getHdfsPath());
this.hdfsutil.setHadoopSite("./hadoop-site.xml");
this.hdfsutil.setHadoopDefault("./hadoop-default.xml");
this.hdfsutil.setConfigure(false);
public static void main(String[] args) throws IOException{
String head = "";
String hostname = "";
String filepath = "";
String hdfsfilepath = "";
String hdfsnode = "";
String localpath = "";
InputStream inStream =
int samplelines = 0;
head = args[0];
//远端服务器类型,http还是ftp
hostname = args[1];
//远端服务器hostname
filepath = args[2];
//远端文件路径
hdfsnode = args[3];
//hdfs的机器名,不带hdfs开头
hdfsfilepath = args[4];
//hdfs的文件路径
localpath = args[5];
//如果需要在本地保存一份的话,输入本地的路径,不保存,传入空格或者samplelines传入0
samplelines = Integer.parseInt(args[6]);
//保存在本地的话,保存前N行,如果不保存,填0
}catch (Exception e){
System.out.println("[FileTrans]:input args error!");
e.printStackTrace();
FileTrans filetrans = new FileTrans(head, hostname, filepath, hdfsnode,hdfsfilepath);
if (filetrans == null){
System.out.println("filetrans null");
if (filetrans.ftp == null && head.equals("ftp")){
System.out.println("filetrans ftp null");
if (filetrans.http == null && (head.equals("http") || head.equals("https"))){
System.out.println("filetrans ftp null");
if (head.equals("ftp")){
inStream = filetrans.ftp.getStream(filepath);
if (samplelines & 0){
filetrans.ftp.writeStream(inStream, localpath, samplelines);
inStream = filetrans.http.getStream(head + "://" + hostname + "/" + filepath);
if (samplelines & 0){
filetrans.http.downLoad(head + "://" + hostname + "/" + filepath, localpath, samplelines);
filetrans.hdfsutil.upLoad(inStream, filetrans.hdfsutil.getFilePath());
if (head == "ftp"){
filetrans.ftp.disconnect();
}catch (IOException e){
System.out.println("[FileTrans]: file trans failed!");
e.printStackTrace();
System.out.println("[FileTrans]: file trans success!");
编译有问题的话,在hadoop工具的那篇文章中有提到,可以参考
注:最好将其他三个工具的文件放在同一个目录下,如果不放在一起,那么请自行引用
这个工具既可以将ftp或者http转移到hdfs,也能将前N行保存到本地,进行分析
1 收藏&&|&&0
你可能感兴趣的文章
分享到微博?
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。下次自动登录
现在的位置:
& 综合 & 正文
将HDFS上的文件拷贝到本地的实现
这里需要进行一点额外的工作才能使得URL识别hdfs的uri。我们要使用java.net.URL的 setURLStreamHandlerFactory()方法设置URLStreamHandlerFactory,这里需要传递一个 FsUrlStreamHandlerFactory。这个操作对一个jvm只能使用一次,我们可以在静态块中调用。
publicclass FIleSystemCat {
* @param args
* @throws IOException */static {//这句是为了让程序识别hdfs协议而进行的设置//setURLStreamHandlerFactory:设置url流处理器工厂
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}publicstaticvoid main(String[] args) throws IOException {// TODO Auto-generated method stub
String path1="hdfs://localhost:9000/user/hadoop/photo/DSC01283.JPG";
String path2="/home/hadoop/qiu.jpg";
InputStream in=null;
OutputStream out=null;
in=new URL(path1).openStream();
out=new FileOutputStream(path2);
IOUtils.copyBytes(in, out, 4096, false);
}在某些情况下设置URLStreamHandlerFactory的方式并不一定回生效。在这种情况下,需要用FileSystem API来打开一个文件的输入流。 文件的位置是使用Hadoop Path呈现在Hadoop中的,与java.io中的不一样。 有两种方式获取FileSystem的实例: public static FileSystem get(Configuration conf) throws IOException public static FileSystem get(URI uri, Configuration conf) throws IOException Configuration封装了client或者server的配置,这些配置从classpath中读取,比如被classpath指向的conf/core-site.xml文件. 第一个方法从默认位置(conf/core-site.xml)读取配置,第二个方法根据传入的uri查找适合的配置文件,若找不到则返回使用第一个方法,即从默认位置读取。
在获得FileSystem实例之后,我们可以调用open()方法来打开输入流: public FSDataInputStream open(Path f) throws IOException public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException 第一个方法的参数f是文件位置,第二个方法的bufferSize就是输入流的缓冲大小。 下面的代码是使用FileSystem打开输入流的示例:
public class FileSystemDoubleCat {
public static void main(String[] args) throws Exception {
String uri = args[0];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
FSDataInputStream in =
in = fs.open(new Path(uri));
IOUtils.copyBytes(in, System.out, 4096, false);
in.seek(0); // go back to the start of the file
IOUtils.copyBytes(in, System.out, 4096, false);
} finally {
IOUtils.closeStream(in);
【上篇】【下篇】您要找的页面不存在 - 简书
您要找的页面不存在
可能是因为您的链接地址有误、该文章已经被作者删除或转为私密状态。JAVA实现批量上传本地文件至HDFS
JAVA实现批量上传本地文件至HDFS
小白一枚,本文简单实现了批量上传遥感影像至HDFS,所以没有实现窗体简单的关闭、缩小、取消等功能。重申这只是简单demo!话不多说直接上代码。
@MarcusPlus
package com.
import java.awt.event.ActionE
import java.awt.event.ActionL
import java.net.URI;
import javax.swing.JB
import javax.swing.JFileC
import javax.swing.JF
import javax.swing.JP
import org.apache.hadoop.conf.C
import org.apache.hadoop.fs.FileS
import org.apache.hadoop.fs.FileS
import org.apache.hadoop.fs.FileU
import org.apache.hadoop.fs.P
public class Demo {
public static void main(String[] args) {
// 创建 JFrame 实例
JFrame frame = new JFrame(&Demo&);
// Setting the width and height of frame
frame.setSize(512, 512);
frame.setLocation(400, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* 创建面板,这个类似于 HTML 的 p 标签
* 我们可以创建多个面板并在 JFrame 中指定位置
* 面板中我们可以添加文本字段,按钮及其他。
JPanel panel = new JPanel();
// 添加面板
frame.add(panel);
* 调用用户定义的方法并添加组件到面板
placeComponents(panel);
// 设置界面可见
frame.setVisible(true);
private static void placeComponents(JPanel panel) {
* 这边设置布局为 null
panel.setLayout(null);
// 创建select按钮
JButton selectButton = new JButton(&Select Local File&);
selectButton.setBounds(10, 80, 80, 25);
panel.add(selectButton);
selectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFileChooser jf = new JFileChooser();
jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jf.setDialogTitle(&请选择要上传的文件夹...&);
jf.showDialog(null,null);
String srcPath=jf.getSelectedFile().getAbsolutePath()+&/*&;
String[] name=jf.getSelectedFile().getAbsolutePath().split(&/&);
String dstPath=&hdfs://127.0.0.1:9000/user/test/&+name[name.length-1]+&/&;
if(srcPath.isEmpty()){
System.out.println(&请选择本地路径!&);
listFile(srcPath, dstPath);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println(&Sucess!&);
protected static void listFile(String srcPath, String dstPath) throws Exception{
// TODO Auto-generated method stub
FileSystem fs =
FileSystem local =
//读取配置文件
Configuration conf=new Configuration();
//指定HDFS地址
URI uri=new URI(&hdfs://127.0.0.1:9000&);
fs=FileSystem.get(uri,conf);
// 获取本地文件
local=FileSystem.getLocal(conf);
//获取文件目录
FileStatus[] listFile=local.globStatus(new Path(srcPath), new RegxAcceptPathFilter(&^.*tif$&));
//获取文件路径
listPath=FileUtil.stat2Paths(listFile);
//输出文件路径
Path outPath=new Path(dstPath);
boolean result = fs.isDirectory(outPath);
if(result==true){
//循环遍历所有文件路径
for(Path p:listPath){fs.copyFromLocalFile(p, outPath);}
fs.mkdirs(outPath);
System.out.println(&创建路径: &+outPath);
for(Path p:listPath){fs.copyFromLocalFile(p, outPath);}
@MarcusPlus
package com.
import org.apache.hadoop.fs.P
import org.apache.hadoop.fs.PathF
public class RegxAcceptPathFilter implements PathFilter{
public RegxAcceptPathFilter(String regex) {
this.regex=
public boolean accept(Path path) {
boolean flag=path.toString().matches(regex);
RegxAcceptPathFilter是用来将本地文件夹下非.tif文件进行过滤掉,以便顺利建立遥感影像库。虽然界面很丑,但是毕竟是个简单demo,别吐槽我啦~

我要回帖

更多关于 java hdfs 上传文件 的文章

 

随机推荐