Hbase配置及命令使用(上传)资料
- 格式:ppt
- 大小:1.94 MB
- 文档页数:30
HBASE 安装一、安装指南二、常见问题一、安装指南Hadoop+Hbase安装配置实录2010-10-12 22:53生产环境:3台机器:master(192.168.0.61),slave1(192.168.0.62),slave2(192.168.0.63) 注意:hostname设置为master/slave1/slave2操作系统:rhel5.4 x86_64master做为namenonde,将slave1和slave2做为datanode1.在master:(在slave1和slave2上操作和以下相同)vi /etc/hosts192.168.0.61 master192.168.0.62 slave1192.168.0.63 slave22.用root操作3.免密码登录#ssh-keygen -t rsa #建立ssh目录,敲回车到底 ,这一步需要先在每台机器上执行。
在master上#scp ~/.ssh/id_rsa.pub root@slave1:/root/.ssh/id_rsa.pub_m将master 上的密钥传到slave1的/home/hadoop下在slave1上#cat /root/.ssh/id_rsa.pub_m >> ~/.ssh/authorized_keys#chmod 644 ~/.ssh/authorized_keys反复操作第3步,完成master<-->slave1 master<-->slave2的免密码登录这样主节点和从节点之间就可以不用密码直接ssh访问,另外,在启动hadoop 时需要master ssh master,因此,在master的~/.ssh下,也执行下cat id_rsa.pub >> authorized_keys 即可。
4.安装JDK到/usr/local下命名为jdk6然后:编辑三台机器的/etc/profile,增加如下内容export JAVA_HOME=/usr/local/jdk6export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/libexport HADOOP_HOME=/hadoop/hadoopexport HBASE_HOME=/hadoop/hbasePATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HBASE_HOME/bin#vi /root/.bashrc增加export HADOOP_CONF_DIR=/hadoop/hadoop-configexport HBASE_CONF_DIR=/hadoop/hbase-config5、防火墙各机器加入iptables策略:#iptables -I INPUT -s 192.168.0.0/255.255.255.0 -j ACCPET#service iptables save-----------------------------------------hadoop配置:1.下载安装#cd /hadoop#wget/apache-mirror//hadoop/core/hadoop-0.20.2/hadoo p-0.20.2.tar.gz#tar -zxvf hadoop-0.20.2.tar.gz#ln -s hadoop-0.20.2 hadoop由于上述目录中hadoop的配置文件和hadoop的安装目录是放在一起的,这样一旦日后升级hadoop版本的时候所有的配置文件都会被覆盖,因此将配置文件与安装目录分离,一种比较好的方法就是建立一个存放配置文件的目录,/hadoop/hadoop-config/,然后将 /hadoop/hadoop/conf/目录中的core-site.xml,slaves,hadoop-env.sh,masters, hdfs-site.xml,mapred-site.xml,这6个文件拷贝到 /hadoop/hadoop-config/目录中,并指定环境变量$HADOOP_CONF_DIR指向该目录。
Hbase建表基本命令总结访问hbase,以及操作hbase,命令不⽤使⽤分号hbase shell 进⼊hbaselist 查看表hbase shell -dhbase(main):024:0> scan '.META.'=============⼩例⼦===================================================1. 创建⼀个表memberhbase(main):025:0> create 'member','m_id','address','info'2.list #查看所有表3.查看表描述hbase(main):028:0> describe 'member'4. scan浏览表中数据scan 'member'5.删除⼀个列族m_id:先将表disable(如果表中之前有数据的话,会把数据清空)hbase(main):030:0> disable 'member'hbase(main):033:0> is_enabled 'member'hbase(main):034:0> alter 'member',{NAME=>'m_id',METHOD=>'delete'}最后enable,否则不能进⾏其他操作hbase(main):038:0> enable 'member'6.插⼊⼏条记录put'member','scutshuxue','info:age','24'put'member','scutshuxue','info:birthday','1987-06-17'put'member','scutshuxue','info:company','alibaba'put'member','scutshuxue','address:contry','china'put'member','scutshuxue','address:province','zhejiang'put'member','scutshuxue','address:city','hangzhou'scan 'member'7.获取数据获取⼀个id的所有数据hbase(main):001:0>get 'member','scutshuxue'获取⼀个id,⼀个列族的所有数据hbase(main):002:0>get 'member','scutshuxue','info'获取⼀个id,⼀个列族中⼀个列的所有数据hbase(main):002:0>get 'member','scutshuxue','info:age'通过timestamp来获取两个版本的数据hbase(main):010:0>get 'member','scutshuxue',{COLUMN=>'info:age',TIMESTAMP=>1321586238965}8. 更新⼀条记录将scutshuxue的年龄改成99hbase(main):004:0>put 'member','scutshuxue','info:age' ,'99'10.查询表中有多少⾏:hbase(main):019:0>count 'member'11.删除整⾏hbase(main):001:0>deleteall 'member','xiaofeng'12.删除id为xiaofeng的值的‘info:age’字段hbase(main):016:0>delete 'member','temp','info:age'13.给‘xiaofeng’这个id增加'info:go'字段,并使⽤counter实现递增hbase(main):041:0> incr 'member','xiaofeng','info:go'连续执⾏incr以上,COUNTER VALUE 的值会递增,通过get_counter获取当前count的值hbase(main):069:0>get_counter 'member','xiaofeng','info:age'hbase(main):058:0>get 'member','xiaofeng','info:go'获取当前count的值hbase(main):069:0>get_counter 'member','xiaofeng','info:age'=============================相关删除=========================1.删除⼀个列族:先将表disable(如果表中之前有数据的话,会把数据清空)hbase(main):030:0> disable 'member'hbase(main):033:0> is_enabled 'member'hbase(main):034:0> alter 'member',{NAME=>'m_id',METHOD=>'delete'}最后enable,否则不能进⾏其他操作hbase(main):038:0> enable 'member'2.删除整⾏hbase(main):001:0>deleteall 'scores','xiaofeng'3.将整张表清空:hbase(main):035:0>truncate 'scores'hbase是先将掉disable掉,然后drop掉后重建表来实现truncate的功能4.drop⼀个表(先disable,在drop)hbase(main):029:0>disable 'scores'hbase(main):029:0>drop 'scores'=====================基本操作=========================================== 1.查询表是否存在hbase(main):021:0>exists 'scores'2.判断表是否enablehbase(main):034:0>is_enabled 'scores'3.判断表是否disablehbase(main):032:0>is_disabled 'scores'4.列出所有的表hbase(main):028:0>list5.创建表memberhbase(main):025:0> create 'member','m_id','address','info'6.查看表描述hbase(main):028:0> describe 'member'=================查询操作================================================1.scan '表名' #查看所有数据2.get是根据主键查看⼀列get '表名','主键'[,'列簇名'][,'列簇名:列名']=============================================================过滤查询表的内容1.FirstKeyOnlyFilter() 得到每⼀⾏的列族info的第⼀个列hbase(main):002:0> scan 'student',{COLUMNS=>'info',FILTER=>"(FirstKeyOnlyFilter())"}2.查看列簇是info,LIMIT是显⽰的⾏数,STARTROW是开始⾏对应的是主键hbase(main):008:0> scan 'student',{COLUMNS=>['info'],LIMIT=>4,STARTROW=>'200977100710'}3.查看列簇是info,namehbase(main):007:0> scan 'student',{COLUMNS=>['info','name'],LIMIT=>4,STARTROW=>'200977100710'}。
HBaseshell命令情况总结通过shell操作HBase(在C60U10版本)1.登录连接客户端所在服务器:**************.0.24进⼊安装⽬录:cd /opt/hadoopclient导⼊环境变量:source bigdata_env进⼊HBase: cd HBase/hbase/bin启动HBase: hbase shell注:此版本不需要认证(查看是否认证klist,认证kinit ⽤户名)(admin Huawei@123)2.HBase shell命令2.1.⼀般操作2.1.1.查询服务器状态:status语法:status⽰例:四个服务器2.1.2.查询HBase版本:version语法:version⽰例:2.1.3.查看当前⽤户:whoami语法:whoami⽰例:2.1.4.退出HBase shell:exit语法:exit⽰例:2.1.5.关闭HBase集群:shutdown语法:shutdown说明:shutdown表⽰关闭HBase服务,必须重新启动HBase才可以恢复,exit只是退出HBase shell,退出之后完全可以重新进⼊。
⽰例:2.1.6.帮助命令:help “命令”语法:help “命令”2.2.DDL操作2.2.1.创建表:create语法:create ‘表名’, {NAME=>’列族名’,VERSIONS=>版本数},{…},…说明:定义表的时候只需要指定列族名,列名在put时动态指定。
⽰例:创建⼀个表名为t1含有三个列族f1,f2,f3。
只指定列族名时可以如下简写。
2.2.2.删除表:drop语法:drop ‘表名’说明:⾸先disable,然后drop⽰例:2.2.3.查看表结构:describe语法:describe ‘表名’说明:只能查看到列族,对列族下的列怎么查看?⽰例:2.2.4.修改表结构:alter语法:说明:修改表结构前必须先disable⽰例1:添加⼀个列族,添加列族f4⽰例2:删除⼀个列族,删除列族f4.⽰例3:修改列族属性,将f1,f2,f3的VERSIONS修改为3⽰例4:修改表属性,如MAX_FILESIZE,MEMSTORE_FLUSHSIZE,READONLY, DEFERRED_LOG_FLUSH ⽰例5:添加⼀个表协同处理器2.2.5.列出所有表:list语法:list⽰例:下⾯显⽰三个表2.2.6.查询表是否存在:exists语法:exists ‘表名’⽰例:2.2.7.查询表是否可⽤:enable,disable,is_enabled,is_disabled语法:enable ‘表名’,disable ‘表名’,is_enabled ‘表名’,is_disabled ‘表名’⽰例:2.3.DML操作2.3.1.添加记录:put语法:put ‘表名’, ’⾏键名’, ’列族名:列名’, ’值’, ‘时间戳’说明:⼀次只能向指定⾏的⼀个列族中的⼀个列put⼀个值,因此必须指定⾏名,列族名:列名。
HBase是一种分布式的、可伸缩的、大数据存储服务,使用Hadoop的HDFS作为底层存储。
要连接HBase,需要配置一些参数,以下是一些常见的连接参数:
1.hbase.zookeeper.quorum: 指定ZooKeeper集群的地址,HBase通过
ZooKeeper进行协调。
2.hbase.zookeeper.property.clientPort: 指定ZooKeeper的端口号,默认
为2181。
3.hbase.master: 指定HBase主服务器的地址和端口号,格式为
hostname:port。
4.hbase.zookeeper.session.timeout: 指定ZooKeeper的会话超时时间,单
位为毫秒。
5.hbase.client.scanner.caching: 指定扫描器缓存大小,可以提高扫描性
能。
6.hbase.client.keyvalue.maxsize: 指定单个键值对的最大大小,超过此大
小的键值对将不会被存储。
7.hbase.client.retries.number: 指定客户端重试次数,当与HBase服务器
通信失败时可以尝试重新连接。
这些参数可以在连接HBase时进行配置,以便更好地控制连接和通信的行为。
具体的参数值取决于实际需求和环境配置。
Hbase记录-HBase基本操作(⼀)HBase创建表可以使⽤命令创建⼀个表,在这⾥必须指定表名和列族名。
在HBase shell中创建表的语法如下所⽰。
create ‘<table name>’,’<column family>’⽰例下⾯给出的是⼀个表名为emp的样本模式。
它有两个列族:“personal data”和“professional data”。
Row key personal data professional data在HBase shell创建该表如下所⽰。
hbase(main):002:0> create 'emp', 'personal data', ’professional data’它会给下⾯的输出。
0 row(s) in 1.1300 seconds=> Hbase::Table - emp验证创建可以验证是否已经创建,使⽤ list 命令如下所⽰。
在这⾥,可以看到创建的emp表。
hbase(main):002:0> listTABLEemp2 row(s) in 0.0340 seconds使⽤Java API创建⼀个表可以使⽤HBaseAdmin类的createTable()⽅法创建表在HBase中。
这个类属于org.apache.hadoop.hbase.client 包。
下⾯给出的步骤是来使⽤Java API创建表在HBase中。
第1步:实例化HBaseAdmin这个类需要配置对象作为参数,因此初始实例配置类传递此实例给HBaseAdmin。
Configuration conf = HBaseConfiguration.create();HBaseAdmin admin = new HBaseAdmin(conf);第2步:创建TableDescriptorHTableDescriptor类是属于org.apache.hadoop.hbase。
HBase官方文档Copyright © 2010 Apache Software Foundation,盛大游戏-数据仓库团队-颜开(译)Abstract这是Apache HBase的官方文档, Hbase是一个分布式,版本化(versioned),构建在Apache Hadoop和Apache ZooKeeper上的列数据库.我(译者)熟悉Hbase的源代码,从事Hbase的开发运维工作,如果有什么地方不清楚,欢迎一起讨论。
邮箱yankaycom@Table of Contents序1. 入门1.1. 介绍1.2. 快速开始1.2.1. 下载解压最新版本1.2.2. 启动 HBase1.2.3. Shell 练习1.2.4. 停止 HBase1.2.5. 下一步该做什么1.3. 慢速开始(相对快速开始)1.3.1. 需要的软件1.3.2. HBase运行模式:单机和分布式1.3.3. 配置例子2. 升级2.1. 从HBase 0.20.x or 0.89.x 升级到 HBase 0.90.x3. 配置3.1. hbase-site.xml 和 hbase-default.xml3.1.1. HBase 默认配置3.2. hbase-env.sh3.3. log4j.properties3.4. 重要的配置3.5. 必须的配置3.6. 推荐的配置3.6.1. zookeeper.session.timeout3.6.2. hbase.regionserver.handler.count3.6.3. 大内存机器的配置3.6.4. LZO 压缩3.6.5. 更大的 Regions3.6.6. 管理 Splitting3.7. 连接Hbase集群的客户端配置和依赖3.7.1. Java客户端配置4. The HBase Shell4.1. 使用脚本4.2. Shell 技巧4.2.1. irbrc4.2.2. LOG 时间转换4.2.3. Debug5. 构建 HBase5.1. 将一个 HBase release 加入到 Apache's Maven Repository6. Developers6.1. IDEs6.1.1. Eclipse6.2. 单元测试6.2.1. Mocito7. HBase 和 MapReduce7.1. 默认 HBase MapReduce 分割器(Splitter)7.2. HBase Input MapReduce 例子7.3. 在一个MapReduce Job中访问其他的HBase Tables7.4. 预测执行8. HBase 的 Schema 设计8.1. Schema 创建8.2. column families的数量8.3. 单调递增Row Keys/时序数据(log)8.4. 尽量最小化row和column的大小8.5. 版本的时间9. Metrics9.1. Metric 安装9.2. RegionServer Metrics9.2.1. hbase.regionserver.blockCacheCount9.2.2. hbase.regionserver.blockCacheFree9.2.3. hbase.regionserver.blockCacheHitRatio9.2.4. hbase.regionserver.blockCacheSize9.2.5. pactionQueueSize9.2.6. hbase.regionserver.fsReadLatency_avg_time9.2.7. hbase.regionserver.fsReadLatency_num_ops9.2.8. hbase.regionserver.fsSyncLatency_avg_time9.2.9. hbase.regionserver.fsSyncLatency_num_ops9.2.10. hbase.regionserver.fsWriteLatency_avg_time9.2.11. hbase.regionserver.fsWriteLatency_num_ops9.2.12. hbase.regionserver.memstoreSizeMB9.2.13. hbase.regionserver.regions9.2.14. hbase.regionserver.requests9.2.15. hbase.regionserver.storeFileIndexSizeMB9.2.16. hbase.regionserver.stores9.2.17. hbase.regionserver.storeFiles10. 跨集群复制11. 数据模型11.1. 概念视图11.2. 物理视图11.3. 表11.4. 行11.5. Column Family11.6. Cells11.7. 版本11.7.1. Hbase的操作(包含版本操作)11.7.2. 现有的限制12. 架构12.1. 客户端12.1.1. 连接12.1.2. 写缓冲和批量操作12.1.3. Filters12.2. Daemons12.2.1. Master12.2.2. RegionServer12.3. Regions12.3.1. Region大小12.3.2. Region Splits12.3.3. Region负载均衡12.3.4. Store12.4. Write Ahead Log (WAL)12.4.1. 目的12.4.2. WAL Flushing12.4.3. WAL Splitting13. 性能调优13.1. Java13.1.1. 垃圾收集和HBase13.2. 配置13.2.1. Regions的数目13.2.2. 管理压缩13.2.3. 压缩13.2.4. hbase.regionserver.handler.count13.2.5. hfile.block.cache.size13.2.6. hbase.regionserver.global.memstore.upperLimit13.2.7. hbase.regionserver.global.memstore.lowerLimit13.2.8. hbase.hstore.blockingStoreFiles13.2.9. hbase.hregion.memstore.block.multiplier13.3. Column Families的数目13.4. 数据聚集13.5. 批量Loading13.5.1. Table创建: 预创建Regions13.6. HBase客户端13.6.1. AutoFlush13.6.2. Scan Caching13.6.3. Scan 属性选择13.6.4. 关闭 ResultScanners13.6.5. 块缓存13.6.6. Row Keys的负载优化14. Bloom Filters14.1. 配置14.1.1. HColumnDescriptor 配置14.1.2. io.hfile.bloom.enabled 全局关闭开关14.1.3. io.hfile.bloom.error.rate14.1.4. io.hfile.bloom.max.fold14.2. Bloom StoreFile footprint14.2.1. StoreFile中的BloomFilter, FileInfo数据结构14.2.2. 在 StoreFile 元数据中的BloomFilter entries15. Hbase的故障排除和Debug15.1. 一般准则15.2. Logs15.2.1. Log 位置15.3. 工具15.3.1. 15.3.2. tail15.3.3. top15.3.4. jps15.3.5. jstack15.3.6. OpenTSDB15.3.7. clusterssh+top15.4. 客户端15.4.1. ScannerTimeoutException15.5. RegionServer15.5.1. 启动错误15.5.2. 运行时错误15.5.3. 终止错误15.6. Master15.6.1. 启动错误15.6.2. 终止错误A. 工具A.1. HBase hbckA.2. HFile 工具A.3. WAL ToolsA.3.1. HLog 工具A.4. 压缩工具A.5. Node下线A.5.1. 依次重启B. HBase中的压缩B.1. 测试压缩工具B.2. hbase.regionserver.codecsB.3. LZOB.4. GZIPC. FAQD. YCSB: 雅虎云服务测试和HbaseIndexList of Tables11.1. 表 webtable11.2. ColumnFamily anchor11.3. ColumnFamily contents序这本书是HBase的官方指南。
两个月前使用过hbase,现在最基本的命令都淡忘了,留一个备查~要注意shutdown与exit之间的不同:shutdown表示关闭hbase服务,必须重新启动hbase才可以恢复,exit只是退出hbase shell,退出之后完全可以重新进入。
hbase使用坐标来定位表中的数据,行健是第一个坐标,下一个坐标是列族。
hbase是一个在线系统,和hadoop mapreduce的紧密结合又赋予它离线访问的功能。
hbase接到命令后存下变化信息或者写入失败异常的抛出,默认情况下。
执行写入时会写到两个地方:预写式日志(write-ahead log,也称hlog)和memstore,以保证数据持久化。
memstore是内存里的写入缓冲区。
客户端在写的过程中不会与底层的hf ile直接交互,当menstore写满时,会刷新到硬盘,生成一个新的hfile.hfile是hbase使用的底层存储格式。
menstore的大小由hbase-site.xml文件里的系统级属性h base.hregion.memstore.flush.size来定义。
hbase在读操作上使用了lru缓存机制(blockcache),blockcache设计用来保存从hfile里读入内存的频繁访问的数据,避免硬盘读。
每个列族都有自己的blockcache。
b lockcache中的block是hbase从硬盘完成一次读取的数据单位。
block是建立索引的最小数据单位,也是从硬盘读取的最小数据单位。
如果主要用于随机查询,小一点的block会好一些,但是会导致索引变大,消耗更多内存,如果主要执行顺序扫描,大一点的bloc k会好一些,block变大索引项变小,因此节省内存。
LRU是Least Recently Used 近期最少使用算法。
内存管理的一种页面置换算法,对于在内存中但又不用的数据块(内存块)叫做LRU,操作系统会根据哪些数据属于LRU而将其移出内存而腾出空间来加载另外的数据。
hbase数据库操作.实验内容与完成情况:(实验具体步骤和实验截图说明)(⼀)编程实现以下指定功能,并⽤ Hadoop 提供的 HBase Shell 命令完成相同任务:(1)列出 HBase 所有的表的相关信息,例如表名;列出所有数据表:列出表的结构查询表是否存在查询表是否可⽤.hbase;import java.io.IOException;public class ListTables {public static Configuration configuration;public static Connection connection;public static Admin admin;public static void listTables() throws IOException {init();HTableDescriptor[] hTableDescriptors = admin.listTables();for (HTableDescriptor hTableDescriptor : hTableDescriptors) {System.out.println("table name:"+ hTableDescriptor.getNameAsString());}close();}public static void init() {configuration = HBaseConfiguration.create();configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");try {connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void close() {try {if (admin != null) {admin.close();}if (connection != null) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {try {listTables();} catch (IOException e) {e.printStackTrace();}}}(2)在终端打印出指定的表的所有记录数据;源代码:package .hbase;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import java.io.IOException;public class ListTableData {public static Configuration configuration;public static Connection connection;public static Admin admin;public static void getData(String tableName)throws IOException{init();Table table=connection.getTable(TableName.valueOf(tableName)); Scan scan=new Scan();ResultScanner scanner=table.getScanner(scan);for(Result result:scanner){printRecoder(result);}System.out.println(“finish!”);close();}public static void printRecoder(Result result)throws IOException{for(Cell cell:result.rawCells()){System.out.println("⾏键:"+new String(CellUtil.cloneRow(cell)));System.out.print("列簇: " + new String(CellUtil.cloneFamily(cell)));System.out.print(" 列: " + new String(CellUtil.cloneQualifier(cell)));System.out.print(" 值: " + new String(CellUtil.cloneValue(cell)));System.out.println("时间戳: " + cell.getTimestamp());}}public static void init() {configuration = HBaseConfiguration.create();configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");try {connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void close() {try {if (admin != null) {admin.close();}if (connection != null) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {try {getData("person");} catch (IOException e) {e.printStackTrace();}}}(3)向已经创建好的表添加和删除指定的列族或列;添加数据源代码:package .hbase;import java.io.IOException;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.Put;import org.apache.hadoop.hbase.client.Table;public class InsertRow {public static Configuration configuration;public static Connection connection;public static Admin admin;public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val)throws IOException{ init();Table table=connection.getTable(TableName.valueOf(tableName));Put put=new Put(rowKey.getBytes());put.addColumn(colFamily.getBytes(),col.getBytes() ,val.getBytes());table.put(put);System.out.println("insert finish!");table.close();}public static void init() {configuration = HBaseConfiguration.create();configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");try {connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void close() {try {if (admin != null) {admin.close();}if (connection != null) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {try {insertRow("student","s002","score","math","100");} catch (IOException e) {e.printStackTrace();}}}删除数据源代码:package .hbase;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;public class DeleteRow {public static Configuration configuration;public static Connection connection;public static Admin admin;public static void deleteRow(String tableName, String rowKey, String colFamily, String col) throws IOException { init();Table table = connection.getTable(TableName.valueOf(tableName));Delete delete = new Delete(rowKey.getBytes());delete.addFamily(Bytes.toBytes(colFamily));delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));table.delete(delete);System.out.println("delete successful!");table.close();close();}public static void init() {configuration = HBaseConfiguration.create();configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");try {connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void close() {try {if (admin != null) {admin.close();}if (null != connection) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {try {deleteRow("student", "s002", "score", "math");} catch (IOException e) {e.printStackTrace();}}}(3)清空指定的表的所有记录数据;源代码:package .hbase;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 java.io.IOException;public class TruncateTable {public static Configuration configuration;public static Connection connection;public static Admin admin;public static void clearRows(String tableName) throws IOException { init();TableName tablename = TableName.valueOf(tableName);admin.disableTable(tablename);admin.truncateTable(tablename, false);System.out.println("delete table successful!");close();}public static void init() {configuration = HBaseConfiguration.create();configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");try {connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void close() {try {if (admin != null) {admin.close();}if (null != connection) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {try {clearRows("student");} catch (IOException e) {e.printStackTrace();}}}(5)统计表的⾏数。
hbase基础操作(最新版)目录1.HBase 简介2.HBase 基本操作2.1 创建表2.2 删除表2.3 插入数据2.4 查询数据2.5 更新数据2.6 删除数据正文【HBase 简介】HBase 是一个分布式、可扩展、高性能的列式存储系统,它基于 Google 的Bigtable 设计,是 Apache Hadoop 的一个子项目。
HBase 适用于海量数据的存储和实时读写操作,被广泛应用于大数据处理领域。
【HBase 基本操作】HBase 的基本操作主要包括创建表、删除表、插入数据、查询数据、更新数据和删除数据。
【创建表】在 HBase 中,表是由行和列组成的,表的结构是灵活可变的。
创建表时需要指定表名、列族和列。
例如,创建一个名为“user”的表,包含“id”、“name”和“age”三个列,可以使用如下命令:```create "user", "id", "name", "age"```【删除表】删除表时,需要使用“drop”命令,指定表名。
例如,删除名为“user”的表,可以使用如下命令:```drop "user"```【插入数据】在 HBase 中,数据以行形式存储,每行包含一个或多个列。
插入数据时,需要指定表名和行键。
例如,向名为“user”的表插入一条数据,行键为“1”,列族为“name”,列值为“张三”,可以使用如下命令:```put "user", "1", "name", "张三"```【查询数据】HBase 提供了多种查询数据的方法,如 get、scan 和 range 扫查等。
例如,查询名为“user”的表中行键为“1”的数据,可以使用如下命令:```get "user", "1"```【更新数据】在 HBase 中,可以通过 put 命令更新数据。