Hbase集群搭建

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

2、配置hbase环境

 cd  /home/master/hbase-2.2.5/conf

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

export JAVA_HOME=/usr/local/java/jdk8
export HBASE_MANAGES_ZK=false

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

<configuration>
	<property>     
		<name>hbase.rootdir</name>     
		<value>hdfs://master:9000/hbase</value>   
	</property>

	<property>   
		<name>hbase.cluster.distributed</name>
		<value>true</value>
	</property>

	<property>
		<name>hbase.master.port</name>
		<value>16000</value>
	</property>

	<property>   
		<name>hbase.zookeeper.quorum</name>
	     <value>master:2181,slave1:2181,slave2:2181</value>
	</property>

	<property>   
		<name>hbase.zookeeper.property.dataDir</name>
	     <value>/home/master/zookeeper-3.4.14/zkdata</value>
	</property>
</configuration>

(3)  regionservers添加集群主机

master
slave1
slave2

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

[root@master master]# 
ln -s /home/master/hadoop-2.9.2/etc/hadoop/core-site.xml   /home/master/hbase-2.25/conf/core-site.xml
[root@master master]# 
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分发到集群其他机器上

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

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

方式1: 
bin/hbase-daemon.sh start master
bin/hbase-daemon.sh start regionserver
方式2:
hbase-2.2.5/bin/start-hbase.sh  

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

3、脚本操作hbase

进入脚本
 bin/hbase shell

 命名空间namespace的常用操作命令

alter_namespace

修改命名空间的属性

create_namespace

创建命名空间

describe_namespace

查看命名空间的结构

drop_namespace

删除命名空间

list_namespace

查看HBase中所有的命名空间

list_namespace_tables

查看指定的命名空间中的所有表

表的常见命令

create

建表

alter

修改表

describe/desc

查看表结构

disable/disable_all

令表失效,在HBase中,只有失效的表才能删除/所有表失效

enable/enable_all

使表生效

drop/drop_all

删除表

exists

判断表是否存在

is_disabled/is_enabled

是否失效/生效

list

查询HBase所有的表

 

  (1) 创建表

     创建表名为country,列簇名为china的表

hbase(main):001:0> create  ‘country’,‘china’

    (2)插入数据

put 'country','中国','china:河南','郑州'
put 'country','中国','china:广东','广州'
put 'country','中国','china:江苏','南京'

(3)扫描表

scan  'country'

(4)更新字段

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

(5)查看指定列

get 'country','河南'

4、javaApi 操作hbase

package org.cn.fcw;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;

/**
 * Hello world!
 */
public class HBaseApp {


    public static Connection connection;

    static {
        try {
            //使用HBaseConfiguration的单例方法实例化
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "master");
            conf.set("hbase.zookeeper.property.clientPort", "2181");
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        try {
            String  tbName="country";
            boolean exist = isTableExist(tbName);
            if(exist){
                System.out.println("向"+tbName+"中添加数据");
                String columnFamily="china";
                String columnName="河南";
                String rowKey=tbName+"#"+columnFamily+"#"+columnName+"#"+System.currentTimeMillis();
                insertTable(tbName,rowKey,columnFamily,columnName,"信阳");
            }else{
                System.out.println(tbName+"-------表不存在{}");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static HBaseAdmin getAdmin() throws IOException {
        System.out.println("连接HBase集群");
        return (HBaseAdmin) connection.getAdmin();
    }

    //判断表是否存在
    public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        //在HBase中管理、访问表需要先创建HBaseAdmin对象
        //HBaseAdmin admin = new HBaseAdmin(conf);
        TableName table = TableName.valueOf(tableName);
        return getAdmin().tableExists(table);
    }


    //创建表
    public static void createTable(String tbName, String[] columnFamilies) throws IOException {
        if (isTableExist(tbName)) {
            System.out.println("表已经存在");
            return;
        }
        TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor = new TableDescriptorBuilder.ModifyableTableDescriptor(TableName.valueOf(tbName));
        for (String columnFamily : columnFamilies) {
            ColumnFamilyDescriptor columnFamilyDescriptor = new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(columnFamily.getBytes());
            tableDescriptor.setColumnFamily(columnFamilyDescriptor);
        }
        getAdmin().createTable(tableDescriptor);
    }


    //刪除表
    public static void deleteTable(String tbName) throws IOException {
        if (isTableExist(tbName)) {
            HBaseAdmin admin = getAdmin();
            admin.disableTable(TableName.valueOf(tbName));
            admin.deleteTable(TableName.valueOf(tbName));
            return;
        }
        System.out.println(tbName + "表不存在");
    }


    /**
     * 插入数据
     *
     * @param tbName       表名
     * @param rowKey       行键(来检索记录的主键)
     * @param columnFamily 列簇
     * @param column       列名
     * @param value        列值
     */
    public static void insertTable(String tbName, String rowKey, String columnFamily, String column, String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tbName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(columnFamily.getBytes(), column.getBytes(), value.getBytes());
        table.put(put);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值