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测试和部署顺利进行,并防止数据 ...
随机推荐
- 探索高效jQuery的奥秘
讨论jQuery和javascript性能的文章并不罕见.然而,本文我计划总结一些速度方面的技巧和我本人的一些建议,来提升你的jQuery和javascript代码.好的代码会带来速度的提升.快速渲染 ...
- mysql与java的之间的连接
package cn.hncu; //注意,以下都是sun公司的接口(类)---这样以后换成Oracle等其它数据库,代码不用动import java.sql.Connection;import ja ...
- View绘制详解(四),谝一谝layout过程
上篇博客我们介绍了View的测量过程,这只是View显示过程的第一步,第二步就是layout了,这个我们一般译作布局,其实就是在View测量完成之后根据View的大小,将其一个一个摆放在ViewGro ...
- iOS之文本属性Attributes的使用
1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...
- 关于C++需要加强学习的几点内容
1.C++ effective指导原则 2.C++标准库 3.数据结构算法 4.C++设计模式 5.shell脚本 6.python
- MySQL提供的错误日志中的错误级别一共有3个分别为:
ERROR_LEVEL-->错误级别 WARNING_LEVEL-->警告级别 INFORMATION_LEVEL-->信息级别
- UVA - 213 Message Decoding (输入字符串并对单个字符进行操作的输入输出)
POINT: 关于表示一个编码:利用code字符数组表示一个编码字符,其中code[len][val]表示长度为len,二进制值为val的字符: 主程序如下: #include <iostrea ...
- PRINTDLG 结构体
//包含 PrintDlg 函数用来初始化Print Dialog Box的信息,在用户关闭窗口后,返回用户选择的信息typedef struct tagPD { DWORD lStructSize; ...
- WCF编程系列(七)信道及信道工厂
WCF编程系列(七)信道及信道工厂 信道及信道栈 前面已经提及过,WCF中客户端与服务端的交互都是通过消息来进行的.消息从客户端传送到服务端会经过多个处理动作,在WCF编程模型中,这些动作是按层 ...
- ###学习《C++ Primer》- 1
点击查看Evernote原文. #@author: gr #@date: 2014-09-30 #@email: forgerui@gmail.com 记录读书过程中一些知识点.可能不系统,:-). ...