Hbase增删改查、关联查询、关系型数据库转化

写在前面

主要的目的是想直接通过Hbase 将上面上面的Mysql 替换成Hbase, 这样能够对我们维护的负担压力减轻更多(调研), 实际过程中遇到了非常多的问题.

  1. 处理多表之间的问题的时候出现了很多问题. 主要是在列式数据库中 对每一行的定义都是非结构化的, 只是对column family 进行严格的规定而已.这样一来我们就没有了我们需要的外键/相关联的, 更多的操作都是通过全表扫描.
  2. 对rowKey 的设计需要有非常高要求. 学习OpenTSDB 中rowKey的巧妙设计, 虽然丢弃了版本号的使用,但是能够更加快的找到timeseries data,这也是一个非常好的一种设计之一.

Hbase 实践

CRUD 操作

1.增加一条记录

public void testPut() throws IOException {
    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("people"));
    HTable table = (HTable) conn.getTable(TableName.valueOf("people"));
    //设置rowkey 为 rk0001
    Put put = new Put(Bytes.toBytes("rk0001"));
    //在info 的column family----> name:zhangsan
    put.addColumn(Bytes.toBytes("info"), Byte
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以帮您回答这个问题。下面是使用 Java 操作 HBase 数据库的增删改查示例代码: 1. 导入必要的包 ``` 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.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; ``` 2. 创建 HBase 配置对象 ``` Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "localhost"); // HBase 的 ZooKeeper 地址 configuration.set("hbase.zookeeper.property.clientPort", "2181"); // HBase 的 ZooKeeper 端口号 ``` 3. 创建 HBase 连接对象和表对象 ``` Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("test_table")); // test_table 是表名 ``` 4. 插入数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("value1")); // cf 是列族名,col1 是列名,value1 是值 table.put(put); ``` 5. 查询数据 ``` Get get = new Get(Bytes.toBytes("row1")); // row1 是行键名 Result result = table.get(get); for (Cell cell : result.listCells()) { String cf = Bytes.toString(CellUtil.cloneFamily(cell)); // 获取列族名 String col = Bytes.toString(CellUtil.cloneQualifier(cell)); // 获取列名 String value = Bytes.toString(CellUtil.cloneValue(cell)); // 获取值 System.out.println(cf + ":" + col + "=" + value); } ``` 6. 修改数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("new_value")); // cf 是列族名,col1 是列名,new_value 是新的值 table.put(put); ``` 7. 删除数据 ``` Delete delete = new Delete(Bytes.toBytes("row1")); // row1 是行键名 delete.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1")); // cf 是列族名,col1 是列名 table.delete(delete); ``` 以上就是使用 Java 操作 HBase 数据库的增删改查示例代码,希望对您有所帮助。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值