java操作Hbase实例

时间:2022-11-10 11:24:15

所用HBase版本为1.1.2,hadoop版本为2.4

/*
* 创建一个students表,并进行相关操作
*/
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.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 HBaseDBDao {
// 声明静态配置
private static Configuration conf = null; static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.1.154");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", "192.168.1.154:6000");
} //判断表是否存在
private static boolean isExist(String tableName) throws IOException {
HBaseAdmin hAdmin = new HBaseAdmin(conf);
return hAdmin.tableExists(tableName);
} // 创建数据库表
public static void createTable(String tableName, String[] columnFamilys)
throws Exception {
// 新建一个数据库管理员
HBaseAdmin hAdmin = new HBaseAdmin(conf);
if (hAdmin.tableExists(tableName)) {
System.out.println("表 "+tableName+" 已存在!");
System.exit(0);
} else {
// 新建一个students表的描述
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
// 在描述里添加列族
for (String columnFamily : columnFamilys) {
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
}
// 根据配置好的描述建表
hAdmin.createTable(tableDesc);
System.out.println("创建表 "+tableName+" 成功!");
}
} // 删除数据库表
public static void deleteTable(String tableName) throws Exception {
// 新建一个数据库管理员
HBaseAdmin hAdmin = new HBaseAdmin(conf);
if (hAdmin.tableExists(tableName)) {
// 关闭一个表
hAdmin.disableTable(tableName);
hAdmin.deleteTable(tableName);
System.out.println("删除表 "+tableName+" 成功!");
} else {
System.out.println("删除的表 "+tableName+" 不存在!");
System.exit(0);
}
} // 添加一条数据
public static void addRow(String tableName, String row,
String columnFamily, String column, String value) throws Exception {
HTable table = new HTable(conf, 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(conf, tableName);
Delete del = new Delete(Bytes.toBytes(row));
table.delete(del);
} // 删除多条数据
public static void delMultiRows(String tableName, String[] rows)
throws Exception {
HTable table = new HTable(conf, tableName);
List<Delete> delList = new ArrayList<Delete>();
for (String row : rows) {
Delete del = new Delete(Bytes.toBytes(row));
delList.add(del);
}
table.delete(delList);
} // 获取一条数据
public static void getRow(String tableName, String row) throws Exception {
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
// 输出结果,raw方法返回所有keyvalue数组
for (KeyValue rowKV : result.raw()) {
System.out.print("行名:" + new String(rowKV.getRow()) + " ");
System.out.print("时间戳:" + rowKV.getTimestamp() + " ");
System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");
System.out.println("值:" + new String(rowKV.getValue()));
}
} // 获取所有数据
public static void getAllRows(String tableName) throws Exception {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
// 输出结果
for (Result result : results) {
for (KeyValue rowKV : result.raw()) {
System.out.print("行名:" + new String(rowKV.getRow()) + " ");
System.out.print("时间戳:" + rowKV.getTimestamp() + " ");
System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
System.out
.print("列名:" + new String(rowKV.getQualifier()) + " ");
System.out.println("值:" + new String(rowKV.getValue()));
}
}
} // 主函数
public static void main(String[] args) {
try {
String tableName = "student";
// 第一步:创建数据库表:“student”
String[] columnFamilys = { "info", "course" };
HBaseDBDao.createTable(tableName, columnFamilys);
// 第二步:向数据表的添加数据
// 添加第一行数据
if (isExist(tableName)) {
HBaseDBDao.addRow(tableName, "zpc", "info", "age", "20");
HBaseDBDao.addRow(tableName, "zpc", "info", "sex", "boy");
HBaseDBDao.addRow(tableName, "zpc", "course", "china", "97");
HBaseDBDao.addRow(tableName, "zpc", "course", "math", "128");
HBaseDBDao.addRow(tableName, "zpc", "course", "english", "85");
// 添加第二行数据
HBaseDBDao.addRow(tableName, "henjun", "info", "age", "19");
HBaseDBDao.addRow(tableName, "henjun", "info", "sex", "boy");
HBaseDBDao.addRow(tableName, "henjun", "course", "china","90");
HBaseDBDao.addRow(tableName, "henjun", "course", "math","120");
HBaseDBDao.addRow(tableName, "henjun", "course", "english","90");
// 添加第三行数据
HBaseDBDao.addRow(tableName, "niaopeng", "info", "age", "18");
HBaseDBDao.addRow(tableName, "niaopeng", "info", "sex","girl");
HBaseDBDao.addRow(tableName, "niaopeng", "course", "china","100");
HBaseDBDao.addRow(tableName, "niaopeng", "course", "math","100");
HBaseDBDao.addRow(tableName, "niaopeng", "course", "english","99");
// 第三步:获取一条数据
System.out.println("**************获取一条(zpc)数据*************");
HBaseDBDao.getRow(tableName, "zpc");
// 第四步:获取所有数据
System.out.println("**************获取所有数据***************");
HBaseDBDao.getAllRows(tableName); // 第五步:删除一条数据
System.out.println("************删除一条(zpc)数据************");
HBaseDBDao.delRow(tableName, "zpc");
HBaseDBDao.getAllRows(tableName);
// 第六步:删除多条数据
System.out.println("**************删除多条数据***************");
String rows[] = new String[] { "henjun","niaopeng" };
HBaseDBDao.delMultiRows(tableName, rows);
HBaseDBDao.getAllRows(tableName);
// 第七步:删除数据库
System.out.println("***************删除数据库表**************");
HBaseDBDao.deleteTable(tableName);
System.out.println("表"+tableName+"存在吗?"+isExist(tableName));
} else {
System.out.println(tableName + "此数据库表不存在!");
} } catch (Exception e) {
e.printStackTrace();
}
} }

