Hbase单机模式部署

淩亂°似流年 2021-09-25 16:02 490阅读 0赞

  HBase是一个分布式、面向列的开源数据库,是Apache Hadoop项目的子项目,适用于非结构化数据存储的数据库。在Hadoop家族中,很多产品为HBase提供服务:

  • Hadoop HDFS为HBase提供了高可靠性的底层存储支持;
  • Hadoop MapReduce为HBase提供了高性能的计算能力;
  • Zookeeper为HBase提供了稳定服务和failover机制;
  • Pig和Hive为HBase提供了高层语言支持,使得在HBase上进行数据统计处理变的非常简单;
  • Sqoop为HBase提供了方便的RDBMS数据导入功能,使得传统数据库数据向HBase中迁移变的非常方便。

1 安装

1.1 下载、解压

从http://hbase.apache.org/找最新的稳定版下载,本文使用的是hbase-0.98.6.1-hadoop2-bin.tar.gz。

解压缩,然后进入到那个要解压的目录:

  1. $ tar xzvf hbase-0.98.6.1-hadoop2-bin.tar.gz
  2. $ cd hbase-0.98.6.1-hadoop2/

1.2 简单配置

这一步可以选择跳过。

此处需要配置的是$HBASE_HOME/conf/hbase-site.xml中的hbase.rootdir,即HBase保存数据的目录。如果不进行配置,默认hbase.rootdir指向/tmp/hbase-${user.name},因为系统重启时会清理/tmp目录,所以重启后会丢失数据。如果是在分布式模式部署中,需要提供的是HDFS上的目录位置。

  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  3. <configuration>
  4. <property>
  5. <name>hbase.rootdir</name>
  6. <value>file:/home/lxh/hadoop/hbase</value>
  7. </property>
  8. </configuration>

2 启动HBase

直接使用start-hbase.sh脚本启动

  1. $ ./bin/start-hbase.sh

启动正常时,在$HBASE_HOME/logs/hbase-lxh-master-ubuntu.log日志的中会提示下面内容:

  1. 2014-10-14 09:47:07,189 INFO [M:0;ubuntu:40435] master.HMaster: Master has completed initialization

通过jps查询进程,会发现多了HMaster这个进程:

  1. 2694 HMaster

3 初探HBase

3.1 启动shell

