[原创]HBase学习笔记(3)- Java程序访问HBase

这里介绍使用java api来访问和操作HBase,例如create、delete、select、update等操作。

1.HBase配置

配置HBase使用的zookeeper集群地址和端口。

private static Configuration configuration;

static {
    configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.property.clientPort", "2181");
    configuration.set("hbase.zookeeper.quorum", "ZK1,ZK2,ZK3");
}

2.创建表

// 创建表
public static boolean create(String tableName, String columnFamily) {
    HBaseAdmin admin = null;
    try {
        admin = new HBaseAdmin(configuration);
        if (admin.tableExists(tableName)) {
            System.out.println(tableName + " exists!");
            return false;
        } else {
            // 逗号分隔,可以有多个columnFamily
            String[] cfArr = columnFamily.split(",");
            HColumnDescriptor[] hcDes = new HColumnDescriptor[cfArr.length];
            for (int i = 0; i < cfArr.length; i++) {
                hcDes[i] = new HColumnDescriptor(cfArr[i]);
            }
            HTableDescriptor tblDes = new HTableDescriptor(TableName.valueOf(tableName));
            for (HColumnDescriptor hc : hcDes) {
                tblDes.addFamily(hc);
            }
            admin.createTable(tblDes);
            System.out.println(tableName + " create successfully!");
            return true;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

3.插入数据

指定表名、rowkey、cf、qualifier和value,插入数据到HBase。

public static boolean put(String tableName, String rowkey, String columnFamily, String qualifier, String value) {
    try {
        HTable table = new HTable(configuration, tableName);
        Put put = new Put(rowkey.getBytes());
        put.add(columnFamily.getBytes(), qualifier.getBytes(), value.getBytes());
        table.put(put);
        System.out.println("put successfully! " + rowkey + "," + columnFamily + "," + qualifier + "," + value);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

 

4.查询数据

4.1.查询指定rowkey的整条记录,返回Result对象。
// 查询
public static Result getResult(String tableName, String rowkey) {
    System.out.println("get result. table=" + tableName + " rowkey=" + rowkey);
    try {
        HTable table = new HTable(configuration, tableName);
        Get get = new Get(rowkey.getBytes());
        return table.get(get);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

 

4.2.展现Result内容
// Result转换成Map形式,便于输出
private static Map<String, Object> result2Map(Result result) {
    Map<String, Object> ret = new HashMap<String, Object>();
    if (result != null && result.listCells() != null) {
        for (Cell cell : result.listCells()) {
            String key = Bytes.toString(CellUtil.cloneQualifier(cell));
            String value = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println(key + " => " + value);
            ret.put(key, value);
        }
    }
    return ret;
}

 

4.3.指定qualifier查询数据
// 查询
public static byte[] get(String tableName, String rowkey, String qualifier) {
    System.out.println("get result. table=" + tableName + " rowkey=" + rowkey + " qualifier=" + qualifier);
    Result result = getResult(tableName, rowkey);
    if (result != null && result.listCells() != null) {
        for (Cell cell : result.listCells()) {
            String key = Bytes.toString(CellUtil.cloneQualifier(cell));
            if (key.equals(qualifier)) {
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(key + " => " + value);
                return CellUtil.cloneValue(cell);
            }
        }
    }
    return null;
}

 

5.查看全表数据

如下只要指定表名,就可以通过Scan来查看全表数据。

// 查看全表
public static List<Map<String, Object>> scan(String tableName) {
    System.out.println("scan table " + tableName);
    try {
        HTable table = new HTable(configuration, tableName);
        Scan scan = new Scan();
        ResultScanner rs = table.getScanner(scan);
        List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
        for (Result r : rs) {
            Map<String, Object> m = result2Map(r);
            StringBuilder sb = new StringBuilder();
            for(String k : m.keySet()) {
                sb.append(k).append("=>").append(m.get(k)).append(" ");
            }
            System.out.println(sb.toString());
            resList.add(m);
        }
        return resList;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

 

6.列出HBase中所有表名

// 列出所有表
public static List<String> list() {
    System.out.println("list tables.");
    try {
        HBaseAdmin admin = new HBaseAdmin(configuration);
        TableName[] tableNames = admin.listTableNames();
        List<String> tblArr = new ArrayList<String>();
        for (int i = 0; i < tableNames.length; i++) {
            tblArr.add(tableNames[i].getNameAsString());
            System.out.println("Table: " + tableNames[i].getNameAsString());
        }
        return tblArr;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

 

7.删除指定qualifier内容

// 指定qualifier删除内容
public static boolean deleteQualifier(String tableName, String rowkey, String columnFamily, String qualifier) {
    System.out.println("delete qualifier. table=" + tableName
            + " rowkey=" + rowkey + " cf=" + columnFamily + " qualifier=" + qualifier);
    try {
        HBaseAdmin admin = new HBaseAdmin(configuration);
        if (admin.tableExists(tableName)) {
            HTable table = new HTable(configuration, tableName);
            Delete delete = new Delete(rowkey.getBytes());
            delete.deleteColumn(columnFamily.getBytes(), qualifier.getBytes());
            table.delete(delete);
        }
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

 

8.删除指定rowkey的记录

// 指定rowkey删除记录
public static boolean deleteRow(String tableName, String rowkey) {
    System.out.println("delete row. table=" + tableName + " rowkey=" + rowkey);
    try {
        HBaseAdmin admin = new HBaseAdmin(configuration);
        if (admin.tableExists(tableName)) {
            HTable table = new HTable(configuration, tableName);
            Delete delete = new Delete(rowkey.getBytes());
            table.delete(delete);
        }
        System.out.println(tableName + ", " + rowkey + " delete successfully!");
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

 

9.删除指定column family

// 删除columnfamily
public static boolean deleteColumnFamily(String tableName, String columnFamily) {
    System.out.println("delete column family. table=" + tableName + " cf=" + columnFamily);
    try {
        HBaseAdmin admin = new HBaseAdmin(configuration);
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteColumn(tableName, columnFamily);
            admin.enableTable(tableName);
        }
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

 

 

 

10.删除表

删除指定表名。

// 删除表
public static boolean delete(String tableName) {
    System.out.println("delete table " + tableName);
    try {
        HBaseAdmin admin = new HBaseAdmin(configuration);
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

 

11.一个测试案例

public class HBaseTest {
public static void main(String[] args) {
        HBaseUtils.create("test1", "cf1,cf2,cf3");
        HBaseUtils.put("test1", "row1", "cf1", "field1", "value1");
        HBaseUtils.put("test1", "row1", "cf1", "field2", "value2");
        HBaseUtils.put("test1", "row1", "cf2", "field3", "value3");
        HBaseUtils.put("test1", "row2", "cf1", "field4", "value4");
        HBaseUtils.list();
        HBaseUtils.get("test1", "row1");
        HBaseUtils.get("test1", "row2", "cf1");
        HBaseUtils.deleteRow("test1", "row2");
        HBaseUtils.scan("test1");
//        HBaseUtils.delete("test1");
        HBaseUtils.list();
    }
}

 

 

 

 

转载于:https://www.cnblogs.com/simplestupid/p/6610325.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值