hbase基本命令,api操作

hbase单节点安装

    启动一个单节点的hbase方便我们测试,学习。在官网下载hbase.tar.gz安装包  https://hbase.apache.org/

修改配置文件${hbase}/conf/hbase-env.sh,指定jdk位置

【hbase-env.sh】

export JAVA_HOME=/usr/local/jdk

【hbase-site.xml】

指定hbase的data路径,zookeeper的data路径

<configuration>
  <property>
    <name>hbase.rootdir</name>
    <value>file:///home/hadoop/hbase/data</value>
  </property>
  <property>
    <name>hbase.zookeeper.property.dataDir</name>
    <value>/home/hadoop/hbase/zkData</value>
  </property>
  <property>
    <name>hbase.unsafe.stream.capability.enforce</name>
    <value>false</value>
    <description>
      Controls whether HBase will check for stream capabilities (hflush/hsync).
      Disable this if you intend to run on LocalFileSystem, denoted by a rootdir
      with the 'file://' scheme, but be mindful of the NOTE below.
      WARNING: Setting this to false blinds you to potential data loss and
      inconsistent system state in the event of process and/or node failures. If
      HBase is complaining of an inability to use hsync or hflush it's most
      likely not a false positive.
    </description>
  </property>
</configuration>
hbase单节点启动
//start-hbase.sh
>start-hbase.sh
//查看webUI,“localhost:16010”


hbase_shell操作

$>hbase shell  //登录shell终端
$hbase>help								//查看帮助
$hbase>help	'list_namespace'			//查看(特定命令)名称空间命令帮助
$hbase>list_namespace					//列出名字空间(数据库)
$hbase>list_namespace_tables 'defalut'	//列出名字空间(数据库)
$hbase>create_namespace 'ns1'			//创建名字空间
$hbase>help 'create'
$hbase>create 'ns1:t1','f1'				//创建表,指定空间下
$hbase>put 'ns1:t1','row1','f1:id',100		//插入数据
$hbase>put 'ns1:t1','row1','f1:name','tom'	//

$hbase>get 'ns1:t1','row1'					//查询指定row
$hbase>scan 'ns1:t1'						//扫描表
$hbase>flush 'ns1:t1'		//清理内存数据到磁盘。
$hbase>count 'ns1:t1'		//统计函数
$hbase>disable 'ns1:t1'		//删除表之前需要禁用表
$hbase>drop 'ns1:t1'		//
$hbase>scan 'hbase:meta'	//查看元数据表
$hbase>split 'ns1:t1'		//切割表 
$hbase>split ''			//切割区域
//给表添加列簇,先禁用表
>disable 'test'
//如果表在default空间中,可以不加空间限定
>disable 'default:test'
//修改表,列簇是“NAME=>'cf2'”,最多保留3个版本的数据
>alter 'test',{NAME=>'cf2',VERSIONS=>3}
//然后enable表
>enable 'test'

java api操作

需要把“hbase-site.xml”放到类路径下

1.put get

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;
import org.junit.Test;

public class TestCRUD {

    @Test
    public void testPut() throws Exception {
        //创建配置文件
        Configuration conf = HBaseConfiguration.create();
        //创建连接
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("default:test");
        //根据表名获得表对象
        Table table = conn.getTable(tableName);
        byte[] row1 = Bytes.toBytes("row2");
        //利用put对象向表中插入数据,new的时候需要指定rowkey
        Put put = new Put(row1);
        byte[] cf1 = Bytes.toBytes("cf1");
        byte[] col = Bytes.toBytes("id");
        byte[] value = Bytes.toBytes("101");
        //put对象添加一列,需要参数列簇,列,值
        put.addColumn(cf1,col,value);
        table.put(put);
        conn.close();
    }
    /**
     * 根据rowkey,列簇,列,查询value
     * @throws Exception
     */
    @Test
    public void testGet() throws Exception{
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("default:test");
        Table table = conn.getTable(tableName);
        byte[] row2 = Bytes.toBytes("row1");
        //通过get对象,执行对表中一行的get操作
        Get get_row2 = new Get(row2);
        Result r = table.get(get_row2);
        byte[] cf1 = Bytes.toBytes("cf1");
        byte[] col = Bytes.toBytes("id");
        byte[] idvalue = r.getValue(cf1,col);
        System.out.println(Bytes.toString(idvalue));
    }
}

hbase中的一些概念

写前日志

WAL,"write ahead log",写前日志。位于、hbase/meta下,表的操作先写WAL里
如果插入大量数据
    table.setAutoFlush(false);//关闭表的自动flush
    put.setWriteToWAL(false);//关闭写前日志
    table.flushCommits();//提交日志

hbase基于hdfs

相同列簇的数据存放在一个文件中。hdfs://ip:port/hbase/data/${名称空间}/${表名}/${区域名称}/${列簇名称}/${文件名}
[WAL目录结构构成]
hdfs://s201:8020/hbase/WALs/${区域服务器名称,主机名,端口号,时间戳}/

client端交互过程
0.hbase集群启动时,master负责分配区域到指定区域服务器。
1.联系zk,找出meta表所在rs(regionserver)
/hbase/meta-region-server
2.定位row key,找到对应region server
3.缓存信息在本地。
4.联系RegionServer
5.HRegionServer负责open HRegion对象,为每个列族创建Store对象,Store包含多个StoreFile实例,
 他们是对HFile的轻量级封装。每个Store还对应了一个MemStore,用于内存存储数据。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值