java操作hdfs读写

HDFS读写操作

public class HdfsOp {

public static void main(String[] args) throws Exception {

hdfsReadFile("hdfs://192.168.153.133:9000/",

"hdfs://192.168.153.133:9000/spark/sparktmp.txt" );

hdfsWriteFile("out/ww.txt","hdfs://chen133:9000/",

"hdfs://192.168.153.133:9000/spark/world.txt");

}

public static void hdfsReadFile(

String hdfsUrl,String hdfsFile) throws Exception {

Configuration cfg=new Configuration();

//"fs.defaultFS"获取配置文件中设置的hdfs端口

cfg.set("fs.defaultFS",hdfsUrl);

//获取文件

FileSystem fs=FileSystem.get(cfg);

if (!fs.exists(new Path(hdfsFile))){

throw new Exception("要读取的文件不存在!");

}

//调用ApI操作,读取文件

FSDataInputStream fis=fs.open(new Path(hdfsFile));

//文件写入到本地

FileOutputStream fos = new FileOutputStream(fileName);

byte[] buffer=new byte[2048];

int count=fis.read(buffer,0,2048);

while(count>0){//数据不足时,会继续写入补null,但在记事本中看不到

fos.write(buffer,0,2048);

count=fis.read(buffer,0,2048);

}

//IOUtils.copyBytes(fis,System.out,1024);//读取文件到控制台

fs.close();

}

public static void hdfsWriteFile(

String fileName, String hdfsUrl,String hdfsFile) throws Exception {

Configuration cfg =new Configuration();

cfg.set("fs.defaultFS",hdfsUrl);

FileSystem fs = FileSystem.get(cfg);

if (fs.exists(new Path(hdfsFile))){

throw new Exception("文件已存在!");

}

FSDataOutputStream fos = fs.create(new Path(hdfsFile));//创建文件

//将内容直接写入文件

//fos.write("你好啊,这世界".getBytes(StandardCharsets.UTF_8));

//读取本地文件上传到hdfs

FileInputStream fis=new FileInputStream(fileName);

byte[] buffer=new byte[2048];

int count=fis.read(buffer,0,2048);

while(count>0){

fos.write(buffer,0,2048);

count=fis.read(buffer,0,2048);

}

//flush()是强行将缓冲区中的内容输出,否则直到缓冲区满后才会一次性的将内容输出

//这里对这些使用不全面,稍后补充

fos.flush();

//关闭流

fs.close();

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

s_schen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值