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 编码实现文件传输、查看等基本文件控制的更多相关文章

  1. java文件传输之文件编码和File类的使用

    ---恢复内容开始--- 我们知道,在用户端和服务端之间存在一个数据传输的问题,例如下载个电影.上传个照片.发一条讯息.在这里我们 就说一下文件的传输. 1.文件编码 相信大家小时候玩过积木(没玩过也 ...

  2. 在本机eclipse中创建maven项目,查看linux中hadoop下的文件、在本机搭建hadoop环境

    注意 第一次建立maven项目时需要在联网情况下,因为他会自动下载一些东西,不然突然终止 需要手动删除断网前建立的文件 在eclipse里新建maven项目步骤 直接新建maven项目出了错      ...

  3. 在Vim中查看文件编码和文件编码转换

    在Vim中查看文件编码和文件编码转换 风亡小窝 关注  0.2 2016.09.26 22:43* 字数 244 阅读 5663评论 0喜欢 2 在Vim中查看文件编码 :set fileencodi ...

  4. linux下常用文件传输命令 (转)

    因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...

  5. 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)

    目录[-] 一.TCP/IP协议 二.TCP与UDP 三.Socket是什么 四.Java中的Socket 五.基本的Client/Server程序 六.多客户端连接服务器 七.信息共享 八.文件传输 ...

  6. 循序渐进Socket网络编程(多客户端、信息共享、文件传输)

    循序渐进Socket网络编程(多客户端.信息共享.文件传输) 前言:在最近一个即将结束的项目中使用到了Socket编程,用于调用另一系统进行处理并返回数据.故把Socket的基础知识总结梳理一遍. 1 ...

  7. Python 字符编码及其文件操作

    本章节内容导航: 1.字符编码:人识别的语言与机器机器识别的语言转化的媒介. 2.字符与字节:字符占多少个字节,字符串转化 3.文件操作:操作硬盘中的一块区域:读写操作 注:浅拷贝与深拷贝 用法: d ...

  8. TCP协议,UDP,以及TCP通信服务器的文件传输

    TCP通信过程 下图是一次TCP通讯的时序图.TCP连接建立断开.包含大家熟知的三次握手和四次握手. 在这个例子中,首先客户端主动发起连接.发送请求,然后服务器端响应请求,然后客户端主动关闭连接.两条 ...

  9. python tcp黏包和struct模块解决方法,大文件传输方法及MD5校验

    一.TCP协议 粘包现象 和解决方案 黏包现象让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd)执行远程命令的模块 需要用到模块subprocess sub ...

随机推荐

  1. Android ObjectOutputStream Serializable引发的血案

    遇到一个问题 安装后第二次进app,闪退 重现步骤 [前置条件] 打包分支:dev_7.13 手机:vivo NEX 8.1.0 [步骤] 安装三星app----同意用户协议进入书城---连续点击ba ...

  2. Asp.net Web Api开发Help Page 添加对数据模型生成注释的配置和扩展

    在使用webapi框架进行接口开发的时候,编写文档会需要与接口同步更新,如果采用手动式的更新的话效率会非常低.webapi框架下提供了一种自动生成文档的help Page页的功能. 但是原始版本的效果 ...

  3. nodejs 实现套接字服务

    nodejs实现套接字服务     一 什么是套接字 1.套接字允许一个进程他通过一个IP地址和端口与另一个进程通信,当你实现对运行在同一台服务器上的两个不同进程的进程间通信或访问一个完全不同的服务器 ...

  4. Hive支持行级update、delete时遇到的问题

    Hive从0.14版本开始支持事务和行级更新,但缺省是不支持的,需要一些附加的配置.要想支持行级insert.update.delete,需要配置Hive支持事务.(行级的insert好像不配置也能运 ...

  5. spring使用set方法注入的常见类型写法

    首先配置spring的pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  6. 第一篇 HTML基础

    浏览网页,就是上网,上网的本质就是下载内容. 浏览器是个解释器,用来执行HTML.css.JS代码的. HTML,CSS, JavaScript 号称网络三剑客. 1. 浏览器发送一个域名给服务端 2 ...

  7. HDU-1496(哈希表)

    Hash入门第一题 题意: 问题描述 考虑具有以下形式的方程: a * x1 ^ 2 + b * x2 ^ 2 + c * x3 ^ 2 + d * x4 ^ 2 = 0 a,b,c,d是来自区间[- ...

  8. 【wx:for】小程序列表渲染的使用说明

    wx:for 控制属性绑定一个数组,即可使用数组中各项的数据重复渲染该组件. 默认数组的当前项的下标变量名默认为 index,数组当前项的变量名默认为 item,即: {{index}} . {{it ...

  9. Java异常处理介绍(Java知识的重点内容)

    Java 异常处理 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error:如果你 ...

  10. 爬虫2.1-scrapy框架-两种爬虫对比

    目录 scrapy框架-两种爬虫对比和大概流程 1. 传统spider爬虫 2. crawl型爬虫 3. 循环页面请求 4. scrapy框架爬虫的大致流程 scrapy框架-两种爬虫对比和大概流程 ...