• hbase 增删改查 api 简单操作


    package com.utils;


    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;


    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HColumnDescriptor;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.KeyValue;
    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.HTableInterface;
    import org.apache.hadoop.hbase.client.HTablePool;
    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.filter.CompareFilter.CompareOp;
    import org.apache.hadoop.hbase.filter.PrefixFilter;
    import org.apache.hadoop.hbase.filter.RegexStringComparator;
    import org.apache.hadoop.hbase.filter.RowFilter;


    public class HBaseTest {
    HBaseAdmin admin=null;
    Configuration conf=null;
    /**
    * 构造函数载入配置
    */
    public HBaseTest(){
    conf = new Configuration();
    conf.set("hbase.zookeeper.quorum","192.168.80.20,192.168.80.21,192.168.80.22");
    conf.set("hbase.rootdir", "hdfs://cluster/hbase");
    conf.set("hbase.master", "192.168.80.20:60010");
    try {
    admin = new HBaseAdmin(conf);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) throws Exception {
    HBaseTest hbase = new HBaseTest();
    //创建一张表
    hbase.createTable("stu","cf");
    // //查询全部表名
    // hbase.getALLTable();
    // //往表中加入一条记录
    // hbase.addOneRecord("stu","key1","cf","name","zhangsan");
    // hbase.addOneRecord("stu","key1","cf","age","24");
    // //查询一条记录
    // hbase.getKey("stu","key1");
    // //获取表的全部数据
    // hbase.getALLData("stu");
    // //删除一条记录
    // hbase.deleteOneRecord("stu","key1");
    // //删除表
    // hbase.deleteTable("stu");
    //scan过滤器的使用
    hbase.getScanData("stu","cf","age");
    //rowFilter的使用
    //84138413_20130313145955
    hbase.getRowFilter("waln_log","^*_201303131400\d*$");
    }


    public void getRowFilter(String tableName, String reg) throws Exception {
    HTable hTable = new HTable(conf, tableName);
    Scan scan = new Scan();
    RowFilter rowFilter = new RowFilter(CompareOp.NOT_EQUAL, new RegexStringComparator(reg));
    scan.setFilter(rowFilter);
    ResultScanner scanner = hTable.getScanner(scan);
    for (Result result : scanner) {
    System.out.println(new String(result.getRow()));
    }
    }
    public void getScanData(String tableName, String family, String qualifier) throws Exception {
    HTable hTable = new HTable(conf, tableName);
    Scan scan = new Scan();
    scan.addColumn(family.getBytes(), qualifier.getBytes());
    ResultScanner scanner = hTable.getScanner(scan);
    for (Result result : scanner) {
    if(result.raw().length==0){
    System.out.println(tableName+" 表数据为空!

    ");
    }else{
    for (KeyValue kv: result.raw()){
    System.out.println(new String(kv.getKey())+" "+new String(kv.getValue()));
    }
    }
    }
    }
    private void deleteTable(String tableName) {
    try {
    if (admin.tableExists(tableName)) {
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
    System.out.println(tableName+"表删除成功。");
    }
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println(tableName+"表删除失败!

    ");
    }

    }
    /**
    * 删除一条记录
    * @param tableName
    * @param rowKey
    */
    public void deleteOneRecord(String tableName, String rowKey) {
    HTablePool hTablePool = new HTablePool(conf, 1000);
    HTableInterface table = hTablePool.getTable(tableName);
    Delete delete = new Delete(rowKey.getBytes());
    try {
    table.delete(delete);
    System.out.println(rowKey+"记录删除成功!");
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println(rowKey+"记录删除失败!");
    }
    }
    /**
    * 获取表的全部数据
    * @param tableName
    */
    public void getALLData(String tableName) {
    try {
    HTable hTable = new HTable(conf, tableName);
    Scan scan = new Scan();
    ResultScanner scanner = hTable.getScanner(scan);
    for (Result result : scanner) {
    if(result.raw().length==0){
    System.out.println(tableName+" 表数据为空!");
    }else{
    for (KeyValue kv: result.raw()){
    System.out.println(new String(kv.getKey())+" "+new String(kv.getValue()));
    }
    }
    }
    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    /**
    * 查询一条记录
    * @param tableName
    * @param rowKey
    */
    public void getKey(String tableName, String rowKey) {
    HTablePool hTablePool = new HTablePool(conf, 1000);
    HTableInterface table = hTablePool.getTable(tableName);
    Get get = new Get(rowKey.getBytes());
    try {
    Result result = table.get(get);
    if (result.raw().length==0) {
    System.out.println("查询的关键词"+rowKey+"不存在");
    }else {
    for (KeyValue kv : result.raw()) {
    System.out.println(new String(kv.getKey())+" "+new String(kv.getValue()));
    }
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * 往表中加入一条记录
    * @param tableName
    * @param rowKey
    * @param column
    * @param qua
    * @param value
    */
    public void addOneRecord(String tableName, String rowKey, String column,
    String qua, String value) {
    HTablePool hTablePool = new HTablePool(conf, 1000);
    HTableInterface table = hTablePool.getTable(tableName);
    Put put = new Put(rowKey.getBytes());
    put.add(column.getBytes(), qua.getBytes(), value.getBytes());
    try {
    table.put(put);
    System.out.println("加入记录 "+rowKey+ " 成功!

    ");
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println("加入记录 "+rowKey+ " 失败。");
    }
    }
    /**
    * 查询全部表名
    * @return
    * @throws Exception
    */
    public List<String> getALLTable() throws Exception {
    ArrayList<String> tables = new ArrayList<String>();
    if(admin!=null){
    HTableDescriptor[] listTables = admin.listTables();
    if (listTables.length>0) {
    for (HTableDescriptor tableDesc : listTables) {
    tables.add(tableDesc.getNameAsString());
    System.out.println(tableDesc.getNameAsString());
    }
    }
    }
    return tables;
    }
    /**
    * 创建一张表
    * @param tableName
    * @param column
    * @throws Exception
    */
    public void createTable(String tableName, String column) throws Exception {
    if(admin.tableExists(tableName)){
    System.out.println(tableName+"表已经存在。");
    }else{
    HTableDescriptor tableDesc = new HTableDescriptor(tableName);
    tableDesc.addFamily(new HColumnDescriptor(column.getBytes()));
    admin.createTable(tableDesc);
    System.out.println(tableName+"表创建成功!");
    }
    }
    }

  • 相关阅读:
    Linux打包&压缩 tar,gzip,bzip2
    Linux递归计算目录md5
    fetion飞信登录异常,错误码10033201、10033202
    Linuxscp如何实现nohup &后台启动
    Linux显示日文4字节半角字符
    Linux目录配置的依据FHS
    Linux解决中文乱码问题: vim/pdf/gedit
    ery validator addMethod 方法的使用
    查询今天发帖量 sql
    JAVA反射机制
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7219660.html
Copyright © 2020-2023  润新知