package testHBase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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; public class TestHBase {
static Configuration cfg = HBaseConfiguration.create(); //得到配置对象
public static void create(String tableName ,String columnFamily) throws Exception{
HBaseAdmin admin = new HBaseAdmin(cfg); //得到HBaseAdmin对象,用于操作HBase
if(admin.tableExists(tableName)){
System.out.println("Table" + tableName + " Exists!");
System.exit(0);
}
else{
HTableDescriptor tableDesc = new HTableDescriptor(tableName); //得到表描述对象 创建表时用到
tableDesc.addFamily(new HColumnDescriptor(columnFamily)); //HColumnDescriptor 列族描述对象
admin.createTable(tableDesc); //创建表
System.out.println("Table " + tableName + " Success!");
}
} static void put(String tableName,String row,String columnFamily,String column,String data) throws Exception{
HTable table = new HTable(cfg,tableName); //得到表对象
Put put = new Put(Bytes.toBytes(row)); //插入数据
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),Bytes.toBytes(data));
table.put(put);
System.out.println("put " + row +"," + "columnFamily:" + column + "," + data); } static void get(String tableName,String row) throws Exception{
HTable table = new HTable(cfg,tableName);
Get get = new Get(Bytes.toBytes(row));//获取行数据
Result result = table.get(get);
System.out.println("Get: " + result);
} static void scan(String tableName) throws Exception{
HTable table = new HTable(cfg,tableName);
Scan scan = new Scan(Bytes.toBytes(tableName)); ///扫描全表
ResultScanner rs = table.getScanner(scan);
for(Result r : rs){
System.out.println("Scan: " + r);
}
} static boolean delete(String tableName) throws Exception{
HBaseAdmin admin = new HBaseAdmin(cfg);
if(admin.tableExists(tableName)){
admin.disableTable(tableName); //先disable
System.out.println("Disable Table " + tableName + " Success!");
admin.deleteTable(tableName); //再删除
System.out.println("Delete Table " + tableName + " Success!");
}
return true; } public static void main(String[] args) throws Exception {
cfg.set("hbase.master", "hadoop:60000");
cfg.set("hbase.rootdir", "hdfs://hadoop:9000/hbase");
cfg.set("hbase.zookeeper.quorum", "hadoop"); String tableName = "hbase_test";
String columnFamily = "cf";
create(tableName, columnFamily);
put(tableName,"row1",columnFamily,"c1","data");
scan(tableName);
get(tableName,"row1");
delete(tableName); }
}

  

最新文章

  1. 半吊子学习Swift--天气预报程序-获取天气信息
  2. eclipse crash
  3. token生成过程
  4. 怎么提高Jquery性能
  5. 实现在Android开发中的Splash Screen开场屏的效果
  6. ios9网络请求https适配
  7. ios UICollectionView滑动时操作
  8. IPC:Sockets
  9. 利用set实现去重
  10. Jar
  11. JavaSE思维导图(六)
  12. 关于cocos2dx的C++调用创建项目
  13. Python装饰器学习(九步入门)
  14. Java注解处理器--编译时处理的注解
  15. django--orm对象关系映射之常用的增删改查
  16. windows 上用 docker 部署aspnetcore 2.0
  17. 前端 ----jQuery操作表单
  18. -Dmaven.multiModuleProjectDirectory system property is not set.
  19. Android Studio打包过程和应用安装过程
  20. Stylus的使用

热门文章

  1. linux 远程连接工具——MTPuTTY
  2. 《锋利的jQuery(第2版)》笔记-第2章-jQuery选择器
  3. 浏览器内部工作原理--作者:Tali Garsiel
  4. 给Excel2013添加WebADI的Oracle加载项
  5. Linux或Unix环境利用符号链接升级Maven
  6. 搭建Apache Web服务器
  7. ubuntu14.04下搭建python+mysql环境
  8. 仅用aspx文件实现Ajax调用后台cs程序。(实例)
  9. SEO之title优化
  10. dom 无法找到 body节点问题