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测试和部署顺利进行,并防止数据 ...
随机推荐
- Debian中完全卸载MySQL
之前服务器上配置测试用的服务环境,我偷懒顺手用网上现成的脚本进行安装,结果MySQL启动不了,于是我只有老老实实的重新安装MySQL Server,原本以为apt-get --purge这类命令可 ...
- Model
using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace iFlytekDemo ...
- xss脚本攻击
xss脚本攻击不仅仅只是alert(1)就算完了,xss脚本攻击真正的用处是盗取普通用户的cookie,或者盗取管理员的cookie. xss分类(类型): 1. 反射型xss2. 存储型xss3. ...
- Oracle取得中文拼音首字母函数
CREATE ) ; V_RETURN ) ; FUNCTION F_NLSSORT (P_WORD IN VARCHAR2) RETURN VARCHAR2 AS BEGIN RETURN NLSS ...
- 将指定SQL的执行计划从共享池删除的方法
如果Oracle的优化器产生了某种错误的执行计划,或者我们希望Oracle对于某个SQL重新进行分析,那么就需要这个SQL的执行计划在共享池中过期,而简单的方法在10.2.0.4以后才出现. 对于 ...
- XMl解析之Pull解析
HttpUtils: package cn.qf.parser; import java.io.BufferedOutputStream; import java.io.FileOutputStrea ...
- ruby 把字符串转为正则匹配表达式
需求 函数,需要通过参数传递字符串,用来做正则匹配 reg = '[0-9]+' def func(str, reg) str.scan(reg) end 由于 reg 在其它地方定义, reg 是字 ...
- javascript开发中的封装模式(转)
var bgAuido={ audio : pingfan.$$('audio'), audioBtn : pingfan.$$('audioBtn'), init : function(){ var ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- 标签的innerHTML属性和html()
在新公司开发编码的时候,经常写js代码:有时候就需要往某个标签里添加一些html脚本或者要拿到某个标签里的html脚本,那么就会用到innerHTML和html. 1.innerHTML属性 w3sc ...