一、get 、put、delete、scan

1、代码

package com.beifeng.senior.hadoop.hbase;

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.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.apache.hadoop.io.IOUtils; /**
* CRUD operations
*
* @author root
*
*/ public class HBaseOperation { public static HTable getHTableByTableName(String tableName) throws Exception {
// get instance of default configuration
Configuration configuration = HBaseConfiguration.create(); // get table instance
HTable table = new HTable(configuration, tableName); return table;
} public void getData() throws Exception {
String tableName = "user"; // default.user HTable table = getHTableByTableName(tableName); //create get with rowkey
Get get = new Get(Bytes.toBytes("10002")); //add column
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age")); //get data
Result result = table.get(get); //key: rowkey + cf + c + version
//value: value
for(Cell cell : result.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "->" //
+ Bytes.toString(CellUtil.cloneValue(cell)));
} //table close
table.close();
} /**
* 建议:
* tablename & column family ->常量, HbaseTableContent
*
* Map<String, Object>
*
* @throws Exception
*/ public void putData() throws Exception {
String tableName = "user"; // default.user HTable table = getHTableByTableName(tableName); Put put = new Put(Bytes.toBytes("10004")); //add a column with value
put.add(Bytes.toBytes("info"),
Bytes.toBytes("name"),
Bytes.toBytes("zhaoliu")); put.add(Bytes.toBytes("info"),
Bytes.toBytes("age"),
Bytes.toBytes("25")); put.add(Bytes.toBytes("info"),
Bytes.toBytes("address"),
Bytes.toBytes("shanghai")); table.put(put); table.close();
} public void deleteData() throws Exception {
String tableName = "user"; // default.user HTable table = getHTableByTableName(tableName); Delete delete = new Delete(Bytes.toBytes("10004")); /*删除单个数据
delete.deleteColumn(Bytes.toBytes("info"),
Bytes.toBytes("address"));
*/ /*删除整个列簇*/
delete.deleteFamily(Bytes.toBytes("info")); table.delete(delete); table.close();
} public static void main(String[] args) throws Exception {
String tableName = "user"; // default.user HTable table = null;
ResultScanner resultScanner = null; try {
table = getHTableByTableName(tableName); /*全表扫描
Scan scan = new Scan();
*/ //指定Rowkey范围
Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes("10002"));
scan.setStopRow(Bytes.toBytes("10004")); resultScanner = table.getScanner(scan); for(Result result : resultScanner) {
System.out.println(Bytes.toString(result.getRow()));
//System.out.println(result); for(Cell cell : result.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "->" //
+ Bytes.toString(CellUtil.cloneValue(cell)));
} System.out.println("--------------------");
} } catch (Exception e) {
e.printStackTrace();
}finally{
IOUtils.closeStream(resultScanner);
IOUtils.closeStream(table);
} } }

HBase命令:

hbase(main):006:0> flush 'table name'        //刷数据

hbase(main):007:0> compact 'table name'    //合并数据

最新文章

  1. 设置height:100%无效的解决方法
  2. CentOS7下安装MySQL5.7安装与配置(转)
  3. [转载]DBA的特质第二部分:性格
  4. 嵌入式Linux驱动学习之路(一)嵌入式系统的软硬件架构
  5. javascript中数组揭秘
  6. mysql存储过程procedure
  7. 【BZOJ】2321: [BeiJing2011集训]星器(数学+特殊的技巧)
  8. 调试 rewrite
  9. 《Linux/Unix系统编程手册》读书笔记4
  10. SDE+ORACLE优化配置
  11. 关于mapreduce过程中出现的错误:Too many fetch-failures
  12. [转]用Python做一个自动生成读表代码的小脚本
  13. c++ primer复习(五):类
  14. CENTOS6.5 teamviewer安装
  15. (使用步骤)ThinkPHP3.1.2中如何配置Ckeditor_4.1.1和Ckfindtor(转)
  16. java模拟数据库缓存
  17. php ajax 下拉加载数据
  18. Java 伙伴系统(模拟)
  19. rsync实现数据增量备份
  20. CV迅速发展

热门文章

  1. jQuery AjaxUpload中文使用API和demo示例
  2. OpenCV 的四大模块
  3. 1492: [NOI2007]货币兑换Cash【CDQ分治】
  4. hdu5698 百度之星2016round2b第3题
  5. Oracle 数据库中序列结合触发器实现主键自增长
  6. asp.net mvc4 之Webapi之客户端或服务器端安全控制
  7. mybatis入门小结(六)
  8. LVS集群的负载调度
  9. uni-app 创建的第一个应用
  10. HDU4850 Wow! Such String! —— 字符串构造