【4】安装hbase

环境准备

  1. jdk
  2. hadoop
  3. zookeeper
  4. hbase-2.1.0-bin.tar.gz

安装步骤

  1. 解压重命名至/home/common/hbase
tar -zxvf hbase-2.1.0-bin.tar.gz
mv hbase-2.1.0 hbase
  1. 配置环境变量
vim /etc/profile

#添加如下内容
export HBASE_HOME=/home/common/hbase
export PATH=$PATH:$HBASE_HOME/bin

#使配置文件生效
source /etc/profile
  1. 修改配置文件hbase-env.sh,在hbase/conf目录下
export JAVA_HOME=本机的JDK地址
# 使用外部zookeeper,不用hbase自带的
export HBASE_MANAGES_ZK=false
  1. 修改配置文件hbase-site.xml
 <configuration>
  <property>
  <name>hbase.master</name> 
  #hbasemaster的主机和端口
  <value>vm01:60000</value>
  </property>
  <property>
  <name>hbase.master.maxclockskew</name>    
  #时间同步允许的时间差
  <value>180000</value>
  </property>
  <property>
  <name>hbase.rootdir</name>
  <value>hdfs://vm01:9000/hbase</value>
  #hbase共享目录,持久化hbase数据
  </property>
  <property>
  <name>hbase.cluster.distributed</name>  
  #是否分布式运行,false即为单机
  <value>true</value>
  </property>
  <property>
  <name>hbase.zookeeper.quorum</name>
  #zookeeper地址
  <value>vm01, vm02,vm03</value>
  </property>
  <property>
  <name>hbase.zookeeper.property.dataDir</name>
  #zookeeper配置信息位置
  <value>/home/common/hbase/tmp/zookeeper</value>
  </property>
</configuration>
  1. 修改regionservers,配置从节点即可
vm02
vm03
  1. 拷贝hbase到其他节点
scp -r /home/common/hbase root@vm02:/home/common
scp -r /home/common/hbase root@vm03:/home/common
  1. 启动zk集群,每个节点的zk/bin目录下

./zkServer.sh start

  1. 启动hdfs集群,主节点hadoop/sbin目录下

./start-dfs.sh

  1. 启动hbase,主节点hbase/bin目录下

./start-hbase.sh

  1. 配置了环境变量可以直接运行上述命令
  2. web访问hbase:vm01:16010
  3. 命令行访问:hbase shell

HBase基本命令

# 进入命令行
hbase shell

# 显示所有表
list

# 创建user表,包含info,address两个族
create 'user','info','address'

# 插入数据
put 'user','user001','info:name','zhangshan'
put 'user','user001','info:age','25'
put 'user','user001','info:gender','male'
put 'user','user001','address:city','guangzhou'
put 'user','user001','address:area','tianhe'

# 插入数据要指定rowKey,
# info列族有name,age,gender三列
# address列族有city,area两列

# 查询信息
# 查询info列族信息
get 'user','user001','info'
get 'user','user001','info:name'

#查询所有
scan 'user'

# 删除信息
delete 'user','user001','info:name'

# 清空表
truncate 'user'

# 停用表,删除表,需要先停用再删除
disable 'user'
drop 'user'

# 其他 修改表,过滤查询,版本信息

HBase2.0 Java开发基本操作

  1. pom依赖
 <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <hadoop.version>2.7.2</hadoop.version>
</properties>

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>${hadoop.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-shaded-client</artifactId>
    <version>2.0.0</version>
</dependency>
  1. 直接用Junit测试
public class CreateTable {

    private Configuration conf = null;

    private Connection conn = null;

    private String tableName = "user";
    
    private String family = "basic_info";

    @Before
    public void init() throws IOException {
        conf = HBaseConfiguration.create();
        //hbase通过zk来连接即可,不用hmaster
        conf.set("hbase.zookeeper.quorum","vm01:2181,vm02:2181,vm03:2181");
        conn = ConnectionFactory.createConnection(conf);
    }
    
     /**
     * 建表
     */
    @Test
    public void testCreat() throws Exception {
        //表管理器       
        Admin admin = conn.getAdmin();
        //table是否可用
        if(!admin.isTableAvailable(TableName.valueOf(tableName))){
            //table构建
            TableDescriptorBuilder tBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
            //列族声明
            tBuilder.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family)).build());
            //创建表
            admin.createTable(tBuilder.build());
        }
        admin.close();
        conn.close();
    }
    
    /**
     * 插入数据
     * @throws IOException
     */
    @Test
    public void testAdd() throws IOException {
        Table table = conn.getTable(TableName.valueOf(tableName));
        //指定rowKey
        Put put = new Put(Bytes.toBytes("user001"));
        //指定列族
        ColumnFamilyDescriptor columnFamily = table.getDescriptor().getColumnFamily(family.getBytes());
        //指定name列和值
        put.addColumn(columnFamily.getName(),"name".getBytes(),Bytes.toBytes("zhangsan"));
        //插入数据
        table.put(put);
        conn.close();
    }
    
    /**
     * Get查询
     */
    @Test
    public void testGet() throws IOException {
        Table table = conn.getTable(TableName.valueOf(tableName));
        Get get = new Get("user001".getBytes());
        get.addColumn(family.getBytes(), "name".getBytes());
        Result result = table.get(get);
        CellScanner cellScanner = result.cellScanner();
        while(cellScanner.advance()){
            Cell cur = cellScanner.current();
            //列族
            byte[] family = cur.getFamilyArray();
            //列
            byte[] qua = cur.getQualifierArray();
            //value
            byte[] val = cur.getValueArray();
            System.out.println(new String(family, cur.getFamilyOffset(), cur.getFamilyLength()));
            System.out.println(new String(qua, cur.getQualifierOffset(), cur.getQualifierLength()));
            System.out.println(new String(val, cur.getValueOffset(), cur.getValueLength()));
        }
    }
    
    /**
     * scan查询
     */
    @Test
    public void testScan() throws IOException {
        Table table = conn.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner scanner =  table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        //遍历所有信息
        while(iterator.hasNext()){
            Result result = iterator.next();
            CellScanner cellScanner = result.cellScanner();
            while(cellScanner.advance()){
                Cell cur = cellScanner.current();
                byte[] family = cur.getFamilyArray();
                byte[] qua = cur.getQualifierArray();
                byte[] val = cur.getValueArray();
                System.out.print(new String(family, cur.getFamilyOffset(), cur.getFamilyLength())+", ");
                System.out.print(new String(qua, cur.getQualifierOffset(), cur.getQualifierLength())+", ");
                System.out.println(new String(val, cur.getValueOffset(), cur.getValueLength()));
            }
        }
    }
}
    原文作者:书生Coder
    原文地址: https://www.jianshu.com/p/a6a48c9e8ad1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