Hbase-API,基本表命令操作

package hbase;

import java.io.IOException;

import java.util.Iterator;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

public class HbaseAPI {
    /**
     * create 'namespace:tablename,'cf1','cf2'
     * 
     * @throws IOException
     * @throws ZooKeeperConnectionException
     * @throws MasterNotRunningException
     */
    @Test
    public void createTable() throws MasterNotRunningException,
            ZooKeeperConnectionException, IOException {
        String tbName = "ns1:person";
        String colFmaliy = "info";

        // 获取配置文件对象--》hbase-common-0.98.6-hadoop2.jar
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hive01:2181");

        // hbase的管理员对象-->对比mysql:root用户-->管理员对象
        HBaseAdmin admin = new HBaseAdmin(configuration);

        // 创建一个表格名称的对象
        TableName tableName = TableName.valueOf(tbName);

        // 相当于hbase shell 命令行中的desc
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

        hTableDescriptor.addFamily(new HColumnDescriptor(colFmaliy));
        //可以直接指定列族
        hTableDescriptor.addFamily(new HColumnDescriptor("job"));
        // 通过管理员对象admin创建表
        admin.createTable(hTableDescriptor);
        admin.close();
        System.out.println(tbName + "已经创建出来了");
    }

    /*
     * 插入数据 put 'namespace:tbname','rowkey','columnfamliy:column','vlaue'
     */
    @Test
    public void putData() throws IOException {
        String tableName = "ns1:user";
        String colFamliy = "info";
        String rowKey = "1001a";
        String column = "name";
        String value = "zhansan";

        // client ->zookeeper(meta-redionserver)-->hbase-meta
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hive01:2181");

        // 具体的表格对象
        HTable table = new HTable(configuration, tableName);

        // 获取当前该表的所有列族
        HColumnDescriptor[] columnFamlies = table.getTableDescriptor()
                .getColumnFamilies();

        // 构建put对象
        Put put = new Put(Bytes.toBytes(rowKey));
        // Put put2=new Put(Bytes.toBytes("1001b"));

        // List<Put> puts = new ArrayList<Put>();
        for (int i = 0; i < columnFamlies.length; i++) {
            // 以字符串的形式获取该表当前所有的列族
            String familyName = columnFamlies[i].getNameAsString();

            // 判断要插入数据的列族在该表中是否存在 ,存在才可以插入
            if (colFamliy.equals(familyName)) {
                put.add(Bytes.toBytes(colFamliy), Bytes.toBytes(column),
                        Bytes.toBytes(value));
                put.add(Bytes.toBytes(familyName), Bytes.toBytes("age"),
                        Bytes.toBytes("17"));
                put.add(Bytes.toBytes(colFamliy), Bytes.toBytes("birthday"),
                        Bytes.toBytes("2017-03-12"));
                put.add(Bytes.toBytes(colFamliy), Bytes.toBytes("address"),
                        Bytes.toBytes("beijing"));
                // put2.add(Bytes.toBytes(colFamliy), Bytes.toBytes(column),
                // Bytes.toBytes(value));
                // put2.add(Bytes.toBytes(colFamliy), Bytes.toBytes("address"),
                // Bytes.toBytes("beijing"));
            }
        }
        table.put(put);
        table.setAutoFlushTo(false);
        // puts.add(put);
        // puts.add(put2);
        // table.put(puts);
        // table.put(List<Put>);推荐使用
        System.out.println("插入成功");

    }

    /**
     * 查询数据 :get get 'namespace:tablesname','roekey'
     * 
     * @throws IOException
     */

    @Test
    public void getRow() throws IOException {
        String tbname = "ns1:person";
        String rowKey = "1001a";

        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hive01:2181");

        // 具体的表格对象
        HTable table = new HTable(configuration, tbname);

        Get get = new Get(Bytes.toBytes(rowKey));

        // 获取对应表格每一行的具体的结果集
        Result result = table.get(get);

        List<Cell> listCells = result.listCells();

        for (Cell cell : listCells) {

            System.out.println("列族:  "
                    + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列:"
                    + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("时间戳:" + cell.getTimestamp());
            System.out
                    .println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out
                    .println("分割线:-------------------------------------------");
        }
    }

    /**
     * 4.全表扫描 获取全表的数据scan 'namespace:tablename'
     * 
     * @throws IOException
     */
    @Test
    public void scanTable() throws IOException {
        // 指定表名和表所在的命名空间
        String tbName = "ns1:person";

        // 获取zookeeper支持
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hive01:2181");

        // 具体的表格对象
        HTable table = new HTable(configuration, tbName);

        // 获取结果集
        ResultScanner resultScanner = table.getScanner(new Scan());

        Iterator<Result> results = resultScanner.iterator();

        while (results.hasNext()) {
            Result result = results.next();

            List<Cell> listCells = result.listCells();

            for (Cell cell : listCells) {
                // 行健
                System.out.println("行健:" + Bytes.toString(result.getRow()));
                System.out.println("列族"
                        + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("列:"
                        + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("时间戳:" + cell.getTimestamp());
                System.out.println("值:"
                        + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("-----------------分割线---------------------");
            }
        }
        resultScanner.close();
        table.close();

    }

    /**
     * 5.删除单元格 delete 'namespace:tablename','rowkey','columnFamliy:column:'
     * 
     * @throws IOException
     */

    @Test
    public void delCell() throws IOException {
        String tbName = "ns1:person";
        String colFamliy = "info";
        String rowKey = "1001a";
        String column = "name";

        // 获取zookeeper支持
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hive01:2181");

        // 具体的表格对象
        HTable table = new HTable(configuration, tbName);

        //Delete对象
        Delete delete =new Delete(Bytes.toBytes(rowKey));
        delete.deleteColumn(Bytes.toBytes(colFamliy), Bytes.toBytes(column));

        table.delete(delete);
        table.close();
        System.out.println("rowKey是:"+rowKey+"的列族"+colFamliy+"下的"+column+"被成功的删除了");

    }

    /**
     * 6.删除整行
     * @throws IOException 
     */

    public void delAll() throws IOException{
        String tbName = "ns1:person";
        String colFamliy = "info";
        String rowKey = "1001a";
        String column = "name";

        // 获取zookeeper支持
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hive01:2181");

        // 具体的表格对象
        HTable table = new HTable(configuration, tbName);

        //Delete对象
        Delete delete =new Delete(Bytes.toBytes(rowKey));

        table.delete(delete);
        table.close();
        System.out.println("rowKey是:"+rowKey+"的列族"+colFamliy+"下的"+column+"被成功的删除了");

    }

    /**
     * 删除表格,drop disable enamble
     * drop   'namespace:tablename'
     * disable 'namespace:tablename'
     * enable  'namespace:tablename'
     * @throws IOException 
     * @throws ZooKeeperConnectionException 
     * @throws MasterNotRunningException 
     * @throws InterruptedException 
     */

    @Test
    public void dropTable() throws MasterNotRunningException, ZooKeeperConnectionException, IOException, InterruptedException{
        String tbName = "ns1:person";

        // 获取配置文件对象--》hbase-common-0.98.6-hadoop2.jar
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hive01:2181");

        // hbase的管理员对象-->对比mysql:root用户-->管理员对象
        HBaseAdmin admin = new HBaseAdmin(configuration);
        //admin.disableTable(tbName);
        admin.enableTable(tbName);
//      admin.deleteTable(tbName);
//      admin.flush(tbName);
//      admin.compact(tbName);
//      admin.split(tbName);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值