我是靠谱客的博主 细心水壶,最近开发中收集的这篇文章主要介绍Hadoop工具类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package hadoop;

 

import java.io.IOException;

import java.net.URI;

import java.net.URISyntaxException;

import java.text.SimpleDateFormat;

import java.util.Date;

 

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.BlockLocation;

import org.apache.hadoop.fs.FSDataOutputStream;

import org.apache.hadoop.fs.FileStatus;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

/**

* hadoop 工具类

* 上传文件-----------upload(String ip,String srcPath,String desPath)

* 下载文件-----------download(String ip,String srcPath,String desPath )

* 遍历文件-----------list(String ip,String path)

* 删除文件-----------delete(String ip,String path,boolean boStr)

* 创建文件夹----------mkdir(String ip,String path)

* 创建并写入文件-------write(String txt,String ip,String path)

* 重命名文件----------rename(String ip,String path,String oldName,String newName)

* 判断文件是否存在------exists(String ip,String pathAndName);

* 查看文件在集群中的位置--fileLoc(String ip,String filePath)

* 文件最后一次修改时间---getTime(String ip,String path)

* @author Administrator

*

*/

public class Hadoop{

private static FileSystem fs;

public void upload(String ip,String srcPath,String desPath){

//创建一个Configuration

Configuration conf = new Configuration();

System.out.println("运行较慢,请耐心等待...");

//创建FileSystem对象

try {

fs = FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

//把本地磁盘中的文件上传到hdfs的某个目录下

try {

fs.copyFromLocalFile(false, new Path(srcPath), new Path(desPath));

System.out.println("运行结果 : 上传成功");

//关闭

fs.close();

} catch (IllegalArgumentException | IOException e) {

e.printStackTrace();

}

}

public void download(String ip,String srcPath,String desPath ){

Configuration conf = new Configuration();

System.out.println("运行较慢,请耐心等待...");

try {

fs = FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

try {

fs.copyToLocalFile(false, new Path(srcPath), new Path(desPath),true);

fs.close();

} catch (IllegalArgumentException | IOException e) {

e.printStackTrace();

}

}

public void list(String ip,String path){

Configuration conf = new Configuration();

FileStatus[] status=null;

System.out.println("运行结果 : 运行较慢,请耐心等待...");

try {

fs = FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

try {

status = fs.listStatus(new Path(path));

} catch (IllegalArgumentException | IOException e) {

e.printStackTrace();

}

for (int i=0; i<status.length;i++){

if(status[i].isFile()){

System.out.println("文件:"+status[i].getPath().toString());

}else if(status[i].isDirectory()){

System.out.println("目录:"+status[i].getPath().toString());

}

}

}

public void delete(String ip,String path,boolean boStr){

Configuration conf = new Configuration();

System.out.println("运行较慢,请耐心等待...");

try {

fs = FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

try {

fs.delete(new Path(path),boStr);

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

fs.close();

} catch (IllegalArgumentException | IOException e) {

e.printStackTrace();

}

}

public void mkdir(String ip,String path){

Configuration conf = new Configuration();

System.out.println("运行较慢,请耐心等待...");

try {

fs = FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

try {

fs.mkdirs(new Path(path));

System.out.println("运行结果 : 创建目录成功");

fs.close();

} catch (IllegalArgumentException | IOException e) {

e.printStackTrace();

}

}

public void write(String txt,String ip,String path){

Configuration conf = new Configuration();

System.out.println("运行较慢,请耐心等待...");

byte[] buff = txt.getBytes();

try {

fs=FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

Path dfs = new Path("hdfs://"+ip+":9000"+path);

FSDataOutputStream outputStream;

try {

outputStream = fs.create(dfs);

outputStream.write(buff,0, buff.length);

System.out.println("运行结果 : 写入文件成功");

fs.close();

} catch (IOException e) {

e.printStackTrace();

}

}

 

public void rename(String ip,String pathNoName,String oldName,String newName){

Configuration conf = new Configuration();

System.out.println("运行较慢,请耐心等待...");

try {

fs=FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

Path frPath = new Path("hdfs://"+ip+":9000"+pathNoName+"/"+oldName);

Path toPath = new Path("hdfs://"+ip+":9000"+pathNoName+"/"+newName);

try {

boolean isRenameFile = fs.rename(frPath, toPath);

if(isRenameFile){

System.out.println("运行结果 : 重命名成功");

}else{

System.out.println("运行结果 : 重命名失败");

}

fs.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public void exists(String ip,String path){

Configuration conf = new Configuration();

System.out.println("运行较慢,请耐心等待...");

try {

fs=FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

Path findfile = new Path("hdfs://"+ip+":9000"+path);

boolean isExists;

try {

isExists = fs.exists(findfile);

if(isExists){

System.out.println("运行结果 : 文件存在");

}else{

System.out.println("运行结果 : 文件不存在");

}

fs.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public void fileLoc(String ip,String Path){

Configuration conf = new Configuration();

System.out.println("运行较慢,请耐心等待...");

FileStatus filestatus=null;

try {

fs=FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

Path fpath = new Path("hdfs://"+ip+":9000"+Path);

try {

filestatus = fs.getFileStatus(fpath);

} catch (IOException e) {

e.printStackTrace();

}

BlockLocation[] blkLocations;

try {

blkLocations = fs.getFileBlockLocations(filestatus, 0, filestatus.getLen());

int blockLen = blkLocations.length;

System.out.println("block数量:"+blockLen);

for(int i = 0; i < blockLen; i++) {

String[] hosts = blkLocations[i].getHosts();

System.out.println("文件在HDFS集群中的位置 : block " + i + " ,location:" + hosts[i]);

}

fs.close();

} catch (IOException e) {

e.printStackTrace();

}

}

 

public void getTime(String ip,String path){

Configuration conf = new Configuration();

System.out.println("运行较慢,请耐心等待...");

try {

fs=FileSystem.get(new URI("hdfs://"+ip+":9000"), conf,"root");

} catch (IOException | InterruptedException | URISyntaxException e) {

e.printStackTrace();

}

Path fpath = new Path("hdfs://"+ip+":9000"+path);

FileStatus fileStatus;

try {

fileStatus = fs.getFileStatus(fpath);

long modificationTime = fileStatus.getModificationTime();

Date date = new Date(modificationTime);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

String time = sdf.format(date);

System.out.println("文件最后一次的修改时间: " + time);

fs.close();

} catch (IOException e) {

e.printStackTrace();

}

}

 

}

 

 

最后

以上就是细心水壶为你收集整理的Hadoop工具类的全部内容,希望文章能够帮你解决Hadoop工具类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部