hadoop2.9 idea_idea上运行Hadoop

HDFSfile.java/************************************************************

Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.

FileName: HDFSfile.java

Author: Light

Version : version1.0

Date: 2018/7/16

Description:以通过hadoop中的fileSystem API进行文件的操作// 模块描述

Version: // 版本信息

实现了对hdfs文件的大部分操作

Function List: // 主要函数及其功能

1 创建目录mkdir("/idea/");

2.创建文件create("/idea/haha.txt");

3.查看hdfs文件内容read("/idea/text.txt");

4文件重命名moveFile("/idea/haha.txt","/idea/hello.txt");

5.上传文件putFile("G://text.txt","/idea/");

6.下载文件getFile("/idea/abc.txt","G://");

7.查询目录下的所有文件listStatus("/idea/");

8.删除文件deleteFile("/idea/hello.txt");

History:

// 历史修改记录

Light 18/7/16 1.0 build this moudle

***********************************************************/

importorg.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;import java.io.*;public classHDFSfile {

Configuration conf;

FileSystem filesystem;

String DEFNAME="fs.defaultFS";

String HDFSURL="hdfs://192.168.72.10:9000";

@Beforepublic void before() throwsIOException {

conf=newConfiguration();

conf.set(DEFNAME, HDFSURL);

filesystem=FileSystem.get(conf);

}/*** junit测试函数

*@throwsIOException*/@Testpublic void Text() throwsIOException {//创建目录//mkdir("/idea/");//创建文件//create("/idea/haha.txt");//查看hdfs文件内容//read("/idea/text.txt");//文件重命名//moveFile("/idea/haha.txt","/idea/hello.txt");//上传文件//putFile("G://text.txt","/idea/");//下载文件//getFile("/idea/abc.txt","G://");//查询目录下的所有文件//listStatus("/idea/");//删除文件//deleteFile("/idea/hello.txt");

}/*** 创建目录

*@parampath 创建目录的地址(例:/hadoop/)

*@throwsIOException*/

public void mkdir(String path) throwsIOException {//创建hdfs目录

if(filesystem.exists(newPath(path)))

{

System.out.println("目录已存在");

}else{boolean result=filesystem.mkdirs(newPath(path));

System.out.println(result);

}

}/*** 创建文件

*@parampath hdfs文件地址(例:/hadoop/abc.txt)

*@throwsIOException*/

public void create(String path) throwsIOException{//创建文件

if(filesystem.exists(newPath(path)))

{

System.out.println("文件已存在");

}else{

FSDataOutputStream outputStream= filesystem.create(newPath(path));

System.out.println("文件创建成功");

}

}/*** 查看文件内容

*@paramdst hdfs文件地址(例:/hadoop/abc.txt)

*@throwsIOException*/

public void read(String dst) throwsIOException {if(filesystem.exists(newPath(dst)))

{

FSDataInputStream inputstream=filesystem.open(newPath(dst));

InputStreamReader isr=newInputStreamReader(inputstream);

BufferedReader br=newBufferedReader(isr);

String str=br.readLine();while(str!=null){

System.out.println(str);

str=br.readLine();

}

br.close();

isr.close();

inputstream.close();

}else{

System.out.println("文件不存在");

}

}/*** 将dst1重命名为dst2,也可以进行文件的移动

*@paramoldpath 旧名

*@paramnewpath 新名*/

public voidmoveFile(String oldpath, String newpath) {

Path path1= newPath(oldpath);

Path path2= newPath(newpath);try{if (!filesystem.exists(path1)) {

System.out.println(oldpath+ " 文件不存在!");return;

}if(filesystem.exists(path2)) {

System.out.println(newpath+ "已存在!");return;

}//将文件进行重命名,可以起到移动文件的作用

filesystem.rename(path1, path2);

System.out.println("文件已重命名!");

}catch(IOException e) {

e.printStackTrace();

}

}/*** 上传文件到hdfs

*@paramlocal

*@paramdst*/

public voidputFile(String local, String dst) {try{//从本地将文件拷贝到HDFS中,如果目标文件已存在则进行覆盖

filesystem.copyFromLocalFile(new Path(local), newPath(dst));

System.out.println("上传成功!");//关闭连接

} catch(IOException e) {

System.out.println("上传失败!");

e.printStackTrace();

}

}/*** 下载文件到本地

*@paramdst

*@paramlocal*/

public voidgetFile(String dst, String local) {try{if (!filesystem.exists(newPath(dst))) {

System.out.println("文件不存在!");

}else{

filesystem.copyToLocalFile(new Path(dst), newPath(local));

System.out.println("下载成功!");

}

}catch(IOException e) {

System.out.println("下载失败!");

e.printStackTrace();

}

}/*** 显示目录下所有文件

*@paramdst*/

public voidlistStatus(String dst) {try{if (!filesystem.exists(newPath(dst))) {

System.out.println("目录不存在!");return;

}//得到文件的状态

FileStatus[] status = filesystem.listStatus(newPath(dst));for(FileStatus s : status) {

System.out.println(s.getPath().getName());

}

}catch (IllegalArgumentException |IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}/*** 删除hdfs中的文件

*@paramdst*/

public voiddeleteFile(String dst) {try{if (!filesystem.exists(newPath(dst))) {

System.out.println("文件不存在!");

}else{

filesystem.delete(new Path(dst), true);

System.out.println("删除成功!");

}

}catch(IOException e) {

System.out.println("删除失败!");

e.printStackTrace();

}

}/*** 关闭filesyatem*/@Afterpublic voiddestory()

{try{

filesystem.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值