HBase编程:

一):大数据(hadoop)初始化环境搭建

二):大数据(hadoop)环境搭建

三):运行wordcount案例

四):揭秘HDFS

五):揭秘MapReduce

六):揭秘HBase

七):HBase编程

-----------------------------------------------------------------

HBase JavaAPI概述:

  1.HBase使用Java语言编写的,自然支持Java编程

  2.支持CRUD操作:create read update delete

  3.JavaAPI包含了所有HBase的shell,甚至更多

  4.JavaAPI是访问HBase的最快方式

JavaAPI程序设计:

1、创建一个Configuration

Configuration conf = HbaseConfiguration.create();

Configuration对象包含了连接到HBase服务的信息:zookeeper的位置,连接时间等

HbaseConfiguration.create()内部逻辑:

  从CLASSPATH下加载hbase-default.xml和hbase-site.xml文件需将hbase-site.xml放入到CLASSPATH下 hbase-site.xml将覆盖hbase-default.xml的同名属性

自定义配置文件,使用Configuration加载

  Configuration newConf = Configuration.create(existingConf);

  用户自定义的配置文件将在已有配置文件之后加载将覆盖hbase-default.xml和 hbase-site.xml中的配置

自定义参数值:

Configuration conf=HbaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", "admin-01,admin-02");

通常不推荐这么做!

2、创建HTable句柄

  提供Configuration对象和待访问Table名称 HTable table = new HTable(conf, tableName);

一个table对应一个HTbale句柄:

  提供了CRUD操作

  设计简单、使用方便

  提供行级事务

  不支持多行事务或者表级别的事务

  严格的行一致性

  并发读、顺序写

创建HTable句柄代价很大  扫描.META.表等; 

  创建一次,以后尽可能复用;

  如果需要创建多个Htable句柄,使用 HTablePool;

  HTable并非线程安全的  一个线程创建一个即可

  Htable支持CRUD批处理

  非线程安全,仅是为了提高性能

3、执行相应的CRUD操作

  执行put、get、delete、scan等操作

  table.getTableName();

4、关闭HTable句柄

  将内存数据刷新到磁盘上  释放各种资源

  table.close();

前期工作:

将虚拟主机当中的hbase-site.xml和hdfs-site.xml文件复制到项目下的classpath下

修改windows真机的hosts文件,添加三台机器的映射

导入POM文件:

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.0</version>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.8.0</version>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.8.0</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.3.1</version>
</dependency>

HBase_API 操作:

package com.gdbd;