进入HBase提供的shell中进行测试。

  1. $ ./bin/hbase shell
  2. 2014-10-14 10:14:55,859 INFO [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available
  3. HBase Shell; enter 'help<RETURN>' for list of supported commands.
  4. Type "exit<RETURN>" to leave the HBase Shell
  5. Version 0.98.6.1-hadoop2, r96a1af660b33879f19a47e9113bf802ad59c7146, Sun Sep 14 21:27:25 PDT 2014
  6. hbase(main):001:0>

3.2 查看帮助

通过键入help命令查看在HBase的shell中的命令。

  1. hbase(main):001:0> help
  2. HBase Shell, version 0.98.6.1-hadoop2, r96a1af660b33879f19a47e9113bf802ad59c7146, Sun Sep 14 21:27:25 PDT 2014
  3. Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
  4. Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.
  5. COMMAND GROUPS:
  6. Group name: general
  7. Commands: status, table_help, version, whoami
  8. Group name: ddl
  9. Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, show_filters
  10. Group name: namespace
  11. Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables
  12. Group name: dml
  13. Commands: append, count, delete, deleteall, get, get_counter, incr, put, scan, truncate, truncate_preserve
  14. Group name: tools
  15. Commands: assign, balance_switch, balancer, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, flush, hlog_roll, major_compact, merge_region, move, split, trace, unassign, zk_dump
  16. Group name: replication
  17. Commands: add_peer, disable_peer, enable_peer, list_peers, list_replicated_tables, remove_peer, set_peer_tableCFs, show_peer_tableCFs
  18. Group name: snapshots
  19. Commands: clone_snapshot, delete_snapshot, list_snapshots, rename_snapshot, restore_snapshot, snapshot
  20. Group name: security
  21. Commands: grant, revoke, user_permission
  22. Group name: visibility labels
  23. Commands: add_labels, clear_auths, get_auths, set_auths, set_visibility
  24. SHELL USAGE:
  25. Quote all names in HBase Shell such as table and column names. Commas delimit
  26. command parameters. Type <RETURN> after entering a command to run it.
  27. Dictionaries of configuration used in the creation and alteration of tables are
  28. Ruby Hashes. They look like this:
  29. {'key1' => 'value1', 'key2' => 'value2', ...}
  30. and are opened and closed with curley-braces. Key/values are delimited by the
  31. '=>' character combination. Usually keys are predefined constants such as
  32. NAME, VERSIONS, COMPRESSION, etc. Constants do not need to be quoted. Type
  33. 'Object.constants' to see a (messy) list of all constants in the environment.
  34. If you are using binary keys or values and need to enter them in the shell, use
  35. double-quote'd hexadecimal representation. For example:
  36. hbase> get 't1', "key\x03\x3f\xcd"
  37. hbase> get 't1', "key\003\023\011"
  38. hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"
  39. The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
  40. For more on the HBase Shell, see http://hbase.apache.org/docs/current/book.html

3.3 create创建表

首先创建一个名为test的表,这个表只有一个列族为cf。可以通过list命令列出所有的表来检查创建情况。

  1. hbase(main):002:0> create 'test', 'cf'
  2. 0 row(s) in 0.4330 seconds
  3. => Hbase::Table - test
  4. hbase(main):003:0> list
  5. TABLE
  6. test
  7. 1 row(s) in 0.0590 seconds
  8. => ["test"]

3.4 put插入数据

test表已经创建成功,通过put 'table', 'row', 'col-pre:col-name', 'value'向其中插入数据。table至表名,row指每行的键key,col-pre是列族前缀,col-name是列名,列族前缀与列名之间通过冒号隔开,value是值value。

  1. hbase(main):005:0> put 'test', 'row1', 'cf:a', 'value1'
  2. 0 row(s) in 0.1380 seconds
  3. hbase(main):006:0> put 'test', 'row2', 'cf:b', 'value2-b'
  4. 0 row(s) in 0.0130 seconds
  5. hbase(main):007:0> put 'test', 'row2', 'cf:c', 'value2-c'
  6. 0 row(s) in 0.0100 seconds
  7. hbase(main):008:0> put 'test', 'row3', 'cf', 'value3'
  8. 0 row(s) in 0.0110 seconds
  9. hbase(main):011:0> put 'test', 'row3', 'cf:e', 'value3-e'
  10. 0 row(s) in 0.0060 seconds

3.5 scan扫描全表

通过scan 'table'命令查询表test的数据:

  1. hbase(main):012:0> scan 'test'
  2. ROW COLUMN+CELL
  3. row1 column=cf:a, timestamp=1413253976039, value=value1
  4. row2 column=cf:b, timestamp=1413253980776, value=value2-b
  5. row2 column=cf:c, timestamp=1413253985691, value=value2-c
  6. row3 column=cf:, timestamp=1413253990953, value=value3
  7. row3 column=cf:e, timestamp=1413254206302, value=value3-e
  8. 3 row(s) in 0.0430 seconds

3.6 get查询某一行

通过get 'table', 'row'命令查询某一行数据:

  1. hbase(main):013:0> get 'test', 'row1'
  2. COLUMN CELL
  3. cf:a timestamp=1413253976039, value=value1
  4. 1 row(s) in 0.0150 seconds
  5. hbase(main):014:0> get 'test', 'row2'
  6. COLUMN CELL
  7. cf:b timestamp=1413253980776, value=value2-b
  8. cf:c timestamp=1413253985691, value=value2-c
  9. 2 row(s) in 0.0120 seconds
  10. hbase(main):015:0> get 'test', 'row3'
  11. COLUMN CELL
  12. cf: timestamp=1413253990953, value=value3
  13. cf:e timestamp=1413254206302, value=value3-e
  14. 2 row(s) in 0.0050 seconds

3.7 disable使表无效

disable 'table'命令可以使表无效,表并没有删除,但是不能进行查询等操作。

  1. hbase(main):017:0> disable 'test'
  2. 0 row(s) in 1.4850 seconds

如果此时再通过get 'table', 'row'查询,则会报错:

  1. hbase(main):018:0> get 'test', 'row3'
  2. COLUMN CELL
  3. ERROR: test is disabled.

3.8 enable使表有效

对于无效的表,可以使用enable 'table'命令使其有效,此时可以进行一系列对表的操作:

  1. hbase(main):020:0> enable 'test'
  2. 0 row(s) in 0.5540 seconds
  3. hbase(main):021:0> get 'test', 'row3'
  4. COLUMN CELL
  5. cf: timestamp=1413253990953, value=value3
  6. cf:e timestamp=1413254206302, value=value3-e
  7. 2 row(s) in 0.0160 seconds

3.9 drop删除表

drop 'table'命令可以删除表,该表必须是无效的表,即通过disable 'table'命令操作的表。

  1. hbase(main):030:0> drop 'test'
  2. 0 row(s) in 0.2300 seconds

3.10 关闭shell

与其他shell类似,退出shell的命令是exit

  1. hbase(main):031:0> exit

4 停止HBase

直接使用脚本stop-hbase.sh停止。

  1. $ ./bin/stop-hbase.sh
  2. stopping hbase....................

发表评论

表情:
评论列表 (有 0 条评论,490人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Hbase单机模式部署

      HBase是一个分布式、面向列的开源数据库,是Apache Hadoop项目的子项目,适用于非结构化数据存储的数据库。在Hadoop家族中,很多产品为HBase提供服务: