




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、技术创新,变革未来Hadoop深入浅出之HBASE介绍课程安排HBASE基础知识*HBASE的伪分布的搭建*HBASE Shell*HBASE的批量导入*HBASE的Java客户端*-加深拓展-HBASE的集群的搭建*HBASE的表设计*HBASE的底层存储模型*HBASE基础知识HBase简介 HBase Hadoop Database,是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群。HBase利用Hadoop HDFS作为其文件存储系统,利用Hadoop MapReduce来处理HBase中的海量数据,利用Z
2、ookeeper作为协调工具。复习HBASE基础知识HBASE中的每一张表,就是所谓的BigTable。稀疏表。RowKey 和 ColumnKey 是二进制值byte,按字典顺序排序;Timestamp 是一个 64 位整数;value 是一个未解释的字节数组byte。表中的不同行可以拥有不同数量的成员。即支持“动态模式“模型逻辑数据模型物理数据模型将逻辑模型中的一个Row分割为根据Column family存储的物理模型数据模型行Row KeyTime StampColumn ContentsColumn AnchorColumn “mime”my.look.ca“n.www”T9CNNT
3、8CNN.COMT6“. “Text/htmlT5“. “t3“. “行键列列字符串、整数、二进制串甚至串行化的结构都可以作为行键表按照行键的“逐字节排序”顺序对行进行有序化处理表内数据非常稀疏,不同的行的列的数完全目可以大不相同可以只对一行上“锁”对行的写操作是始终是“原子”的数据模型列列必须用族(family)来定义任意一列有如下形式“族:标签”其中,族和标签都可为任意形式的串物理上将同“族”数据存储在一起数据可通过时间戳区分版本Row KeyTime StampColumn ContentsColumn AnchorColumn “mime”my.look.ca“n.www”T9CNNT
4、8CNN.COMT6“. “Text/htmlT5“. “t3“. “族标签HBASE基础知识表是存放数据的。表由行和列组成数据模型Row Key: 行键,Table的主键,Table中的记录按照Row Key排序Timestamp: 时间戳,每次数据操作对应的时间戳,可以看作是数据的version numberColumn Family:列簇,Table在水平方向有一个或者多个Column Family组成,一个Column Family中可以有任意多个Column组成,即Column Family支持动态扩展,无需预先定义Column的数量以及类型,所有Column均以二进制格式存储,用户
5、需要自行进行类型转换。HBASE基础知识物理存储 Table 在行的方向上分割为多个HRegion,一个region由startkey,endkey)表示,每个HRegion分散在不同的RegionServer中参数hbase.hregion.max.filesizeHBASE基础知识架构体系Client 包含访问hbase 的接口,client 维护着一些cache 来加快对hbase 的访问,比如regione 的位置信息Zookeeper 保证任何时候,集群中只有一个running master 存贮所有Region 的寻址入口 实时监控Region Server 的状态,将Region
6、 server 的上线和下线信息,实时通知给Master 存储Hbase 的schema,包括有哪些table,每个table 有哪些column familyMaster 可以启动多个HMaster,通过Zookeeper的Master Election机制保证总有一个Master运行为Region server 分配region负责region server 的负载均衡发现失效的region server 并重新分配其上的regionHBASE基础知识HBASE基础知识架构体系Region Server维护Master 分配给它的region,处理对这些region 的IO 请求负责切分在
7、运行过程中变得过大的region 可以看出,client 访问hbase 上数据的过程并不需要master 参与,寻址访问zookeeper 和region server,数据读写访问regioneserver。HRegionServer主要负责响应用户I/O请求,向HDFS文件系统中读写数据,是HBase中最核心的模块。 系统架构HBASE与相关软件hadoop.1.1.2hbase-0.94.2-security.tar.gzJDK6RHEL6.3前提条件:本机或集群环境下hadoop.1.1.2已经安装成功本地伪分布模式搭建(一)把hbase-0.94.2-security.tar.gz
8、复制到/usr/local解压hbase-0.94.2-security.tar.gz与重命名#cd /usr/local#tar -zxvf hbase-0.94.2-security.tar.gz#mv hbase-0.94.2-security hbase修改/etc/profile文件。#vi /etc/profile增加 export HBASE_HOME=/home/hbase修改 export PATH=$JAVA_HOME/bin:$PATH:$HADOOP_HOME/bin:$HBASE_HOME/bin保存退出#source /etc/profile本地伪分布模式搭建(二)
9、修改$HBASE_HOME/conf/hbase-env.sh文件 export JAVA_HOME=/usr/local/jdkexport HBASE_MANAGES_ZK=true保存后退出修改$HBASE_HOME/conf/hbase-site.xml hbase.rootdir hdfs:/hadoop0:9000/hbase hbase.cluster.distributed true hbase.zookeeper.quorum hadoop0 dfs.replication 1注意:$HBASE_HOME/conf/hbase-site.xml的hbase.rootdir的主
10、机和端口号与$HADOOP_HOME/conf/core-site.xml的的主机和端口号一致本地伪分布模式搭建(三)在$HBASE_HOME/conf/regionservers文件增加localhost保存退出先启动Hadoop,后启动HBASE#cd $HBASE_HOME/bin#./start-hbase.sh停止#cd $HBASE_HOME/bin#./stop-hbase.sh本地伪分布模式搭建(四)#jps查看hdfs目录,你会发现在根目录下多了一个hbase的目录#hadoop fs -ls /Web Consolehttp:/hadoop0:60010/master-st
11、atusHBASE Shellhbase提供了一个shell的终端给用户交互名称命令表达式创建表create 表名称, 列族名称1,列族名称2,列族名称N添加记录 put 表名称, 行名称, 列名称:, 值查看记录get 表名称, 行名称查看表中的记录总数count 表名称删除记录delete 表名 ,行名称 , 列名称删除一张表先要屏蔽该表,才能对该表进行删除,第一步 disable 表名称 第二步 drop 表名称查看所有记录scan 表名称 查看某个表某个列中所有数据scan 表名称 , COLUMNS=列族名称:列名称更新记录 就是重写一遍进行覆盖#$HBASE_HOME/bin/hb
12、ase shell HBASE Shell的DDL操作创建表create users,user_id,address,info表users,有三个列族user_id,address,info列出全部表list 得到表的描述describe users创建表create users_tmp,user_id,address,info删除表disable users_tmpdrop users_tmp#$HBASE_HOME/bin/hbase shell quit exists usersis_enabled usersis_disabled usersHBASE Shell的DML操作添加记录获
13、取一条记录1.取得一个id的所有数据get users,xiaoming2.获取一个id,一个列族的所有数据get users,xiaoming,info3.获取一个id,一个列族中一个列的所有数据get users,xiaoming,info:ageput users,xiaoming,info:age,24;put users,xiaoming,info:birthday,1987-06-17;put users,xiaoming,info:company,alibaba;put users,xiaoming,address:contry,china;put users,xiaoming,
14、address:province,zhejiang;put users,xiaoming,address:city,hangzhou;put users,zhangyifei,info:birthday,1987-4-17;put users,zhangyifei,info:favorite,movie;put users,zhangyifei,info:company,alibaba;put users,zhangyifei,address:contry,china;put users,zhangyifei,address:province,guangdong;put users,zhang
15、yifei,address:city,jieyang;put users,zhangyifei,address:town,xianqiao;HBASE Shell的DML操作更新记录put users,xiaoming,info:age ,29get users,xiaoming,info:ageput users,xiaoming,info:age ,30get users,xiaoming,info:age获取单元格数据的版本数据get users,xiaoming,COLUMN=info:age,VERSIONS=1get users,xiaoming,COLUMN=info:age,V
16、ERSIONS=2get users,xiaoming,COLUMN=info:age,VERSIONS=3获取单元格数据的某个版本数据get users,xiaoming,COLUMN=info:age,TIMESTAMP=1364874937056全表扫描scan usersHBASE Shell的DML操作删除xiaoming值的info:age字段delete users,xiaoming,info:ageget users,xiaoming删除整行deleteall users,xiaoming统计表的行数count users清空表truncate usersHBASE基础知识HB
17、ase中有两张特殊的Table,-ROOT-和.META. .META.:记录了用户表的Region信息,.META.可以有多个regoin -ROOT-:记录了.META.表的Region信息,-ROOT-只有一个region Zookeeper中记录了-ROOT-表的locationClient访问用户数据之前需要首先访问zookeeper,然后访问-ROOT-表,接着访问.META.表,最后才能找到用户数据的位置去访问HBASE的Java_API(一)/hbase操作必备 private static Configuration getConfiguration() Configurat
18、ion conf = HBaseConfiguration.create();conf.set(hbase.rootdir, hdfs:/hadoop0:9000/hbase);/使用eclipse时必须添加这个,否则无法定位conf.set(hbase.zookeeper.quorum, hadoop0);return conf;HBASE的Java_API(二)/创建一张表public static void create(String tableName, String columnFamily) throws IOExceptionHBaseAdmin admin = new HBas
19、eAdmin(getConfiguration();if (admin.tableExists(tableName) System.out.println(table exists!);elseHTableDescriptor tableDesc = new HTableDescriptor(tableName);tableDesc.addFamily(new HColumnDescriptor(columnFamily);admin.createTable(tableDesc);System.out.println(create table success!);HBASE的Java_API(
20、三)/添加一条记录public static void put(String tableName, String row, String columnFamily, String column, String data) throws IOExceptionHTable table = new HTable(getConfiguration(), tableName);Put p1 = new Put(Bytes.toBytes(row);p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data)
21、;table.put(p1);System.out.println(put+row+,+columnFamily+:+column+,+data+);HBASE的Java_API(四)/读取一条记录public static void get(String tableName, String row) throws IOExceptionHTable table = new HTable(getConfiguration(), tableName);Get get = new Get(Bytes.toBytes(row);Result result = table.get(get);Syste
22、m.out.println(Get: +result);HBASE的Java_API(五)/显示所有数据public static void scan(String tableName) throws IOExceptionHTable table = new HTable(getConfiguration(), tableName);Scan scan = new Scan();ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) System.out.println(Scan: +resul
23、t);HBASE的Java_API(六)/删除表public static void delete(String tableName) throws IOExceptionHBaseAdmin admin = new HBaseAdmin(getConfiguration();if(admin.tableExists(tableName)try admin.disableTable(tableName); admin.deleteTable(tableName); catch (IOException e) e.printStackTrace(); System.out.println(Del
24、ete +tableName+ 失败);System.out.println(Delete +tableName+ 成功);HBASE的Java_API(七)public static void main(String args) throws IOException String tableName=hbase_tb;String columnFamily=cf;HBaseTestCase.create(tableName, columnFamily);HBaseTestCase.put(tableName, row1, columnFamily, cl1, data);HBaseTestC
25、ase.get(tableName, row1);HBaseTestCase.scan(tableName);HBaseTestCase.delete(tableName);练习详单入库HBASE表定义为:create wlan_log, cfRowKey设计:msisdn:日期时间串(yyyyMMddHHmmss)源文件数据增加一个字段rowkeyHBASE结合MapReduce批量导入static class BatchImportMapper extends MapperSimpleDateFormat dateformat1=new SimpleDateFormat(yyyyMMddH
26、Hmmss);Text v2 = new Text();protected void map(LongWritable key, Text value, Context context) throws java.io.IOException ,InterruptedException final String splited = value.toString().split(t);try final Date date = new Date(Long.parseLong(splited0.trim();final String dateFormat = dateformat1.format(d
27、ate);String rowKey = splited1+:+dateFormat;v2.set(rowKey+t+value.toString();context.write(key, v2); catch (NumberFormatException e) final Counter counter = context.getCounter(BatchImport, ErrorFormat);counter.increment(1L);System.out.println(出错了+splited0+ +e.getMessage();HBASE结合MapReduce批量导入static c
28、lass BatchImportReducer extends TableReducerprotected void reduce(LongWritable key, java.lang.Iterable values, Context context) throws java.io.IOException ,InterruptedException for (Text text : values) final String splited = text.toString().split(t);final Put put = new Put(Bytes.toBytes(splited0);pu
29、t.add(Bytes.toBytes(cf), Bytes.toBytes(date), Bytes.toBytes(splited1);/省略其他字段,调用put.add(.)即可context.write(NullWritable.get(), put);HBASE结合MapReduce批量导入public static void main(String args) throws Exception final Configuration configuration = new Configuration();/设置zookeeperconfiguration.set(hbase.zoo
30、keeper.quorum, hadoop0);/设置hbase表名称configuration.set(TableOutputFormat.OUTPUT_TABLE, wlan_log);/将该值改大,防止hbase超时退出configuration.set(dfs.socket.timeout, 180000);final Job job = new Job(configuration, HBaseBatchImport);job.setMapperClass(BatchImportMapper.class);job.setReducerClass(BatchImportReducer.class);/设置map的输出,不设置reduce的输出类型job.setMapOutputKeyClass(LongWritable.class);job.setMapOutputValueClass(Text.class);job.setInputFormatClass(TextInputFormat.class);/不再设置输出路径,而是设置输出格式
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 本溪中考生物试题及答案
- 职业生涯发展中的软技能应用试题及答案
- 电商中物流体系的构建与优化试题及答案
- 真实案例商务英语考试试题及答案
- 择业倾向测试题及答案
- 环保产业技术创新与产业升级产业竞争力分析报告
- 辉县特岗面试真题及答案
- 数字技术在2025年文化遗产展示与传播中的应用策略分析报告
- 绿色建筑材料市场推广困境与政策支持路径探索:2025年行业分析报告
- 理解乐曲结构2025年乐理考试试题及答案
- (二模)2024~2025学年度苏锡常镇四市高三教学情况调研(二)物理试卷(含答案)
- GB/T 36572-2018电力监控系统网络安全防护导则
- GB/T 19066.1-2003柔性石墨金属波齿复合垫片分类
- 2021年新高考I卷英语试题分析及备考展望2022课件
- 常见的圆柱截切体名师优质课赛课一等奖市公开课获奖课件
- 《蒋公的面子剧本》
- 护生临床沟通能力测评量表
- 《工厂供电》第六版习习题解答(不全)
- 海水分析化学 考试大纲
- 国内常见模具钢牌号对照表
- 解聘证明范本
评论
0/150
提交评论