Hbase集群搭建

女爷i 2023-02-18 03:21 90阅读 0赞

1、官网下载Hbase解压值指定目录(在master机器上)

20200615212541513.png

2、配置hbase环境

cd /home/master/hbase-2.2.5/conf

(1)修改hbase-env.sh配置

  1. export JAVA_HOME=/usr/local/java/jdk8
  2. export HBASE_MANAGES_ZK=false

(2)修改hbase-site.xml配置

  1. <configuration>
  2. <property>
  3. <name>hbase.rootdir</name>
  4. <value>hdfs://master:9000/hbase</value>
  5. </property>
  6. <property>
  7. <name>hbase.cluster.distributed</name>
  8. <value>true</value>
  9. </property>
  10. <property>
  11. <name>hbase.master.port</name>
  12. <value>16000</value>
  13. </property>
  14. <property>
  15. <name>hbase.zookeeper.quorum</name>
  16. <value>master:2181,slave1:2181,slave2:2181</value>
  17. </property>
  18. <property>
  19. <name>hbase.zookeeper.property.dataDir</name>
  20. <value>/home/master/zookeeper-3.4.14/zkdata</value>
  21. </property>
  22. </configuration>

(3) regionservers添加集群主机

  1. master
  2. slave1
  3. slave2

(4)软连接hadoop配置文件到hbase:

  1. [root@master master]#
  2. ln -s /home/master/hadoop-2.9.2/etc/hadoop/core-site.xml /home/master/hbase-2.25/conf/core-site.xml
  3. [root@master master]#
  4. ln -s /home/master/hadoop-2.9.2/etc/hadoop/hdfs-site.xml /home/master/hbase-2.25/conf/hdfs-site.xml

(5)将hbase-2.2.5分发到集群其他机器上

  1. cp hbase-2.2.5 root@slave1:/home/master
  2. cp hbase-2.2.5 root@slave2:/home/master

(6)启动hbase集群,由于hbase依赖hdfs和zookeeper,需要先启动hdfs和zookeeper

  1. 方式1
  2. bin/hbase-daemon.sh start master
  3. bin/hbase-daemon.sh start regionserver
  4. 方式2
  5. hbase-2.2.5/bin/start-hbase.sh

启动成功后访问hbase管理页面 http://master:16010

3、脚本操作hbase

  1. 进入脚本
  2. bin/hbase shell

(1) 创建表

  1. 创建表名为country,列簇名为china的表
  2. hbase(main):001:0> create country’,‘china
  3. 2)插入数据
  4. put 'country','中国','china:河南','郑州'
  5. put 'country','中国','china:广东','广州'
  6. put 'country','中国','china:江苏','南京'

(3)扫描表

  1. scan 'country'

(4)更新字段

  1. put 'country','中国','china:广东','深圳'

(5)查看指定列

  1. get 'country','河南'

4、javaApi 操作hbase

  1. package org.cn.fcw;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.*;
  4. import org.apache.hadoop.hbase.client.*;
  5. import java.io.IOException;
  6. /**
  7. * Hello world!
  8. */
  9. public class HBaseApp {
  10. public static Connection connection;
  11. static {
  12. try {
  13. //使用HBaseConfiguration的单例方法实例化
  14. Configuration conf = HBaseConfiguration.create();
  15. conf.set("hbase.zookeeper.quorum", "master");
  16. conf.set("hbase.zookeeper.property.clientPort", "2181");
  17. connection = ConnectionFactory.createConnection(conf);
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. public static void main(String[] args) {
  23. try {
  24. String tbName="country";
  25. boolean exist = isTableExist(tbName);
  26. if(exist){
  27. System.out.println("向"+tbName+"中添加数据");
  28. String columnFamily="china";
  29. String columnName="河南";
  30. String rowKey=tbName+"#"+columnFamily+"#"+columnName+"#"+System.currentTimeMillis();
  31. insertTable(tbName,rowKey,columnFamily,columnName,"信阳");
  32. }else{
  33. System.out.println(tbName+"-------表不存在{}");
  34. }
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. private static HBaseAdmin getAdmin() throws IOException {
  40. System.out.println("连接HBase集群");
  41. return (HBaseAdmin) connection.getAdmin();
  42. }
  43. //判断表是否存在
  44. public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
  45. //在HBase中管理、访问表需要先创建HBaseAdmin对象
  46. //HBaseAdmin admin = new HBaseAdmin(conf);
  47. TableName table = TableName.valueOf(tableName);
  48. return getAdmin().tableExists(table);
  49. }
  50. //创建表
  51. public static void createTable(String tbName, String[] columnFamilies) throws IOException {
  52. if (isTableExist(tbName)) {
  53. System.out.println("表已经存在");
  54. return;
  55. }
  56. TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor = new TableDescriptorBuilder.ModifyableTableDescriptor(TableName.valueOf(tbName));
  57. for (String columnFamily : columnFamilies) {
  58. ColumnFamilyDescriptor columnFamilyDescriptor = new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(columnFamily.getBytes());
  59. tableDescriptor.setColumnFamily(columnFamilyDescriptor);
  60. }
  61. getAdmin().createTable(tableDescriptor);
  62. }
  63. //刪除表
  64. public static void deleteTable(String tbName) throws IOException {
  65. if (isTableExist(tbName)) {
  66. HBaseAdmin admin = getAdmin();
  67. admin.disableTable(TableName.valueOf(tbName));
  68. admin.deleteTable(TableName.valueOf(tbName));
  69. return;
  70. }
  71. System.out.println(tbName + "表不存在");
  72. }
  73. /**
  74. * 插入数据
  75. *
  76. * @param tbName 表名
  77. * @param rowKey 行键(来检索记录的主键)
  78. * @param columnFamily 列簇
  79. * @param column 列名
  80. * @param value 列值
  81. */
  82. public static void insertTable(String tbName, String rowKey, String columnFamily, String column, String value) throws IOException {
  83. Table table = connection.getTable(TableName.valueOf(tbName));
  84. Put put = new Put(rowKey.getBytes());
  85. put.addColumn(columnFamily.getBytes(), column.getBytes(), value.getBytes());
  86. table.put(put);
  87. }
  88. }

发表评论

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

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

相关阅读

    相关 hbase

    1.上传hbase安装包(测试使用的是hbase1.2.3) 2.解压 3.配置hbase集群,要修改3个文件(首先zk集群已经安装好了) 注意:要把hadoop的hd