- 浏览: 239522 次
- 性别:
文章分类
最新评论
使用URL的方式读取一个文件内容,需要设置一个handler工厂,这个工厂只能设置一次
使用hadoop的FileSystem读取文件
将一个本地文件拷贝到hadoop文件系统中
列出文件属性
通过路径过滤器查找文件
删除,支持递归删除
重命名
检查文件是否存在
查找某个文件在HDFS中的位置
获取HDFS集群上所有节点的名称
创建本地和远端的checksum
压缩和解压缩,压缩池
static { URL.setURLStreamHandlerFactory( new FsUrlStreamHandlerFactory() ); } public void test1() throws IOException { URL u = new URL("hdfs://IP:8020/test"); InputStream is = u.openStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = null; while( (line=br.readLine()) != null ) { System.out.println(line); } br.close(); }
使用hadoop的FileSystem读取文件
public void test2() throws IOException { String url = "hdfs://IP:8020/test-data/hello.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url),config); InputStream is = null; is = fs.open(new Path(url)); IOUtils.copyBytes(is, System.out, 4096, false); }
将一个本地文件拷贝到hadoop文件系统中
public void test3() throws IOException { String src = "C:\\test.txt"; String dest = "hdfs://IP:8020/test-data/hello.txt"; InputStream is = new BufferedInputStream(new FileInputStream(src)); Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(dest), config); OutputStream os = fs.create(new Path(dest), new Progressable() { @Override public void progress() { System.out.print("."); } }); IOUtils.copyBytes(is, os, 4096, true); System.out.println("ok~"); }
列出文件属性
public void test4() throws IOException { String url = "hdfs:/IP:8020/test-data/hello.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url),config); FileStatus status = fs.getFileStatus(new Path(url)); System.out.println("AccessTime : "+status.getAccessTime()); System.out.println("BlockSize : "+status.getBlockSize()); System.out.println("group : "+status.getGroup()); System.out.println("len : "+status.getLen()); System.out.println("ModificationTime : "+status.getModificationTime()); System.out.println("owner : "+status.getOwner()); System.out.println("is dir ? : "+status.isDir()); System.out.println("path : "+status.getPath()); System.out.println("permission : "+status.getPermission()); System.out.println("replication : "+status.getReplication()); }
通过路径过滤器查找文件
public void test5() throws IOException { String url = "hdfs://IP:8020/test-data/*"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url),config); FileStatus[] status = fs.globStatus( new Path(url), new RegexPathFilter("^.*hello.*$") ); for(FileStatus s:status) { System.out.println(s.getPath().toString()); } System.out.println("filter execute ok"); } //路径正则过滤器类 public class RegexPathFilter implements PathFilter { private final String regex; public RegexPathFilter(String regex) { this.regex = regex; } @Override public boolean accept(Path path) { return path.toString().matches(regex); } }
删除,支持递归删除
public void delete() throws IOException { String url = "hdfs://IP:8020/test-data/xxx.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url),config); fs.delete(new Path(url), false); System.out.println("delete ok"); }
重命名
public void test5_rename() throws IOException { String url = "hdfs://IP:8020/test-data/xx.txt"; String url2 = "hdfs://IP:8020/test-data/modify-xx.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url),config); boolean isok = fs.rename(new Path(url), new Path(url2)); System.out.println("complete : "+isok); }
检查文件是否存在
public void exist() throws IOException { String url = "hdfs:/IP:8020/test-data/modify-xx.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url),config); boolean isExist = fs.exists( new Path(url)); System.out.println("exist ? "+isExist); }
查找某个文件在HDFS中的位置
public void test5_location() throws IOException { String url = "hdfs://IP:8020/test-data/hello.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url),config); FileStatus status = fs.getFileStatus(new Path(url)); BlockLocation[] bls = fs.getFileBlockLocations(status, 0, status.getLen()); for(int i=0;i<bls.length;i++) { String[] hosts = bls[i].getHosts(); System.out.println("block :"+i+"\tlocation : "+hosts[i]); } }
获取HDFS集群上所有节点的名称
public void test5_allnode() throws IOException { String url2 = "hdfs://IP:8020/test-data/modify-xx.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url2),config); DistributedFileSystem hdfs = (DistributedFileSystem)fs; DatanodeInfo[] status = hdfs.getDataNodeStats(); for(DatanodeInfo d:status) { System.out.println(d.getHostName()); } }
创建本地和远端的checksum
public void localCreateChecksum() throws IOException { String url = "file:///C:/zzzz/abc.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url),config); ChecksumFileSystem cfs = new LocalFileSystem(fs); FSDataOutputStream fsdos = cfs.create(new Path(url)); fsdos.write("hehe".getBytes()); fsdos.flush(); fsdos.close(); } public void distributeCreateChecksum() throws IOException { String url = "hdfs://IP:8020/test/abc.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url),config); ChecksumFileSystem cfs = new LocalFileSystem(fs); FSDataOutputStream fsdos = cfs.create(new Path(url)); fsdos.write("hehe~".getBytes()); fsdos.flush(); fsdos.close(); }
压缩和解压缩,压缩池
public void compress() throws IOException { FileInputStream fis = new FileInputStream("C:\\zzzz\\xx.txt"); GzipCodec gc = new GzipCodec(); String url = "hdfs://IP:8020/test/compress.txt"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url), config); OutputStream fdos = fs.create(new Path(url)); byte[] buf = new byte[10240]; int len = fis.read(buf); System.out.println("content:"); System.out.println( new String(buf,0,len) ); CompressionOutputStream cos = gc.createOutputStream(fdos); cos.write(buf,0,len); cos.flush(); cos.close(); } public void decompress() throws IOException { String url = "hdfs://IP:8020/test/compress.gz"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url), config); GzipCodec gc = new GzipCodec(); FSDataInputStream fdis = fs.open(new Path(url)); CompressionInputStream cis = gc.createInputStream(fdis); IOUtils.copy(cis, System.out); } public void comprssPool() throws IOException { FileInputStream fis = new FileInputStream("C:\\zzzz\\abc.txt"); GzipCodec gc = new GzipCodec(); FileOutputStream fdos = new FileOutputStream("C:/zzzz/pool.txt"); Compressor compressor = CodecPool.getCompressor(gc); CompressionOutputStream cos = gc.createOutputStream(fdos, compressor); IOUtils.copy(fis, cos); CodecPool.returnCompressor(compressor); }
发表评论
-
Hadoop-DataNode分析
2016-04-06 18:41 651HDFS主要流程 客户端创建到name ... -
Hadoop-远程过程调用
2015-06-15 16:46 523Hadoop IPC类图如下 ... -
HBase-cache相关
2015-04-09 15:03 631一些配置参数 hbase.lru.block ... -
HBase-压缩和分割原理
2015-03-31 11:33 582HRegionServer调用合并请求 主要逻 ... -
Hbase-HMaster架构
2015-03-24 19:21 2196HMaster的整体结构 一个master包含如下 ... -
Hadoop-balancer执行原理
2015-01-28 21:19 931核心类在 org.apache.h ... -
Hadoop-commons分析
2014-10-22 18:19 744hadoop的配置文件相关类 Configurati ... -
HBase-客户端请求
2014-05-27 19:46 1621客户端相关参数 参数 默认值 含义 ... -
HBase-服务端处理请求的过程
2014-05-19 19:19 2487Region的架构 HRegionSe ... -
HBase-打印日志内容分析
2014-05-15 17:14 946WARN org.apache.hadoop.hba ... -
HBase-遇到的问题
2014-05-15 16:43 2836rowlock release problem wi ... -
HBase-HFile的读写操作
2014-05-03 19:40 3341写入数据: public class Test ... -
HBase-WAL相关线程处理逻辑
2014-02-28 19:21 2124日志同步线程 HLog$L ... -
HBase-region server的线程
2014-02-28 19:19 48这个是用于和HDFS通讯的客户端线程 Daemon Th ... -
HBase-RegionServer架构
2014-02-28 19:06 4702RegionServer的整体结构 一个regio ... -
HBase-HLog分析
2014-02-08 17:37 2182HLog的全部实现在包: org.apache.hado ... -
HBase-HFile分析
2014-01-10 19:25 3250HFile的整体结构图如下: 整个HFile分四 ... -
HBase-线程调整
2013-08-20 17:14 750read线程数量 ipc.server.read.thread ... -
Hadoop-常见问题
2013-07-05 12:50 599经典漫画讲解HDFS原理 http://blog.china ... -
HBase-各种API操作
2013-07-03 11:08 948初始化 Configuration HBASE_CONFI ...
相关推荐
在这里,我们选择 CentOS 6.5 作为操作系统。下载 CentOS 6.5 的镜像文件,并将其烧录到安装媒体中。启动安装程序,选择 Install CentOS 6.5,回车,继续安装。选择语言,默认是 English,学习可以选择中文,正时...
熟悉常用的Linux操作和Hadoop操作
熟悉常用的Linux操作和Hadoop操作.docx
数据科学导论 实验1:常用Linux操作和 Hadoop操作 1、Linux虚拟机安装和操作 2、Hadoop安装和操作 1)创建Hadoop用户 2)SSH登录权限设置 3)安装Java环境 4)单机安装配置 5)伪分布式安装配置
【实验目的】 1、Hadoop 运行在 Linux 系统上,因此...本实验旨在熟悉常用的 Linux 操作和 Hadoop 操作,为顺利开展后续其他实验奠定基础。 【实验平台】 操作系统:Linux(建议 Ubuntu22.04); Hadoop 版本:3.5.5。
Hadoop I/O操作8. 下一代MapReduce: Yarn9. HDFS简介10. HDFS文件结构11. Hive详解12. HBase详解13. Mahout简介14. Pig详解15. ZooKeeper详解16. Avro详解17. Chukwa详解18. Hadoop的常用插件与开发19. Hadoop在...
该文档记录了在linux的shell环境下执行的hadoop的相关操作命令,包括多hafs文件的相关操作
显示当前目录结构 #显示当前目录结构 ...删除操作 #删除文件 hadoop fs -rm [path] #递归删除目录和文件 hadoop fs -rm -R [path] 从本地加载文件到 HDFS #二选一执行即可 hadoop fs -put [localsrc]
04-hive的常用语法.avi 05-hql语法及自定义函数.avi 06-hbase表结构.avi 07-hbase集群架构及表存储机制.avi 08-hbase-shell.avi 09-hbase的java api.avi 第七天 storm+kafka 006-kafka整合storm.avi 01-...
Hadoop HDFS分布式文件系统常用命令汇总 Hadoop HDFS分布式文件系统是大数据存储和处理的核心组件。作为一个分布式文件系统,HDFS提供了高可靠性、高可扩展性和高性能的存储解决方案。在使用HDFS时,经常需要执行...
A.2实验二:熟悉常用的HDFS操作 本实验对应第4章的内容。 A.2.1 实验目的 (1)理解 HDFS在Hadoop体系结构中的角色。(2)熟练使用HDFS操作常用的 Shell命令。(3)熟悉HDFS操作常用的Java API。 A.2.2 实验平台 (1)操作...
第8-11章全面地阐述了hadoop的i/o操作、hdfs的原理与基本操作,以及hadoop的各种管理操作,如集群的维护等;第12-17章详细而系统地讲解了hive、hbase、mahout、pig、zookeeper、avro、chukwa等所有与hadoop相关的子...
Hadoop I/O操作8. 下一代MapReduce: Yarn9. HDFS简介10. HDFS文件结构11. Hive详解12. HBase详解13. Mahout简介14. Pig详解15. ZooKeeper详解16. Avro详解17. Chukwa详解18. Hadoop的常用插件与开发19. Hadoop在...
A.3实验三:熟悉常用的HBase操作 本实验对应第5章的内容。 A.3.1 实验目的 (1)理解HBase在Hadoop体系结构中的角色。(2)熟练使用HBase操作常用的 Shell命令。(3)熟悉HBase操作常用的 Java API。 A.3.2 实验平台 (1...
大数据课程自备资料:Hadoop安装指南 + Linux常用操作命令
实验1 熟悉常用的Linux操作和Hadoop操作 一、实验目的 Hadoop运行在Linux系统上,因此,需要学习实践一些常用的Linux命令。本实验旨在熟悉常用的Linux操作和Hadoop操作,为顺利开展后续的其他实验奠定基础。 二、...
这是在单机上的Cent-OS 系统中以Hadoop伪分布式形式进行安装和配置的过程,并收集和整理了Hadoop运维常用操作命令。 非常适合没有集群环境的初学者入门使用。
熟悉常用的HDFS操作 (1) 理解HDFS在Hadoop体系结构中的角色; (2) 熟练使用HDFS操作常用的Shell命令; (3) 熟悉HDFS操作常用的Java API。
hadoop 全套环境搭建指南,三台虚拟机环境准备 linux基础及shell增强 大数据集群环境准备 zookeeper介绍及集群操作 网络编程
hadoop2安装和常用shell命令,