---
title: "hbase shell+javaAPI"
author: "weir2009"
date: "2014-06-20T16:25:55Z"
tags: []
categories: ["编程语言"]
slug: "hbase-shell-javaapi"
draft: false
lastmod: "2014-06-20T16:25:55Z"
id: 2083055
url: "https://weir2009.iteye.com/blog/2083055"
---

名称

命令表达式

查看所有表

list

创建表

create '表名称', '列名称1','列名称2','列名称N'

添加记录      

put '表名称', '行名称', '列名称:xx', '值'

查看记录

get '表名称', '行名称'

查看表中的记录总数

count  '表名称'

删除记录

delete  '表名' ,'行名称' , '列名称'

删除一张表

先要屏蔽该表，才能对该表进行删除，第一步 disable '表名称' 第二步  drop '表名称'

查看所有记录

scan "表名称"

查看某个表某个列中所有数据

scan "表名称" , \['列名称:']

更新记录 

就是重写一遍进行覆盖

查看表结构

describe '表名'

 

Hbase java API

 

**import** org.apache.hadoop.conf.Configuration;

**import** org.apache.hadoop.hbase.HBaseConfiguration;

 

Configuration conf = HBaseConfiguration.*create*();

conf.set("hbase.zookeeper.quorum", "weir:9000");

 

hbase.zookeeper.quorum 为默认，说明是有zookeeper来管理的

weir:9000 为hbase-site.xml 中的hdfs://weir:9000/hbase 指定的ip（主机名）端口号

如果是本地的话可以不设置：

conf.set("hbase.zookeeper.quorum", "weir:9000");

这句话，也就是说连接的是localhost

 

这样就连接上了hbase

 

创建表：

 

HBaseAdmin admin = **new** HBaseAdmin(conf);

      //删除表

      **if** (admin.tableExists(name)) {

        admin.disableTable(name);

        admin.deleteTable(name);

      }

//记录表的描述符(表名称)

      HTableDescriptor table = **new** HTableDescriptor(TableName.*valueOf*(name));

//列族描述符    

HColumnDescriptor hd = **new** HColumnDescriptor(col);

//限制最大版本

      hd.setMaxVersions(version);

      table.addFamily(hd);

      //创建表

      admin.createTable(table);

 

hbaseAdmin 提供了创建表、创建列族、检查表是否存在、修改表结构和列族结构、删除表等功能

 

/\**

    * 创建单列表

    * **@param** name

    * **@param** col

    * **@param** version

    * **@throws** Exception

    \*/

   **publicvoid** put(String name,String col,**int** version) **throws** Exception {

      HBaseAdmin admin = **new** HBaseAdmin(conf);

     

      **if** (admin.tableExists(name)) {

        admin.disableTable(name);

        admin.deleteTable(name);

      }

      HTableDescriptor table = **new** HTableDescriptor(TableName.*valueOf*(name));

      HColumnDescriptor hd = **new** HColumnDescriptor(col);

      hd.setMaxVersions(version);

      table.addFamily(hd);

     

      admin.createTable(table);

   }

 

put("tab\_global", "param", 1);

tab\_global 表示表名

param      表示列族

1           表示最大版本号

 

插入数据：

//表中的行名称

Put put = **new** Put(Bytes.*toBytes*("row\_userid"));

//添加列put.add(Bytes.*toBytes*("param"),Bytes.*toBytes*("userid"),Bytes.*toBytes*((**long**)0));

//得到表

      HTable ht = **new** HTable(conf, "tab\_global");

//添加put数据

      ht.put(put);