import java.io.IOException;
import java.util.ArrayList;
import java.util.List; 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.KeyValue;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.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 OperateTable {
private static Configuration configuration=null;
static{
configuration=HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "master");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
} public static void createTable(String tableName,String[] columnFamilys) throws IOException {
HBaseAdmin admin=new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
System.out.println("表已存在");
System.exit(0);
}
else {
HTableDescriptor descriptor=new HTableDescriptor(tableName);
for (String columnFamily:columnFamilys) {
descriptor.addFamily(new HColumnDescriptor(columnFamily));
}
admin.createTable(descriptor);
System.out.println("创建表成功");
}
} //删除数据库表
public static void deleteTable(String tableName) throws Exception, ZooKeeperConnectionException {
HBaseAdmin admin=new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("删除表成功");
}
else {
System.out.println("删除的表不存在");
System.exit(0);
}
} //添加一条数据
public static void addRow(String tableName,String row,String columnFamily,String column,String value) throws IOException {
HTable table=new HTable(configuration,tableName);
Put put=new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
table.put(put);
} //删除一条数据
public static void delRow(String tableName,String row) throws Exception {
HTable table=new HTable(configuration,tableName);
Delete delete=new Delete(Bytes.toBytes(row));
table.delete(delete);
} //删除多条数据
public static void delMulRows(String tableName,String[] rows) throws Exception{
HTable table=new HTable(configuration,tableName);
List<Delete> list=new ArrayList<Delete>();
for (String row:rows) {
Delete delete=new Delete(Bytes.toBytes(row));
list.add(delete);
}
table.delete(list);
} //获取一行数据
public static void getRow(String tableName,String row) throws Exception {
HTable table=new HTable(configuration,tableName);
Get get=new Get(Bytes.toBytes(row));
Result result=table.get(get);
for(KeyValue keyValue:result.raw()){
System.out.println("Row Name:"+new String(keyValue.getRow())+" ");
System.out.println("Timestamp:"+keyValue.getTimestamp()+" ");
System.out.println("Column Family:"+new String(keyValue.getFamily())+" ");
System.out.println("Row Name:"+new String(keyValue.getQualifier())+" ");
System.out.println("Value:"+new String(keyValue.getValue())+" ");
}
} //获取所有数据
public static void getAllRows(String tableName) throws Exception {
HTable table=new HTable(configuration,tableName);
Scan scan=new Scan();
ResultScanner resultScanner=table.getScanner(scan);
for(Result result:resultScanner){
for(KeyValue keyValue:result.raw()){
System.out.print("Row Name:"+new String(keyValue.getRow())+" ");
System.out.println("Timestamp:"+keyValue.getTimestamp()+" ");
System.out.println("Column Family:"+new String(keyValue.getFamily())+" ");
System.out.println("Row Name:"+new String(keyValue.getQualifier())+" ");
System.out.println("Value:"+new String(keyValue.getValue())+" ");
}
}
} public static void main(String[] args) {
try {
String tableName="users"; // 第一步:创建数据库表:“users”
String[] columnFamilyStrings={"info","course"};
OperateTable.createTable(tableName, columnFamilyStrings); // 第二步:向数据表的添加数据
// 添加第一行数据
OperateTable.addRow(tableName, "tht", "info", "age", "20");
OperateTable.addRow(tableName, "tht", "info", "sex", "boy");
OperateTable.addRow(tableName, "tht", "course", "china", "97");
OperateTable.addRow(tableName, "tht", "course", "math", "128");
OperateTable.addRow(tableName, "tht", "course", "english", "85");
// 添加第二行数据
OperateTable.addRow(tableName, "xiaoxue", "info", "age", "19");
OperateTable.addRow(tableName, "xiaoxue", "info", "sex", "boy");
OperateTable.addRow(tableName, "xiaoxue", "course", "china", "90");
OperateTable.addRow(tableName, "xiaoxue", "course", "math", "120");
OperateTable.addRow(tableName, "xiaoxue", "course", "english", "90");
// 添加第三行数据
OperateTable.addRow(tableName, "qingqing", "info", "age", "18");
OperateTable.addRow(tableName, "qingqing", "info", "sex", "girl");
OperateTable.addRow(tableName, "qingqing", "course", "china", "100");
OperateTable.addRow(tableName, "qingqing", "course", "math", "100");
OperateTable.addRow(tableName, "qingqing", "course", "english", "99"); System.out.println("获取一条数据:");
OperateTable.getRow(tableName, "tht"); System.out.println("获取所有数据");
OperateTable.getAllRows(tableName); System.out.println("删除一条数据");
OperateTable.delRow(tableName, "tht");
OperateTable.getAllRows(tableName); System.out.println("删除多条数据");
String[] rowsStrings={ "xiaoxue", "qingqing" };
OperateTable.delMulRows(tableName, rowsStrings);
OperateTable.getAllRows(tableName); System.out.println("删除数据库");
OperateTable.deleteTable(tableName);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Hbase实例的更多相关文章

  1. java操作Hbase实例

    所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...

  2. 使用C#和Thrift来访问Hbase实例

    今天试着用C#和Thrift来访问Hbase,主要参考了博客园上的这篇文章.查了Thrift,Hbase的资料,结合博客园的这篇文章,终于搞好了.期间经历了不少弯路,下面我尽量详细的记录下来,免得大家 ...

  3. sqoop1.4.6从mysql导入hdfs\hive\hbase实例

    //验证sqoop是否连接到mysql数据库sqoop list-tables --connect 'jdbc:mysql://n1/guizhou_test?useUnicode=true& ...

  4. 从零自学Hadoop(20):HBase数据模型相关操作上

    阅读目录 序 介绍 命名空间 表 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...

  5. Python操作HBase之happybase

    安装Thrift 安装Thrift的具体操作,请点击链接 pip install thrift 安装happybase pip install happybase 连接(happybase.Conne ...

  6. 云HBase发布全文索引服务,轻松应对复杂查询

    云HBase发布了“全文索引服务”功能,自2019年01月25日后创建的云HBase实例,可以在控制台免费开启此“全文索引服务”功能.使用此功能可以让用户在HBase之上构建功能更丰富的搜索业务,不再 ...

  7. 阿里云HBase携X-Pack再进化,重新赋能轻量级大数据平台

    一.八年双十一,造就国内最大最专业HBase技术团队 阿里巴巴集团早在2010开始研究并把HBase投入生产环境使用,从最初的淘宝历史交易记录,到蚂蚁安全风控数据存储.持续8年的投入,历经8年双十一锻 ...

  8. 阿里云HBase全新发布X-Pack 赋能轻量级大数据平台

    一.八年双十一,造就国内最大最专业HBase技术团队 阿里巴巴集团早在2010开始研究并把HBase投入生产环境使用,从最初的淘宝历史交易记录,到蚂蚁安全风控数据存储.持续8年的投入,历经8年双十一锻 ...

  9. Hbase学习02

    第2章 Apache HBase配置 本章在“入门”一章中进行了扩展,以进一步解释Apache HBase的配置. 请仔细阅读本章,特别是基本先决条件,确保您的HBase测试和部署顺利进行,并防止数据 ...

随机推荐

  1. 斐波那契数列_java版本

    package 斐波那契数列; public class fbnq { public static void main(String[] args){ System.out.println(fibon ...

  2. 马上学Android开发在线视频教程全集

    马上学Android开发视频教程全集 马上学Android开发[马上学Android]安卓开发视频教程 001 Androi 马上学Android开发[马上学Android]安卓开发视频教程 002 ...

  3. Android_sharePreference_ex1

    xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...

  4. Arp欺骗攻击的另类应用之屌丝泡妞记

    http://www.2cto.com/Article/201210/163974.html   arp欺骗,我想大家都应该知道怎么回事了.不知道的去问度娘...   就不废话了,还是直接上图上教程比 ...

  5. 【运维】使用FC命令辅助查杀DLL木马

    使用FC命令辅助查杀DLL木马 在windows系统中,system32目录下是木马隐身的好地方,查找起来非常困难,许多木马都削尖了脑袋往那里钻,DLL木马也不例外.针对这一点用户可以在安装好系统和必 ...

  6. [原]unity中WWW isDone方法只能在主线程中调用

    项目中要使用动态加载,原计划是生成WWW对象后,放到一个容器里.由一个独立线程轮询容器里的对象,如果www.isDone为true时,回调一个接口把结果交给请求方. new Thread( new T ...

  7. 【Objective-C】3 -self关键字

    一.Java中的this只能用在动态方法中,不能用在静态方法中 1.在动态方法中使用this关键字 1 public class Student { 2 private int age; 3 publ ...

  8. 使用 x3dom 框架及 WebGL 在浏览器上显示 3 维模型

    如果需要在浏览器上显示 3D 画面的话, 现在一般会使用 ​WebGL, 典型的例如 three.js(​http://mrdoob.github.com/three.js/), 但是 WebGL 对 ...

  9. invalid code signing entitlement的通用暴力解决办法

    1.添加的一台苹果设备为开发机子后,打版本,说profile 没找到,报错 2.上传二进制文件到itunes connect ,报错 3.有时候还什么 appID 无效,报错 烦死他了 我的解决办法, ...

  10. [jquery]高级篇--标签选择

    1>3中初始化 $(document).ready(function(){ alert("开始了"); }); $(function(){ trace("初始化方法 ...