项目所需jar包如下:

java操作Hbase实例

程序运行结果如下:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
十一月 25, 2015 9:43:05 上午 org.apache.hadoop.util.NativeCodeLoader <clinit>
警告: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
十一月 25, 2015 9:43:06 上午 org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper <init>
信息: Process identifier=hconnection-0x1cdc2ce connecting to ZooKeeper ensemble=192.168.1.154:2181
十一月 25, 2015 9:43:11 上午 org.apache.hadoop.hbase.client.HBaseAdmin postOperationResult
信息: Created student
创建表 student 成功!
**************获取一条(zpc)数据*************
行名:zpc 时间戳:1448415791324 列族名:course 列名:china 值:97
行名:zpc 时间戳:1448415791333 列族名:course 列名:english 值:85
行名:zpc 时间戳:1448415791329 列族名:course 列名:math 值:128
行名:zpc 时间戳:1448415791285 列族名:info 列名:age 值:20
行名:zpc 时间戳:1448415791314 列族名:info 列名:sex 值:boy
**************获取所有数据***************
行名:henjun 时间戳:1448415791344 列族名:course 列名:china 值:90
行名:henjun 时间戳:1448415791360 列族名:course 列名:english 值:90
行名:henjun 时间戳:1448415791354 列族名:course 列名:math 值:120
行名:henjun 时间戳:1448415791336 列族名:info 列名:age 值:19
行名:henjun 时间戳:1448415791340 列族名:info 列名:sex 值:boy
行名:niaopeng 时间戳:1448415791377 列族名:course 列名:china 值:100
行名:niaopeng 时间戳:1448415791385 列族名:course 列名:english 值:99
行名:niaopeng 时间戳:1448415791381 列族名:course 列名:math 值:100
行名:niaopeng 时间戳:1448415791368 列族名:info 列名:age 值:18
行名:niaopeng 时间戳:1448415791374 列族名:info 列名:sex 值:girl
行名:zpc 时间戳:1448415791324 列族名:course 列名:china 值:97
行名:zpc 时间戳:1448415791333 列族名:course 列名:english 值:85
行名:zpc 时间戳:1448415791329 列族名:course 列名:math 值:128
行名:zpc 时间戳:1448415791285 列族名:info 列名:age 值:20
行名:zpc 时间戳:1448415791314 列族名:info 列名:sex 值:boy
************删除一条(zpc)数据************
行名:henjun 时间戳:1448415791344 列族名:course 列名:china 值:90
行名:henjun 时间戳:1448415791360 列族名:course 列名:english 值:90
行名:henjun 时间戳:1448415791354 列族名:course 列名:math 值:120
行名:henjun 时间戳:1448415791336 列族名:info 列名:age 值:19
行名:henjun 时间戳:1448415791340 列族名:info 列名:sex 值:boy
行名:niaopeng 时间戳:1448415791377 列族名:course 列名:china 值:100
行名:niaopeng 时间戳:1448415791385 列族名:course 列名:english 值:99
行名:niaopeng 时间戳:1448415791381 列族名:course 列名:math 值:100
行名:niaopeng 时间戳:1448415791368 列族名:info 列名:age 值:18
行名:niaopeng 时间戳:1448415791374 列族名:info 列名:sex 值:girl
**************删除多条数据***************
***************删除数据库表**************
十一月 25, 2015 9:43:11 上午 org.apache.hadoop.hbase.client.HBaseAdmin call
信息: Started disable of student
十一月 25, 2015 9:43:15 上午 org.apache.hadoop.hbase.client.HBaseAdmin postOperationResult
信息: Disabled student
十一月 25, 2015 9:43:18 上午 org.apache.hadoop.hbase.client.HBaseAdmin postOperationResult
信息: Deleted student
删除表 student 成功!
表student存在吗?false

