使用DFSClient实现 hadoop上传文件功能,采用使用输入输出流实现 Hadoop 版本2.7.0

11 篇文章 0 订阅
10 篇文章 0 订阅

Hadoop 版本2.7.0

使用DFSClient实现hadoop上传文件功能,采用使用输入输出流实现。


另外说明:代码运行环境中需要配置 /etc/hosts

添加

10.11.12.45      master

否则无法识别 hdfs://master:9000 报错!


下面是代码:

package com.feng.test.hdfs;



import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.hdfs.DFSClient;


public class UploadFile {


/**
* @param args
*/
public static void main(String[] args) {
String hostname = "master";
int port = 9000;

//目标文件路径
String destFile = "/feng/83.txt";
//原文件路径
String fsrc = "/Users/feng/Development/hadoopworkspace/TestHadoop/files/83.txt";

FileInputStream fis = null;
OutputStream outputStream = null;
DFSClient client = null;
try{
//构造 Configuration
Configuration config = new Configuration();
//程序配置
config.set("hadoop.job.ugi", "feng,111111");
config.set("fs.default.name", "hdfs://master:9000");
//config.set("hadoop.tmp.dir", "/tmp/hadoop-yshengClient");
config.set("dfs.replication", "1");
config.set("mapred.job.tracker", "master:9001");

//构造DFClient
InetSocketAddress isa = new InetSocketAddress(hostname, port);
client = new DFSClient(isa, config);

//获取输出流
boolean isWrite = true;
outputStream = client.create(destFile, isWrite);

            File file = new File(fsrc);
            if(file.exists()){
            fis = new FileInputStream(file);
            byte[] buff = new byte[1024];
int readCount = 0;
readCount = fis.read(buff);
while (readCount != -1) {
outputStream.write(buff, 0, readCount);
readCount = fis.read(buff);
}
            }

            outputStream.flush();
System.out.println("success end!");
}catch(Exception e){
e.printStackTrace();
}finally{
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(client != null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值