Hbase实例
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实例的更多相关文章
- java操作Hbase实例
所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...
- 使用C#和Thrift来访问Hbase实例
今天试着用C#和Thrift来访问Hbase,主要参考了博客园上的这篇文章.查了Thrift,Hbase的资料,结合博客园的这篇文章,终于搞好了.期间经历了不少弯路,下面我尽量详细的记录下来,免得大家 ...
- sqoop1.4.6从mysql导入hdfs\hive\hbase实例
//验证sqoop是否连接到mysql数据库sqoop list-tables --connect 'jdbc:mysql://n1/guizhou_test?useUnicode=true& ...
- 从零自学Hadoop(20):HBase数据模型相关操作上
阅读目录 序 介绍 命名空间 表 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...
- Python操作HBase之happybase
安装Thrift 安装Thrift的具体操作,请点击链接 pip install thrift 安装happybase pip install happybase 连接(happybase.Conne ...
- 云HBase发布全文索引服务,轻松应对复杂查询
云HBase发布了“全文索引服务”功能,自2019年01月25日后创建的云HBase实例,可以在控制台免费开启此“全文索引服务”功能.使用此功能可以让用户在HBase之上构建功能更丰富的搜索业务,不再 ...
- 阿里云HBase携X-Pack再进化,重新赋能轻量级大数据平台
一.八年双十一,造就国内最大最专业HBase技术团队 阿里巴巴集团早在2010开始研究并把HBase投入生产环境使用,从最初的淘宝历史交易记录,到蚂蚁安全风控数据存储.持续8年的投入,历经8年双十一锻 ...
- 阿里云HBase全新发布X-Pack 赋能轻量级大数据平台
一.八年双十一,造就国内最大最专业HBase技术团队 阿里巴巴集团早在2010开始研究并把HBase投入生产环境使用,从最初的淘宝历史交易记录,到蚂蚁安全风控数据存储.持续8年的投入,历经8年双十一锻 ...
- Hbase学习02
第2章 Apache HBase配置 本章在“入门”一章中进行了扩展,以进一步解释Apache HBase的配置. 请仔细阅读本章,特别是基本先决条件,确保您的HBase测试和部署顺利进行,并防止数据 ...
随机推荐
- php关于日期时间 php日期 php时间
strtotime 的牛逼用法: $a='-4 days '.date('Y-m-d');$day = date('Y-m-d', strtotime($a));var_dump($day); /** ...
- 如何快速的开发一个完整的iOS直播app(原理篇)
目录 [如何快速的开发一个完整的iOS直播app](原理篇) [如何快速的开发一个完整的iOS直播app](播放篇) [如何快速的开发一个完整的iOS直播app](采集篇) 前言 大半年没写博客了,但 ...
- 如何真正免费运营推广APP应用
随着移动终端的迅速普及,各类APP如雨后春笋般涌现出来,但是真正的运营成功的产品却寥寥无几. 从瓜分渠道资源到抢占用户的过程中,很多同行都明显的感觉到,渠道平台所带来的量日益减少,但是刊例价格却一再攀 ...
- jemalloc源码结构分析(二):CPU字节对齐算法
在调用arena_malloc_small过程中,要根据申请内存大小,进行对齐计算,然后分配一个整块儿.算法如下: 1)定义一个SIZE_CLASSES宏,它主要用于生成后面两个表,small_siz ...
- Centos 6.4 python 2.6 升级到 3.5.2
查看python的版本 #python -V Python 1.下载Python-2.5.2 #wget https://www.python.org/ftp/python/3.5.2/Python- ...
- java集合总结
java中集合是很重要的一点,巩固这边学习的知识,把知识理一下 按马士兵的视频,总结的也很好,集合就是一个“1136” 1个图,1个类Collections,3个知识点:增强for循环,泛型,打包和解 ...
- vagrant yii2 Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002]
开发环境:vangrant + LAMP 安装了yii2 advanced版本之后,通过url访问fornted 报数据库user表不存在,看了安装yii2 advanced的教程,里面说需要需要运行 ...
- dede版权信息修改
login:dede-templets-login.htm 系统主页:dede-templets-index2.htm 主体内容在index_body.htm文件 干掉: $(function() ...
- PHP之基本语法
人生最幸福的事之一就是,邻居家的wifi密码是123456789,于是回家在pad上也照样可以扯淡.任何语言都有自己的语法,这里只简单说些我觉得应该注意的地方. 首先要明白,PHP是运行于服务器端的脚 ...
- Microsoft JScript 运行时错误: Sys.WebForms.PageRequestManagerParserErrorException无法分析从服务器收到的消息。之所以出现此错误,
Microsoft JScript 运行时错误: Sys.WebForms.PageRequestManagerParserErrorException: 无法分析从服务器收到的消息.之所以出现此错误 ...