hadoop 编码实现文件传输、查看等基本文件控制
hadoop集群搭建参考:https://www.cnblogs.com/asker009/p/9126354.html
1、创建一个maven工程,添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.home</groupId>
<artifactId>FileSystemCat</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</project>
2、实现cat、copy、filestatus等基本代码,代码可以在windows的IDE环境中正常运行(参考上篇在windows里调试hadoop),也可以打成jar包放入远程hadoop集群上执行。
如果对hadoop的端口不熟悉,在测试环境可以关闭hadoop集群上的防火墙。
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils; import java.io.*;
import java.net.URI;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId; /**
* @Author: xu.dm
* @Date: 2019/1/31 14:39
* @Description:
*/
public class FileSystemCat { private static String HDFSUri = "hdfs://bigdata-senior01.home.com:9000"; public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
//文本文件cat
// fileCat(args); //文件copy
fileCopyWithProgress(args); //file status
// fileStatus(args); // file status pattern
// filePattern(args); long endTime = System.currentTimeMillis();
long timeSpan = endTime - startTime;
System.out.println("耗费时间:" + timeSpan + "毫秒");
} private static FileSystem getFileSystem() {
Configuration conf = new Configuration(); //文件系统
FileSystem fs = null;
String hdfsUri = HDFSUri;
if (StringUtils.isBlank(hdfsUri)) {
//返回默认文件系统,如果在hadoop集群下运行,使用此种方法可直接获取默认文件系统;
try {
fs = FileSystem.get(conf);
} catch (IOException e) {
e.printStackTrace();
}
} else {
//返回指定的文件系统,如果在本地测试,需要此种方法获取文件系统;
try {
URI uri = new URI(hdfsUri.trim());
fs = FileSystem.get(uri, conf);
} catch (Exception e) {
e.printStackTrace();
}
}
return fs;
} ///hadoop输出文本文件内容
private static void fileCat(String[] args) throws Exception {
String uri = args[0];
Configuration conf = new Configuration();
// conf.set("fs.hdfs.impl",org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
conf.set("fs.defaultFS","hdfs://bigdata-senior01.home.com:9000");
FileSystem fs = FileSystem.get(URI.create(uri), conf);
// InputStream in = null;
// FSDataInputStream继承 java.io.DataInputStream,支持随机访问
FSDataInputStream in = null;
try {
in = fs.open(new Path(uri));
IOUtils.copyBytes(in, System.out, 4096, false);
in.seek(0);
IOUtils.copyBytes(in, System.out, 4096, false);
} finally {
IOUtils.closeStream(in);
}
} //将本地文件拷贝到hadoop文件系统上
//需要开通datanode用于数据传输端口:9866
private static void fileCopyWithProgress(String[] args) throws Exception {
String locaSrc = args[0];
String dst = args[1]; //从windows环境提交的时候需要设置hadoop用户名
//在linux的其他用户环境下估计也需要
System.setProperty("HADOOP_USER_NAME","hadoop"); Configuration conf = new Configuration();
// conf.set("fs.DefaultFs", "hdfs://bigdata-senior01.home.com:9000"); //因为涉及到两个文件系统的数据传输,如果dst不是全路径的话(带不带hdfs的头)用这种方式取到的还是本地文件系统
//如果不涉及两个文件系统,dst写短路径是没问题的
// FileSystem fs = FileSystem.get(URI.create(dst), conf); FileSystem fs = FileSystem.get(URI.create(HDFSUri),conf); // fs.copyFromLocalFile(new Path(locaSrc),new Path(dst));
// fs.close();
// System.out.println("copyFromLocalFile...done"); InputStream in = new BufferedInputStream(new FileInputStream(locaSrc)); FSDataOutputStream out = null; out = fs.create(new Path(dst), () -> System.out.print(".")); IOUtils.copyBytes(in, out, 4096, true);
} //查找文件,递归列出给定目录或者文件的属性
private static void fileStatus(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://bigdata-senior01.home.com:9000");
Path file = new Path(args[0]);
FileSystem fs = null;
try {
// fs = FileSystem.get(URI.create(args[0]),conf);
fs = FileSystem.get(conf); // fs = getFileSystem(); //对单个文件或目录
// FileStatus fileStatus = fs.getFileStatus(file); //对单个文件或目录下所有文件和目录
FileStatus[] fileStatuses = fs.listStatus(file); //FileUtil封装了很多文件功能
//FileStatus[] 和 Path[]转换
// Path[] files = FileUtil.stat2Paths(fileStatuses); for (FileStatus fileStatus : fileStatuses) {
System.out.println("-------------->");
System.out.println("是否目录:" + fileStatus.isDirectory());
System.out.println("path:" + fileStatus.getPath().toString());
System.out.println("length:" + fileStatus.getLen());
System.out.println("accessTime:" + LocalDateTime.ofInstant(Instant.ofEpochMilli(fileStatus.getAccessTime()), ZoneId.systemDefault()));
System.out.println("permission:" + fileStatus.getPermission().toString());
//递归查找子目录
if (fileStatus.isDirectory()) {
FileSystemCat.fileStatus(new String[]{fileStatus.getPath().toString()});
}
}
} finally {
if (fs != null)
fs.close();
}
} //查找文件,通配模式,不能直接用于递归,应该作为递归的最外层,不进入递归
private static void filePattern(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://bigdata-senior01.home.com:9000");
FileSystem fs = null;
try {
fs = FileSystem.get(conf);
//输入参数大于1,第二个参数作为排除参数
//例如:hadoop jar FileSystemCat.jar /demo ^.*/demo[1-3]$ 排除/demo1,/demo2,/demo3
//例如:hadoop jar FileSystemCat.jar /demo/wc* ^.*/demo/wc3.*$ 排除/demo下wc3开头所有文件
FileStatus[] fileStatuses = null;
if (args.length > 1) {
System.out.println("过滤路径:" + args[1]);
fileStatuses = fs.globStatus(new Path(args[0]), new RegexExcludePathFilter(args[1]));
} else {
fileStatuses = fs.globStatus(new Path(args[0]));
} for (FileStatus fileStatus : fileStatuses) {
System.out.println("-------------->");
System.out.println("是否目录:" + fileStatus.isDirectory());
System.out.println("path:" + fileStatus.getPath().toString());
System.out.println("length:" + fileStatus.getLen());
System.out.println("modificationTime:" + LocalDateTime.ofInstant(Instant.ofEpochMilli(fileStatus.getModificationTime()), ZoneId.systemDefault()));
System.out.println("permission:" + fileStatus.getPermission().toString());
if (fileStatus.isDirectory()) {
FileSystemCat.fileStatus(new String[]{fileStatus.getPath().toString()});
}
}
} finally {
if (fs != null)
fs.close();
}
} }
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter; /**
* @Author: xu.dm
* @Date: 2019/2/1 13:38
* @Description:
*/
public class RegexExcludePathFilter implements PathFilter { private final String regex; public RegexExcludePathFilter(String regex) {
this.regex = regex;
} @Override
public boolean accept(Path path) {
return !path.toString().matches(this.regex);
}
}
hadoop 编码实现文件传输、查看等基本文件控制的更多相关文章
- java文件传输之文件编码和File类的使用
---恢复内容开始--- 我们知道,在用户端和服务端之间存在一个数据传输的问题,例如下载个电影.上传个照片.发一条讯息.在这里我们 就说一下文件的传输. 1.文件编码 相信大家小时候玩过积木(没玩过也 ...
- 在本机eclipse中创建maven项目,查看linux中hadoop下的文件、在本机搭建hadoop环境
注意 第一次建立maven项目时需要在联网情况下,因为他会自动下载一些东西,不然突然终止 需要手动删除断网前建立的文件 在eclipse里新建maven项目步骤 直接新建maven项目出了错 ...
- 在Vim中查看文件编码和文件编码转换
在Vim中查看文件编码和文件编码转换 风亡小窝 关注 0.2 2016.09.26 22:43* 字数 244 阅读 5663评论 0喜欢 2 在Vim中查看文件编码 :set fileencodi ...
- linux下常用文件传输命令 (转)
因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...
- 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)
目录[-] 一.TCP/IP协议 二.TCP与UDP 三.Socket是什么 四.Java中的Socket 五.基本的Client/Server程序 六.多客户端连接服务器 七.信息共享 八.文件传输 ...
- 循序渐进Socket网络编程(多客户端、信息共享、文件传输)
循序渐进Socket网络编程(多客户端.信息共享.文件传输) 前言:在最近一个即将结束的项目中使用到了Socket编程,用于调用另一系统进行处理并返回数据.故把Socket的基础知识总结梳理一遍. 1 ...
- Python 字符编码及其文件操作
本章节内容导航: 1.字符编码:人识别的语言与机器机器识别的语言转化的媒介. 2.字符与字节:字符占多少个字节,字符串转化 3.文件操作:操作硬盘中的一块区域:读写操作 注:浅拷贝与深拷贝 用法: d ...
- TCP协议,UDP,以及TCP通信服务器的文件传输
TCP通信过程 下图是一次TCP通讯的时序图.TCP连接建立断开.包含大家熟知的三次握手和四次握手. 在这个例子中,首先客户端主动发起连接.发送请求,然后服务器端响应请求,然后客户端主动关闭连接.两条 ...
- python tcp黏包和struct模块解决方法,大文件传输方法及MD5校验
一.TCP协议 粘包现象 和解决方案 黏包现象让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd)执行远程命令的模块 需要用到模块subprocess sub ...
随机推荐
- spring之HttpInvoker
一.HttpInvoker是常用的Java同构系统之间方法调用实现方案,是众多Spring项目中的一个子项目.顾名思义,它通过HTTP通信即可实现两个Java系统之间的远程方法调用,使得系统之间的通信 ...
- 流式断言器AssertJ介绍
本文来自网易云社区 作者:范旭斐 大家在使用testng.junit做自动化测试的过程中,经常会用到testng.junit自带的断言器,有时候对一个字符串.日期.列表进行断言很麻烦,需要借助到jdk ...
- Mac环境下RabbitMq安装与测试教程
RabbitMq安装与测试教程 Installing on Mac I. 安装 123456789 brew install rabbitmq ## 进入安装目录cd /usr/local/Cella ...
- mysql新手进阶02
云想衣裳花想容,春风拂槛露华浓. 若非群玉山头见,会向瑶台月下逢. 现在有一教学管理系统,具体的关系模式如下: Student (no, name, sex, birthday, class) Tea ...
- leetcode-帕斯卡三角形
帕斯卡三角形 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 示例: 输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4 ...
- lintcode172 删除元素
删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个数组 [0, ...
- Java 消息对列
ActiveMQ入门实例 1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5. ...
- 微软的XML可视化编辑器:XML Notepad 2007
最近项目需要定义xml协议格式,编写xml文件比较多,之前使用xml spy工具,但是不够轻量级. 微软提供的xml nodepad 2007很实用,希望能给大家提供帮助. 运行后的界面 下载地址:h ...
- LiveVideoStack Meet|深圳 多媒体开发新趋势
2018年初始,音视频技术生态并不平静,Codec争夺愈加激烈,新一代标准的挑战一浪高过一浪:WebRTC的定版也为打通浏览器.移动端乃至IoT带来了机会:此外AI.区块链技术的兴起,催化着与多媒体领 ...
- 三:QJM HDFS高可用
本文介绍的是HDFS的一种HA方案.虽然有checkpoint node \backup node等,但是不能实现自动的failover. http://hadoop.apache.org/docs/ ...