参考链接:

新浪微博数据解析与java操作Hbase实例

Java对hbase的操作

HBase Java简单示例

我的HBase之旅

java操作Hbase实例的更多相关文章

  1. Java 操作 HBase 教程

    Java 操作 HBase 教程 一.简介 二.hbase-client 引入 三.连接操作 四.表操作 五.运行测试 相关博文原文地址: 博客园:美码师:HBase(2) Java 操作 HBase ...

  2. Hbase深入学习&lpar;六&rpar; Java操作HBase

    Hbase深入学习(六) ―― Java操作HBase 本文讲述如何用hbase shell命令和hbase java api对hbase服务器进行操作. 先看以下读取一行记录hbase是如何进行工作 ...

  3. Java操作hbase总结

    用过以后,总得写个总结,不然,就忘喽. 一.寻找操作的jar包. java操作hbase,首先要考虑到使用hbase的jar包. 因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到 ...

  4. memcached—Java操作Memcached实例

    前面博客介绍了如何在Windows操作系统中安装Memcached,总结一下如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; imp ...

  5. 错误&colon; 找不到或无法加载主类 java操作hbase出错

    用java操作hbase 利用maven引入hbase包后发现无法启动程序,然后网上说是包的冲突. 我引入了下面三个包然后程序就不能运行了. <dependency> <groupI ...

  6. 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  7. &lpar;转&rpar;Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  8. Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  9. HBase之四--&lpar;1&rpar;:Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

随机推荐

  1. osg 中鼠标拾取线段的端点和中点

    //NeartestPointNodeVisitor.h #pragma once #include <osg\Matrix> #include <vector> #inclu ...

  2. 怎么去掉li标签前面的点&quest;&quest;

    <ul class="list"> <li> </li> <li> </li> </ul> .list li ...

  3. JavaScript DOM高级程序设计 5动态修改样式和层叠样式表2--我要坚持到底!

    把样式置于DOM脚本之外 style属性 我们可以这样设置前景色之类的属性: element.style.color='red'; 也可以使用下面的代码设置背景颜色: element.style.ba ...

  4. 我的开源框架之Accordion控件

    需求: (1)实现手风琴面板控件,支持静态HTML与JSON方式创建控件 (2)支持远程加载数据 (3)支持面板激活.远程加载事件注册 (4)支持动态添加.删除项目 实现图例 客户代码 <div ...

  5. HTML入门5

    格式化文本,高阶处理,接下来了解,标记引文,描述列表,计算机代码和其他文本,上下标,联系信息等数据. 学习不太知名的HTML元素来标记高级语义特征. 描述列表,也叫自定义列表,第三种类型的列表,除了u ...

  6. complex类的定义和实现

    #include<iostream> #include<cmath> using namespace std; class complex { public: complex( ...

  7. day01-计算机原理与操作系统

  8. 从浏览器地址栏输入URL到浏览器呈现数据全过程解析

    一.输入设备(或粘贴)输入 URL,按下 Enter键 或其他按钮开始请求. 二.浏览器开始解析 URL 关于 URL 到相关知识点:什么是URI,URL以及URN,你真的理解了吗. 1.URL 是否 ...

  9. Linux内核分析——第六周学习笔记20135308

    第六周 进程的描述和进程的创建 一.进程描述符task_struct数据结构 1.操作系统三大功能 进程管理 内存管理 文件系统 2.进程控制块PCB——task_struct 也叫进程描述符,为了管 ...

  10. postman 学习网址

    postman使用详解: http://gold.xitu.io/entry/57597a62a341310061337885 https://www.getpostman.com/docs/writ ...