hbase HBASE简介 1.是什么 HBase是Apache的Hadoop项目的子项目,用于存储海量的结构化或者半结构,非结构化的数据。存储结构化和非结构数据的分布式数据库系统将数据存储在不同的机器上利用不用机器来处理并发请求 2.特点 1).高可靠性 , 分布式的数据库 , 存储的数据是安全的, 集群对外服务是可靠的 2).高性能, 处理数据的速度,效率, 分布式的数据库就可以利用集群中所有机器的运算资源处理数据 3).可伸缩性 从存储能力和运算能力处理 ,可以通过添加节点来扩展存储容量和添加运算资源 4).面向列 : 在hbase底层存储的所有的数据都是以key/value组成的字节数据 它介于nosql(notonlysql)和RDBMS之间(nosql一般不存储结构化数据 ,hbase结构化和非结构化都存),仅能通过主键(row key)和主键的range来检索数据,仅支持单行事务(可通过hive或者Phoenix支持来实现多表join等复杂操作)。主要用来存储非结构化和半结构化和结构化的松散数据。与hadoop一样,Hbase目标主要依靠横向扩展,通过不断增加廉价的商用服务器,来增加计算和存储能力。 HBase中的表一般有这样的特点: 1 大:一个表可以有上几十亿行,上百万列 2 面向列:面向列(族)的存储和权限控制,列(族)独立检索。 3 可以高效应对稀疏数据表:对于为空(null)的列,并不占用存储空间,因此,表可以设计的非常稀疏。 1.2.hbase的特点 1)海量存储 Hbase适合存储PB级别的海量数据,在PB级别的数据以及采用廉价PC存储的情况下,能在几十到百毫秒内返回数据。这与Hbase的极易扩展性息息相关。正式因为Hbase良好的扩展性,才为海量数据的存储提供了便利。 2)列式存储 这里的列式存储其实说的是列族存储,Hbase是根据列族来存储数据的。列族下面可以有非常多的列,列族在创建表的时候就必须指定。 3)极易扩展 Hbase的扩展性主要体现在两个方面,一个是基于上层处理能力(RegionServer)的扩展,一个是基于存储的扩展(HDFS)。 通过横向添加RegionSever的机器,进行水平扩展,提升Hbase上层的处理能力,提升Hbsae服务更多Region的能力。 备注:RegionServer的作用是管理region、承接业务的访问,这个后面会详细的介绍通过横向添加Datanode的机器,进行存储层扩容,提升Hbase的数据存储能力和提升后端存储的读写能力。 4)高并发 集群中对外服务的节点叫RegionServer(存储表的部分范围数据) 并发查询的时候 不同的机器节点对外服务 支持高并发 由于目前大部分使用Hbase的架构,都是采用的廉价PC,因此单个IO的延迟其实并不小,一般在几十到上百ms之间。这里说的高并发,主要是在并发的情况下,Hbase的单个IO延迟下降并不多。能获得高并发、低延迟的服务。 5)稀疏 稀疏主要是针对Hbase列的灵活性,在列族中,你可以指定任意多的列,在列数据为空的情况下,是不会占用存储空间的。 3.应用场景 应用场景 HBase擅长于存储结构简单的海量数据但索引能力有限,而Oracle等传统关系型数据库(RDBMS)能够提供丰富的查询能力,但却疲于应对TB级别的海量数据存储,HBase对传统的RDBMS并不是取代关系,而是一种补充。不适合使用需要数据分析,比如报表(rowkey) 单表数据不超过千万(200万)适合使用HBASE单表数据超过千万 ,并发量还很高数据分析需求弱 ,或者不需要那么灵活和实时 HBASE基本概念 HBASE架构 1.4.系统架构 1.4.1.client 提供了访问hbase的接口 提供cache提高访问hbase的效率 , 比如region的信息 1.4.2.zookeeper 通过选举保证任何时候,集群中只有一个master,Master与RegionServers 启动时会向ZooKeeper注册 存贮所有Region的寻址入口:索引表-meta-所在服务器 实时监控Regionserver的上线和下线信息。并实时通知给Master 存储HBase的schema和table元数据 默认情况下,HBase 管理ZooKeeper 实例,比如, 启动或者停止ZooKeeper,但一般我们不会让hbase自己管理一套zookeeper进程,而是让hbase使用我们外部的一个公共zookeeper集群 ,所以我们在安装的时候通常都会配置自己的ZK集群 .Zookeeper的引入使得Master不再有单点故障
1.4.3.HMaster (分配数据到底存储在哪个regiserver中) 为Region server分配region(数据) 负责Region server的负载均衡 发现失效的Regionserver并通过拆分LOG重新分配其上的region 管理用户对table定义的各类DDL操作
1.4.4.HRegionServer 管理master为其分配的Region 处理来自客户端的读写请求 负责和底层HDFS的交互,存储数据到HDFS 负责Region变大以后的拆分 负责Storefile的合并工作 负载均衡的事情 管理表的片段数据(region) Region:HBase中的数据都是按row-key进行排序的,对这些按row-key排序的数据进行水平切分,每一片称为一个Region,它有startkey和endkey,Region的大小可以配置,一台RegionServer中可以放多个Region CF:列族。一个列族中的所有列存储在相同的HFile文件中 HFile:HFile就是Hadoop磁盘文件,一个列族中的数据保存在一个或多个HFile中,这些HFile是对列族的数据进行水平切分后得到的。 MemStore:HFile在内存中的体现。当我们update/delete/create时,会先写MemStore,写完后就给客户端response了,当Memstore达到一定大小后,会将其写入磁盘,保存为一个新的HFile。HBase后台会对多个HFile文件进行merge,合并成一个大的HFile 1.4.5.HDFS HDFS为Hbase提供最终的底层数据存储服务,同时为HBase提供高可用(Hlog存储在HDFS)的支持 提供元数据和表数据的底层分布式存储服务 数据多副本,保证的高可靠和高可用性
1.5.hbase的数据存储模型 hbase中也是有表的概念的,表存储的数据的行数和列数非常多! 百万列 table 代表一个数据集 user: name age ID gender friend 工作 职位…爱好 行键 唯一的确定一行数据 , 行的标识 , 索引 , 全局排序 列族 列的属性相近的划分成一组, 方便管理属性 列名 具体到真正的属性, 行的列只有插入数据的时候才确定 region : 表的行范围数据抽象 K:table:行键:列族:属性 V: 值
1.5.1.逻辑视图 HBase以表的形式存储数据。表有行和列组成。列划分为若干个列族(row family)
在物理上,hbase表中的数据全是key-value形式存在 一个完整的key,由3段组成: 行键:列族名:列名 (rowkey:columnFamily:qualifier) Hbase中的数据没有类型约束的(所谓没有类型,其实就是byte[]) 在物理上,hbase的这些key-value是按“完整key”的字典顺序有序存储 ! 1.5.1.1. RowKey 用来表示唯一一行记录的主键,HBase的数据是按照RowKey的字典顺序进行全局排序的,所有的查询都只能依赖于这一个排序维度。
小 提 示 通过下面一个例子来说明一下”字典排序“的原理: RowKey {“abc”, “a”, “bdf”, “cdf”, “defg”} 按字典排序后的结果为: {“a”, “abc”, “bdf”, “cdf”, “defg”} 也就是说,当两个RowKey进行排序时,先对比两个RowKey的第一个字节,如果相同,则对比第二个字节,依此类推…如果在对比到第M个字节时,已经超出了其中一个RowKey的字节长度,那么,短的RowKey要被排在另外一个RowKey的前面 1.5.1.2. Region 区别于Cassandra/DynamoDB的”Hash分区”设计,HBase中采用了”Range分区”,将Key的完整区间切割成一个个的”Key Range” ,每一个”Key Range”称之为一个Region。 也可以这么理解:将HBase中拥有数亿行的一个大表,横向切割(rowkey的某个范围—起始rowkey,结束rowkey)成一个个”子表“,这一个个”子表“就是Region:
Region是HBase中负载均衡的基本单元,当一个Region增长到一定大小以后,会自动分裂成两个。
1.5.1.3. Column Family (列族,列簇) 如果将Region看成是一个表的横向切割,那么,一个Region中的数据列的纵向切割,称之为一个Column Family。每一个列,都必须归属于一个Column Family,这个归属关系是在写数据时指定的,而不是建表时预先定义。 核心概念 数据存储特点 HBASE安装 启停 环境配置 日志 时间同步 yum -y install ntpdate.x86_64 ntpdate 0.asia.pool.ntp.org 手动的设置时间 date -s “2020-01-08 17:12:55” ZK集群启动 2.1.1.Zookeeper集群启动 [root@linux01 ~]# /usr/apps/zookeeper-3.4.6/bin/zkServer.sh start [root@linux02 ~]# /usr/apps/zookeeper-3.4.6/bin/zkServer.sh start [root@linux03 ~]# /usr/apps/zookeeper-3.4.6/bin/zkServer.sh start 2.1.2.HDFS正常启动 [root@linux01 ~]# start-dfs.sh hdfs正常启动 配置 tar -zxvf hbase-2.0.4-bin.tar.gz -C /opt/apps/ 2.1.5.hbase配置 1)hbase-env.sh修改内容:注意放开注释 export JAVA_HOME=/opt/apps/jdk1.8.0_141/ esc /ZK export HBASE_MANAGES_ZK=false 2)hbase-site.xml修改内容:
hbase.rootdir hdfs://linux01:9000/hbase hbase.cluster.distributed true hbase.zookeeper.quorum linux01:2181,linux02:2181,linux03:2181 3)regionservers 配置 启动集群中的Regionserver机器 linux01 linux02 linux03 2.1.6.集群分发 [root@linux01 apps]# pwd /opt/apps [root@linux01 apps]# scp -r hbase-2.0.4/ linux02:$PWD [root@linux01 apps]# scp -r hbase-2.0.4/ linux03:$PWD 1.启动方式1 bin/hbase-daemon.sh start master bin/hbase-daemon.sh start regionserver 2.启动方式2 bin/start-hbase.sh 一键启动 HBASE的shell客户端 基本 version status whoami DDL 建表 修改 查看结构 禁用 启动 删除表 regions信息 过滤器 是否存在 DML put scan get delete deleteall namespace 列表 创建 删除 修改 查看名称空间下的表 HBASE的基本原理 数据存储在HDFS中的 HFILE 内容 Region:HBase中的数据都是按row-key进行排序的,对这些按row-key排序的数据进行水平切分,每一片称为一个Region,它有startkey和endkey,Region的大小可以配置,一台RegionServer中可以放多个RegionCF:列族。一个列族中的所有列存储在相同的HFile文件中 HFile:HFile就是Hadoop磁盘文件,一个列族中的数据保存在一个或多个HFile中,这些HFile是对列族的数据进行水平切分后得到的。 MemStore:HFile在内存中的体现。当我们update/delete/create时,会先写MemStore,写完后就给客户端response了,当Memstore达到一定大小后,会将其写入磁盘,保存为一个新的HFile。HBase后台会对多个HFile文件进行merge,合并成一个大的HFile HFILE 位置 HBASE的java客户端 ``` package filter; import HbaseUtils.HbaseUtils; import org.apache.hadoop.hbase.CompareOperator; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.RowFilter; import java.util.Iterator; public class ScanDemo125125125125 { public static void main(String[] args) throws Exception { Table tb = HbaseUtils.getHbaseTableName(“tb_filter”); Scan scan = new Scan();
/** * 参数一 比较运算符 * LESS,小于 * LESS_OR_EQUAL, 小于等于 * EQUAL, * NOT_EQUAL, * GREATER_OR_EQUAL, * GREATER, * NO_OP, * 参数二 比较规则 */ // >= 第二行 RowFilter filter1 = new RowFilter(CompareOperator.GREATER_OR_EQUAL, new BinaryComparator("k0002".getBytes())); // <= 第四行 RowFilter filter2 = new RowFilter(CompareOperator.LESS_OR_EQUAL, new BinaryComparator("k0003".getBytes())); // 设置行过滤器 /* scan.setFilter(filter1); scan.setFilter(filter2);*/ // 设置多条件过滤 类似于 SQL中的where id >=2 and id <=10 //FilterList 参数是可变的 可以传入多个过滤器 同时生效 FilterList filterList = new FilterList(filter1, filter2); scan.setFilter(filterList) ; ResultScanner scanner = tb.getScanner(scan); // 遍历数据 Iterator<Result> iterator = scanner.iterator(); while(iterator.hasNext()){ Result result = iterator.next(); HbaseUtils.showData(result); } tb.close(); } }package filter; import HbaseUtils.HbaseUtils; import org.apache.hadoop.hbase.CompareOperator; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.SubstringComparator; import org.apache.hadoop.hbase.filter.ValueFilter; import java.util.Iterator; public class ScanDemo125 { public static void main(String[] args) throws Exception { Table tableabc = HbaseUtils.getHbaseTableName(“tableabc”); Get get = new Get(“rk001”.getBytes()); get.addColumn(“cf”.getBytes(),“a”.getBytes()); Result result = tableabc.get(get); HbaseUtils.showData(result); Scan scan = new Scan(); ValueFilter valueFilter = new ValueFilter(CompareOperator.EQUAL, new SubstringComparator(“abc”)); scan.setFilter(valueFilter); ResultScanner scanner = tableabc.getScanner(scan); Iterator iterator = scanner.iterator(); while(iterator.hasNext()){ Result next = iterator.next(); HbaseUtils.showData(next); } } }
package HbaseClient;
import HbaseUtils.HbaseUtils; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*;
import java.io.IOException; import java.util.ArrayList;
public class CreateTableDemo { public static void main(String[] args) throws IOException { Admin admin = HbaseUtils.getHbaseAdmin(); modifyTable(admin); createTable1(admin); createTable2(admin); createTable3(admin); modifyTable(admin); deleteTable(admin); admin.close(); }
/** * 删除表 * @param admin * @throws IOException */ public static void deleteTable(Admin admin) throws IOException{ TableName tb_spl = TableName.valueOf("tb_spl"); if(admin.tableExists(tb_spl)){ admin.disableTable(tb_spl); if(admin.isTableDisabled(tb_spl)){ admin.deleteTable(tb_spl); }else{ System.out.println("请先禁用表"); } }else{ System.out.println("表不存在"); } } /** * 修改表 * @param admin * @throws IOException */ private static void modifyTable(Admin admin) throws IOException { TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("tb_spl")); ColumnFamilyDescriptorBuilder cf = ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes()); //仅修改某个列族的属性 cf.setMaxVersions(5); ColumnFamilyDescriptor cfbuild = cf.build(); tableDescriptorBuilder.setColumnFamily(cfbuild); TableDescriptor tableDescriptor = tableDescriptorBuilder.build(); admin.modifyTable(tableDescriptor); admin.modifyColumnFamily(TableName.valueOf("tb_spl"),cfbuild); } /** * 创建预分region表 * @param admin * @throws IOException */ public static void createTable3(Admin admin) throws IOException{ TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("demo")); ColumnFamilyDescriptorBuilder cf = ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes()); ColumnFamilyDescriptor cfbuild = cf.build(); tableDescriptorBuilder.setColumnFamily(cfbuild); TableDescriptor tableDescriptor = tableDescriptorBuilder.build(); byte[][] keys = new byte[][]{"a".getBytes(),"b".getBytes(),"c".getBytes(),"d".getBytes(),"e".getBytes()}; admin.createTable(tableDescriptor,keys); } public static void createTable2(Admin admin) throws IOException{ TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("demo125")); ColumnFamilyDescriptorBuilder cf1 = ColumnFamilyDescriptorBuilder.newBuilder("cf1".getBytes()); ColumnFamilyDescriptor cf1build = cf1.build(); // 设置数据存储的版本的个数 //cf1.setMaxVersions(5); // 设置列族中数据的存储时间 单位秒 //cf1.setTimeToLive(240); ColumnFamilyDescriptorBuilder cf2 = ColumnFamilyDescriptorBuilder.newBuilder("cf2".getBytes()); ColumnFamilyDescriptor cf2build = cf2.build(); // 设置数据存储的版本的个数 //cf2.setMaxVersions(5); // 设置列族中数据的存储时间 单位秒 //cf2.setTimeToLive(240); ArrayList<ColumnFamilyDescriptor> al = new ArrayList<ColumnFamilyDescriptor>(); al.add(cf1build); al.add(cf2build); tableDescriptorBuilder.setColumnFamilies(al); TableDescriptor build = tableDescriptorBuilder.build(); admin.createTable(build); /* ArrayList<ColumnFamilyDescriptor> al = new ArrayList <ColumnFamilyDescriptor>(); al.add(cf1build); al.add(cf2build); tableDescriptorBuilder.setColumnFamilies(al);*/ TableDescriptor tableDescriptor = tableDescriptorBuilder.build(); admin.createTable(tableDescriptor); } /** * 创建一个列族的表 * @param admin * @throws IOException */ private static void createTable1(Admin admin) throws IOException { TableDescriptorBuilder tableabc = TableDescriptorBuilder.newBuilder(TableName.valueOf("tableabc")); ColumnFamilyDescriptorBuilder cf = ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes()); cf.setMaxVersions(5); cf.setTimeToLive(125); ColumnFamilyDescriptor cfbuild = cf.build(); tableabc.setColumnFamily(cfbuild); TableDescriptor build = tableabc.build(); admin.createTable(build);/* //表的构建器 表名 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(“tb_spl”)); // 列族构建器 ColumnFamilyDescriptorBuilder cf = ColumnFamilyDescriptorBuilder.newBuilder(“cf”.getBytes()); // 设置数据存储的版本的个数 //cf.setMaxVersions(5); // 设置列族中数据的存储时间 单位秒 //cf.setTimeToLive(240); // 列族的描述对象 ColumnFamilyDescriptor cfbuild = cf.build(); // 表中添加列族 // 表的构建器 添加列族 tableDescriptorBuilder.setColumnFamily(cfbuild); //表描述对象 TableDescriptor tableDescriptor = tableDescriptorBuilder.build(); //创建表 admin.createTable(tableDescriptor);*/ } }
package HbaseClient;
import HbaseUtils.HbaseUtils; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException; import java.util.ArrayList; import java.util.Iterator;
public class DataDemo { public static void main(String[] args) throws Exception { Table tableName = HbaseUtils.getHbaseTableName(“tb_spl”); getRowDatas(tableName); getOneRowData(tableName); putDemo1(tableName); putDemo2(tableName); scanDatas(tableName); deleteData(tableName); tableName.close(); } public static void deleteData(Table tableName) throws IOException { // 获取删除对象 指定行 Delete delete = new Delete(Bytes.toBytes(“rk000”)); //设置参数 删除某个属性 delete.addFamily(Bytes.toBytes(“cf”)); //delete.addColumn(); // 没有进行任何的参数设置 删除整行数据 tableName.delete(delete); tableName.close(); } /** * 扫描数据 * @param tableName * @throws IOException / public static void scanDatas(Table tableName) throws IOException{ Scan scan = new Scan(); //起始行包括 scan.withStartRow(“rk001”.getBytes()); //结束行不包括 scan.withStopRow(“rk004”.getBytes()); ResultScanner scanner = tableName.getScanner(scan); Iterator iterator = scanner.iterator(); while(iterator.hasNext()){ Result result = iterator.next(); HbaseUtils.showData(result); } } /* * 获取多行数据 * @param tableName * @throws IOException */ private static void getRowDatas(Table tableName) throws IOException { Get get1 = new Get(“rk001”.getBytes()); Get get2 = new Get(“rk002”.getBytes()); Get get3 = new Get(“rk003”.getBytes()); // 指定获取数据的 哪个列族的哪个属性 get1.addColumn(“cf”.getBytes(),“name”.getBytes()); // 指定获取哪个列族的数据 get1.addFamily(“cf”.getBytes()); ArrayList al = new ArrayList(); al.add(get1); al.add(get2); al.add(get3); // 查询多行数据 返回每行组成的数组 Result[] results = tableName.get(al); for (Result result : results) { HbaseUtils.showData(result); } }
/** * 获取一行数据 * @param tableName * @throws IOException */ private static void getOneRowData(Table tableName) throws IOException { Get get = new Get("rk001".getBytes()); Result result = tableName.get(get); while(result.advance()){ Cell cell = result.current(); byte[] familyArray = cell.getFamilyArray(); byte[] qualifierArray = cell.getQualifierArray(); byte[] rowArray = cell.getRowArray(); byte[] valueArray = cell.getValueArray(); //byte[] family = CellUtil.cloneFamily(cell); byte[] family = CellUtil.cloneFamily(cell); byte[] qualifier = CellUtil.cloneQualifier(cell); byte[] row = CellUtil.cloneRow(cell); byte[] value = CellUtil.cloneValue(cell); System.out.println(" 行: "+new String(row)+" 列族: "+new String(family)+" 属性: "+new String(qualifier)+" 值: "+new String(value)); } }/* // get对象 代表一行数据 (所有的列族 属性) Get get = new Get(“rk001”.getBytes()); // 一行结果数据 有很多单元格 单元格的个数不确定 稀疏性 Result result = tableName.get(get); while(result.advance()){ // 只要有下一个单元格进入循环 // 获取当前的单元格对象 Cell cell = result.current(); byte[] familyArray = cell.getFamilyArray(); byte[] qualifierArray = cell.getQualifierArray(); byte[] valueArray = cell.getValueArray(); byte[] row = CellUtil.cloneRow(cell); byte[] family = CellUtil.cloneFamily(cell); byte[] qualifier = CellUtil.cloneQualifier(cell); byte[] value = CellUtil.cloneValue(cell); System.out.println(" 行: “+new String(row)+” 列族: “+new String(family)+” 属性: “+new String(qualifier)+” 值: "+new String(value)); }*/
/** * 一次put一行数据 * @param tableName * @throws IOException */ private static void putDemo1(Table tableName) throws IOException { //获取所有的列族 tableName.getDescriptor().getColumnFamilies(); Put put = new Put("rk001".getBytes()); put.addColumn("cf".getBytes(),"name".getBytes(),"abc".getBytes()); put.addColumn("cf".getBytes(),"age".getBytes(),"125".getBytes()); tableName.put(put); } /** * 一次put多行数据 * @param tableName * @throws IOException */ private static void putDemo2(Table tableName) throws IOException { Put put1 = new Put("rk001".getBytes()); Put put2 = new Put("rk002".getBytes()); Put put3 = new Put("rk003".getBytes()); put1.addColumn("cf1".getBytes(),"name".getBytes(),"abc".getBytes()); put1.addColumn("cf1".getBytes(),"age".getBytes(),"125".getBytes()); put2.addColumn("cf2".getBytes(),"gender".getBytes(),"n".getBytes()); put2.addColumn("cf2".getBytes(),"telephone".getBytes(),"125".getBytes()); put1.addColumn("cf3".getBytes(),"address".getBytes(),"abcabc".getBytes()); put1.addColumn("cf3".getBytes(),"email".getBytes(),"125".getBytes()); ArrayList<Put> al = new ArrayList<Put>(); al.add(put1); al.add(put2); al.add(put3); tableName.put(al);/* Put put1 = new Put(“rk001”.get.Bytes()); Put put2 = new Put(“rk002”.getBytes()); Put put3 = new Put(“rk003”.getBytes()); put1.addColumn(“cf”.getBytes(),“name”.getBytes(),“abc”.getBytes()); put1.addColumn(“cf”.getBytes(),“age”.getBytes(),“125”.getBytes()); put2.addColumn(“cf”.getBytes(),“uid”.getBytes(),“125”.getBytes()); put2.addColumn(“cf”.getBytes(),“tel”.getBytes(),“125”.getBytes()); put3.addColumn(“cf”.getBytes(),“gender”.getBytes(),“n”.getBytes()); put3.addColumn(“cf”.getBytes(),“address”.getBytes(),“hahaha”.getBytes()); ArrayList al = new ArrayList(); al.add(put1); al.add(put2); al.add(put3); tableName.put(al);*/ } }
package HbaseClient;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
public class HbaseConnection1 { public static void main(String[] args) throws IOException { Configuration config = HBaseConfiguration.create(); config.set(“hbase.zookeeper.quorom”,“linux01:2181,linux02:2181,linux03:2181”); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf(“abc_tb”)); Admin admin = connection.getAdmin(); TableName[] tableNames = admin.listTableNames(); for (TableName tableName : tableNames) { // 获取namespace的字节数据 byte[] namespace = tableName.getNamespace(); // 获取表名的字节数据 byte[] name = tableName.getName(); System.out.println(new String(namespace) + “<—>” + new String(name) ); } admin.close(); table.close(); connection.close(); // 1 配置对象 // Configuration config = HBaseConfiguration.create(); // 2 指定zookeeper的位置 //config.set(“hbase.zookeeper.quorom”,“linux01:2181,linux02:2181,linux03:2181”); // 3 获取hbase的连接对象 //Connection connection = ConnectionFactory.createConnection(config); /** * * 从连接对象中可以获取两个对象 * 1 admin DDL语言有关的 , 工具运维命令有关的操作 * 修改表 * 创建表 * 删除表 * 创建namespace * 删除namespace * 快照… * flush * 合并 * 负载均衡 * 禁用表… * 列出所有的表… * 2 table DMl数据操作有关的命令 * 获取数据 * 添加数据 * …表数据操作有关的 * * */ //Admin admin = connection.getAdmin(); //Table table = connection.getTable(null); //connection.close();
}}
package HbaseClient;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
public class HbaseConnection1 { public static void main(String[] args) throws IOException { Configuration config = HBaseConfiguration.create(); config.set(“hbase.zookeeper.quorom”,“linux01:2181,linux02:2181,linux03:2181”); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf(“abc_tb”)); Admin admin = connection.getAdmin(); TableName[] tableNames = admin.listTableNames(); for (TableName tableName : tableNames) { // 获取namespace的字节数据 byte[] namespace = tableName.getNamespace(); // 获取表名的字节数据 byte[] name = tableName.getName(); System.out.println(new String(namespace) + “<—>” + new String(name) ); } admin.close(); table.close(); connection.close(); // 1 配置对象 // Configuration config = HBaseConfiguration.create(); // 2 指定zookeeper的位置 //config.set(“hbase.zookeeper.quorom”,“linux01:2181,linux02:2181,linux03:2181”); // 3 获取hbase的连接对象 //Connection connection = ConnectionFactory.createConnection(config); /** * * 从连接对象中可以获取两个对象 * 1 admin DDL语言有关的 , 工具运维命令有关的操作 * 修改表 * 创建表 * 删除表 * 创建namespace * 删除namespace * 快照… * flush * 合并 * 负载均衡 * 禁用表… * 列出所有的表… * 2 table DMl数据操作有关的命令 * 获取数据 * 添加数据 * …表数据操作有关的 * * */ //Admin admin = connection.getAdmin(); //Table table = connection.getTable(null); //connection.close();
}}
package HbaseClient;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
public class HbaseConnection2 { public static void main(String[] args) throws IOException { Configuration config = HBaseConfiguration.create(); config.set(“hbase.zookeeper.quorom”,“linux01:2181,linux02:2181,linux03:2181”); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf(“abc_tb”)); Admin admin = connection.getAdmin(); // 获取系统中所有的表,标的抽象 TableName[] tableNames = admin.listTableNames(); for (TableName tableName : tableNames) { // 获取namespace的字节数据 byte[] namespace = tableName.getNamespace(); // 获取表名的字节数据 byte[] name = tableName.getName(); System.out.println(new String(namespace) + “<—>” + new String(name) ); } admin.close(); table.close(); connection.close(); } }
package HbaseClient;
import HbaseUtils.HbaseUtils; import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.client.Admin;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
public class NameSpaceDemo { public static void main(String[] args) throws IOException { Admin admin = HbaseUtils.getHbaseAdmin(); //修改名称空间的属性 //changeNameSpace(admin); //查看系统下所有的名称空间 //getAllNameSpace(admin); // 获取名称空间的构建器 //creatNameSpace(admin); admin.close();
} /** * 修改名称空间的属性 * @param admin * @throws IOException */ private static void changeNameSpace(Admin admin) throws IOException { NamespaceDescriptor.Builder doit = NamespaceDescriptor.create("doit"); doit.removeConfiguration("author"); doit.addConfiguration("auhtor","abchahaha"); NamespaceDescriptor build = doit.build(); admin.createNamespace(build);/* NamespaceDescriptor.Builder doit = NamespaceDescriptor.create(“doit”); doit.removeConfiguration(“author”); doit.addConfiguration(“author”,“abc”); NamespaceDescriptor build = doit.build(); admin.modifyNamespace(build);*/ }
/** *查看系统下所有的名称空间 * 并且查看配置信息 * * @param admin * @throws IOException */ private static void getAllNameSpace(Admin admin) throws IOException { //NamespaceDescriptor.Builder doit = NamespaceDescriptor.create("doit"); NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors(); for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) { String name = namespaceDescriptor.getName(); Map<String, String> map = namespaceDescriptor.getConfiguration(); System.out.println(name + "<--->" + map.entrySet()); }/* NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors(); for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) { // 一个名称空间对象 // 名字 String name = namespaceDescriptor.getName(); Map<String, String> map = namespaceDescriptor.getConfiguration(); System.out.println(name + “<—>” + map.entrySet()); }*/ }
/** * 创建名称空间 * @param admin * @throws IOException */ private static void creatNameSpace(Admin admin) throws IOException { NamespaceDescriptor.Builder doit = NamespaceDescriptor.create("doit"); HashMap<String,String> hm = new HashMap<String,String>(); hm.put("author","abc"); hm.put("desc","my first namespace"); doit.addConfiguration(hm); NamespaceDescriptor namespaceDescriptor = doit.build(); admin.createNamespace(namespaceDescriptor);/* // 获取名称空间的构建器 NamespaceDescriptor.Builder doit = NamespaceDescriptor.create(“doit”); // 对名称空间的属性设置 HashMap<String,String> hm = new HashMap<>(); hm.put(“author”,“DOIT”); hm.put(“desc”,“my first namespace”); doit.addConfiguration(hm); //构造名称空间对象 NamespaceDescriptor namespaceDescriptor = doit.build(); // 调用方法 创建 admin.createNamespace(namespaceDescriptor);*/ } }
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
public class HbaseConnection1 { public static void main(String[] args) throws IOException { Configuration config = HBaseConfiguration.create(); config.set(“hbase.zookeeper.quorom”,“linux01:2181,linux02:2181,linux03:2181”); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf(“abc_tb”)); Admin admin = connection.getAdmin(); TableName[] tableNames = admin.listTableNames(); for (TableName tableName : tableNames) { // 获取namespace的字节数据 byte[] namespace = tableName.getNamespace(); // 获取表名的字节数据 byte[] name = tableName.getName(); System.out.println(new String(namespace) + “<—>” + new String(name) ); } admin.close(); table.close(); connection.close(); // 1 配置对象 // Configuration config = HBaseConfiguration.create(); // 2 指定zookeeper的位置 //config.set(“hbase.zookeeper.quorom”,“linux01:2181,linux02:2181,linux03:2181”); // 3 获取hbase的连接对象 //Connection connection = ConnectionFactory.createConnection(config); /** * * 从连接对象中可以获取两个对象 * 1 admin DDL语言有关的 , 工具运维命令有关的操作 * 修改表 * 创建表 * 删除表 * 创建namespace * 删除namespace * 快照… * flush * 合并 * 负载均衡 * 禁用表… * 列出所有的表… * 2 table DMl数据操作有关的命令 * 获取数据 * 添加数据 * …表数据操作有关的 * * */ //Admin admin = connection.getAdmin(); //Table table = connection.getTable(null); //connection.close(); } }
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
public class HbaseConnection2 { public static void main(String[] args) throws IOException { Configuration config = HBaseConfiguration.create(); config.set(“hbase.zookeeper.quorom”,“linux01:2181,linux02:2181,linux03:2181”); Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin(); Table table = connection.getTable(TableName.valueOf(“tb_spl”)); TableName[] tableNames = admin.listTableNames(); for (TableName tableName : tableNames) { byte[] namespace = tableName.getNamespace(); byte[] name = tableName.getName(); System.out.println(new String(namespace) + “<—>” + new String(name) ); } admin.close(); table.close(); connection.close(); /* Configuration config = HBaseConfiguration.create(); config.set(“hbase.zookeeper.quorom”,“linux01:2181,linux02:2181,linux03:2181”); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf(“abc_tb”)); Admin admin = connection.getAdmin(); // 获取系统中所有的表,标的抽象 TableName[] tableNames = admin.listTableNames(); for (TableName tableName : tableNames) { // 获取namespace的字节数据 byte[] namespace = tableName.getNamespace(); // 获取表名的字节数据 byte[] name = tableName.getName(); System.out.println(new String(namespace) + “<—>” + new String(name) ); } admin.close(); table.close(); connection.close();*/ } }