import java.io.IOException;
import java.util.Scanner; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
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; /**
* Hello world!
*
*/
public class App { public static void main(String[] args) throws IOException { do {
System.out.println("\n\n*********************HBase_API***神码操作平台*********************");
System.out.println("1、创建表");
System.out.println("2、向表中添加数据");
System.out.println("3、查询表中所有数据");
System.out.println("4、查询表中指定RowKey的所有数据");
System.out.println("5、删除指定列族的指定列");
System.out.println("6、删除指定列族");
System.out.println("7、删除表中指定RowKey的所有数据删除表中指定RowKey的所有数据");
System.out.println("8、删除表");
Scanner input = new Scanner(System.in);
System.out.println("请输入......");
int num = input.nextInt();
switch (num) {
case 1:
demo01();
break;
case 2:
demo02();
break;
case 3:
cat();
break;
case 4:
catRows();
break;
case 5:
delFamilyColumn();
break;
case 6:
delFamily();
break;
case 7:
delRow();
break;
case 8:
delTable();
break;
}
} while (true);
} /***
* 创建表
*
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
private static void demo01() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
System.out.println("正在 创建表...\n");
// 创建一个Configuration对象
Configuration configuration = HBaseConfiguration.create();
/**
* 创建HBaseAdmin对象 此对象 提供了 建表,创建列族,检查表是否存在,修改表和列族结构,删除表等功能 HBaseAdmin实例的生命周期不宜太长
*/
HBaseAdmin admin = new HBaseAdmin(configuration);
if (admin.tableExists("hbase_demo")) {
System.out.println("表已经存在!!!");
} else { // 证明表不存在
HTableDescriptor table = new HTableDescriptor("hbase_demo"); // 创建表的描述对象
// new HColumnDescriptor对象:设置列族的特性
table.addFamily(new HColumnDescriptor("grade"));
table.addFamily(new HColumnDescriptor("course"));
// 定义好了表名和列族 可以创建表
admin.createTable(table);
System.out.println("表创建成功!!!");
}
} /***
* 向表中添加数据
*
* @throws IOException
*/
private static void demo02() throws IOException {
System.out.println("正在 向表中添加数据...\n");
// 创建一个Configuration对象
Configuration configuration = HBaseConfiguration.create();
// 创建HTable对象
HTable table = new HTable(configuration, "hbase_demo");
// 创建put对象
Put put = new Put("xiaohei".getBytes()); // xiaohei是 row key
put.addColumn("course".getBytes(), "java".getBytes(), "90".getBytes()); // course是列族
put.addColumn("course".getBytes(), "sql".getBytes(), "99".getBytes()); // java 和sql 都是列
put.addColumn("grade".getBytes(), "one".getBytes(), "1".getBytes());
// 向表中增加数据
table.put(put);
// 关闭table
table.close();
System.out.println("插入成功......");
} /***
* 查询表数据
*
* @throws IOException
*/
private static void cat() throws IOException {
System.out.println("正在 查询表数据...\n");
// 创建一个Configuration对象
Configuration configuration = HBaseConfiguration.create();
// 创建HTable对象
HTable table = new HTable(configuration, "hbase_demo");
// 创建ResultScanner对象
ResultScanner rs = table.getScanner(new Scan());
for (Result r : rs) {
System.out.println("***************************Result Start***************************");
for (Cell cell : r.rawCells()) {
System.out.println("=======================start============================");
System.out.println("RowKey(行键)===>" + Bytes.toString(r.getRow()));
System.out.println("family(列族)===>" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("column(列)===>" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value(值)===>" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("=========================END==========================");
}
System.out.println("****************************Result END****************************");
}
// 关闭table
table.close();
System.out.println("表数据查询成功...");
} /***
* 查询表中指定RowKey的所有数据
*
* @throws IOException
*/
private static void catRows() throws IOException {
System.out.println("正在 查询表中指定RowKey的所有数据...\n");
// 创建一个Configuration对象
Configuration configuration = HBaseConfiguration.create();
// 创建HTable对象
HTable table = new HTable(configuration, "hbase_demo");
// 创建get对象 获取所有rowkey是xiaohei的所有数据
Get get = new Get(Bytes.toBytes("xiaohei"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("=======================start============================");
System.out.println("RowKey(行键)===>" + Bytes.toString(result.getRow()));
System.out.println("family(列族)===>" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("column(列)===>" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value(值)===>" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("=========================END==========================\n");
}
// 关闭table
table.close();
System.out.println("成功 查询表中指定RowKey的所有数据");
} /***
* 删除表中指定RowKey的所有数据
*
* @throws IOException
*/
private static void delRow() throws IOException {
System.out.println("正在删除表中指定RowKey的所有数据...\n");
// 创建一个Configuration对象
Configuration configuration = HBaseConfiguration.create();
// 创建HTable对象
HTable table = new HTable(configuration, "hbase_demo");
// 创建delete对象
Delete delete = new Delete(Bytes.toBytes("xiaohei"));
table.delete(delete);
// 关闭table
table.close();
System.out.println("成功删除表中指定RowKey的所有数据...");
} /***
* 删除指定列族的指定列
*
* @throws IOException
*/
private static void delFamilyColumn() throws IOException {
System.out.println("正在 删除指定列族的指定列...\n");
// 创建一个Configuration对象
Configuration configuration = HBaseConfiguration.create();
// 创建HTable对象
HTable table = new HTable(configuration, "hbase_demo");
// 创建delete对象
Delete delete = new Delete(Bytes.toBytes("xiaobaibai"));// 指定的
delete.addColumn(Bytes.toBytes("course"), Bytes.toBytes("java"));
table.delete(delete);
// 关闭table
table.close();
System.out.println("成功 删除指定列族的指定列...");
} /***
* 删除指定列族的
*
* @throws IOException
*/
private static void delFamily() throws IOException {
System.out.println("正在 删除指定列族...\n");
// 创建一个Configuration对象
Configuration configuration = HBaseConfiguration.create();
// 创建HTable对象
HTable table = new HTable(configuration, "hbase_demo");
// 创建delete对象
Delete delete = new Delete(Bytes.toBytes("xiaobaibai"));// 指定的
delete.addFamily("course".getBytes());
table.delete(delete);
// 关闭table
table.close();
System.out.println("成功 删除指定列族...");
} /***
* 删除表
*
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
private static void delTable() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
System.out.println("正在 删除表...\n");
// 创建一个Configuration对象
Configuration configuration = HBaseConfiguration.create();
// 创建HAdmin对象
HBaseAdmin admin = new HBaseAdmin(configuration);
admin.disableTable("hbase_demo");// 禁用表
admin.deleteTable("hbase_demo");// 删除表
// 关闭admin对象
admin.close();
System.out.println("成功 删除表...");
} }

最新文章

  1. Python高手之路【三】python基础之函数
  2. Java 第7章 数组
  3. x01.os.16: 添加功能
  4. DIV设置overflow无效的原因
  5. More on Conditions - To Compare -Comparing Sequences and Other Types
  6. Beta冲刺集合
  7. (译) JSON-RPC 2.0 规范(中文版)
  8. python实现简体中文和繁体相互转换
  9. swagger-ui中测试接口(enum传值) 报400错误
  10. RobotFramework测试问题二:各种元素不能定位问题
  11. (转)c#反射
  12. bzoj4873(最大权闭合子图)
  13. javascript 正则(将数字转化为三位分隔的样式)【转】
  14. c#double类型保留百分号后两位,且禁止四舍五入的方法
  15. 学习magento要学哪些知识
  16. CentOS /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
  17. shell 转码BIG5 UTF8
  18. 网站功能操作分布引导插件:Intro.js介绍;React里如何使用Intro.js以及如何进行分页导航
  19. 表格Table宽度设置无效的解决方法
  20. 大数据分析系统Hadoop的13个开源工具

热门文章

  1. leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法
  2. CentOS6.9 下编译安装MySQL5.7.19
  3. 【JMeter4.0学习(五)】JMeter对服务器监控测试脚本开发
  4. 基于markdown的blog系统调研1:typecho
  5. vuforia 中摄像机的开启与关闭
  6. ios uitableview button 获取cell indexpath.row
  7. Cocos2d-x中使用第三方so库
  8. tomcat下发布项目,遇到的问题总结
  9. double,long double及各变量数组内存开销
  10. 【BZOJ3651】网络通信 LCT