赞
踩
接上一篇HBase客户端JAVA开发(一),本篇在HBase客户端通过IDEA平台使用JAVA编程实现以下功能:
1.判断一张表是否存在
2.在hbase中创建表
3.删除hbase的表
4.往表插入数据
5.扫描表数据 (scan全表扫描)
6.按照表名、ROWKEY获取一行数据
一、DEMO目录树结构
在resources下只需要添加hadoop配置中的core-site.xml(可到集群节点中的/etc/hadoop文件夹中下载)、hbase-site.xml(可到集群节点中的/etc/hbase文件夹中下载)、log4j.properties(可到/etc/hadoop文件夹中下载,用于日志生成)三个文件即可。
二、功能实现
首先要从resources的core-site.xml和hbase-site.xml获取集群的配置信息,用一个静态变量conf保存这些信息
- public static Configuration conf;
-
- static{
- conf = HBaseConfiguration.create();
-
- }
接下来就可以一一实现相关功能了:
1、判断一张表是否存在
这里完成两项操作:
• 连接HBase数据库——应用Connection类对象;
• 通过表名来查找数据库中是否有同名的表——应用HBaseAdmin类对象;
- public static boolean isExist(String tableName){
- //对表操作需要使用HbaseAdmin
- try {
- Connection connection = ConnectionFactory.createConnection(conf);
- //管理表
- HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
-
- return admin.tableExists(TableName.valueOf(tableName));
-
- } catch (IOException e) { e.printStackTrace();}
- return false;
- }
2、在hbase中创建表
这里完成三项操作:
• 创建表描述器
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
• 创建列族
- for(String cf:columnfamily){
-
- htd.addFamily(new HColumnDescriptor(cf));
- }
• 创建表
- admin.createTable(htd);
- System.out.println("表已经创建成功!");
3、删除hbase的表
通过表名判断表是否存在,若存在则删除,否则给出提示信息
- if (!isExist(tableName)) {
- System.out.println("表不存在");}
- else {
- //2.如果表存在,删除
- admin.disableTable(TableName.valueOf(tableName));
- admin.deleteTable(TableName.valueOf(tableName));
- System.out.println("表删除了");
- }
4、插入表数据
应用Put对象来实现数据插入,注意HBase存储的都是字节数组,没有具体类型;
- Table t = connection.getTable(TableName.valueOf(tableName));
- Put p = new Put(Bytes.toBytes(rowkey));
- //加入数据:列族,列名,列值
- p.addColumn(Bytes.toBytes(cf),Bytes.toBytes(column),Bytes.toBytes(value));
- t.put(p);
5、扫描表数据
通过Scan类和ResultScanner类对象共同完成整个表数据扫描
- //对表操作需要使用HbaseAdmin
- Connection connection = ConnectionFactory.createConnection(conf);
- Table t = connection.getTable(TableName.valueOf(tableName));
-
- //1.实例scan
- Scan s = new Scan();
- //2.拿到Scanner对象
- ResultScanner rs = t.getScanner(s);
- //3.遍历
- for (Result r:rs){
- Cell[] cells = r.rawCells();
- //遍历具体数据
- for (Cell c : cells){
- System.out.print("行键为:"+Bytes.toString(CellUtil.cloneRow(c))+" ");
- System.out.print("列族为:"+Bytes.toString(CellUtil.cloneFamily(c))+" ");
- System.out.print("列名为:"+Bytes.toString(CellUtil.cloneQualifier(c))+" ");
- System.out.println("值为:"+Bytes.toString(CellUtil.cloneValue(c)));
- }
6、按照表名、ROWKEY获取一行数据
- Connection connection = ConnectionFactory.createConnection(conf);
- //拿到表对象
- Table t = connection.getTable(TableName.valueOf(tableName));
-
- //扫描指定数据需要实例对象Get
- Get get = new Get(Bytes.toBytes(rowkey));
- //可加过滤条件
- get.addFamily(Bytes.toBytes("info"));
- Result rs = t.get(get);
- //遍历
- Cell[] cells = rs.rawCells();
- for (Cell c : cells){
- System.out.print("行键为:"+ Bytes.toString(CellUtil.cloneRow(c))+" ");
- System.out.print("列族为:"+Bytes.toString(CellUtil.cloneFamily(c))+" ");
- System.out.print("列名:"+Bytes.toString(CellUtil.cloneQualifier(c))+" ");
- System.out.println("值为:"+Bytes.toString(CellUtil.cloneRow(c))+" ");
- }
三、完整程序展示
- package HBaseDemo1;
-
- import com.codahale.metrics.ConsoleReporter;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.*;
- import org.apache.hadoop.hbase.client.*;
- import org.apache.hadoop.hbase.util.Bytes;
- import org.junit.After;
- import org.junit.Before;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
-
-
- public class HBaseDemo1 {
- //获取配置信息
- public static Configuration conf;
-
- static{
- conf = HBaseConfiguration.create();
-
- }
-
- //1.判断一张表是否存在
- public static boolean isExist(String tableName){
- //对表操作需要使用HbaseAdmin
- try {
- Connection connection = ConnectionFactory.createConnection(conf);
- //管理表
- HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
- return admin.tableExists(TableName.valueOf(tableName));
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
-
- //2.在hbase创建表
- public static void createTable(String tableName,String... columnfamily){
- try {
- //对表操作需要使用HbaseAdmin
- Connection connection = ConnectionFactory.createConnection(conf);
- //管理表
- HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
-
- //表如果存在,请输入其他表名
- if(isExist(tableName)){
- System.out.println("表存在,请输入其他表名");
- }else{
- //注意:创建表的话,需要创建一个描述器
- HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
-
- //创建列族
- for(String cf:columnfamily){
- htd.addFamily(new HColumnDescriptor(cf));
- }
-
- //创建表
-
- admin.createTable(htd);
- System.out.println("表已经创建成功!");
-
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- //3.删除hbase的表
- public static void deleteTable(String tableName) {
- try {
- //对表操作需要使用HbaseAdmin
- Connection connection = ConnectionFactory.createConnection(conf);
- //管理表
- HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
-
- //1.表如果存在,请输入其他表名
- if (!isExist(tableName)) {
- System.out.println("表不存在");
- } else {
- //2.如果表存在,删除
- admin.disableTable(TableName.valueOf(tableName));
- admin.deleteTable(TableName.valueOf(tableName));
- System.out.println("表删除了");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- //4.添加数据put 'user','rowkey','info:name','tony'
- public static void addRow(String tableName,String rowkey,String cf,String column,String value){
- try {
- //对表操作需要使用HbaseAdmin
- Connection connection = ConnectionFactory.createConnection(conf);
- Table t = connection.getTable(TableName.valueOf(tableName));
- //1.表如果存在,请输入其他表名
- if (!isExist(tableName)) {
- System.out.println("表不存在");
- } else {
- //用put方式加入数据
- Put p = new Put(Bytes.toBytes(rowkey));
- //加入数据
- p.addColumn(Bytes.toBytes(cf),Bytes.toBytes(column),Bytes.toBytes(value));
- t.put(p);
-
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
-
- //5.扫描表数据 scan全表扫描
- public static void scanAll(String tableName){
- try {
- //对表操作需要使用HbaseAdmin
- Connection connection = ConnectionFactory.createConnection(conf);
- Table t = connection.getTable(TableName.valueOf(tableName));
-
- //实例scan
- Scan s = new Scan();
- //拿到Scanner对象
- ResultScanner rs = t.getScanner(s);
-
- //遍历
- for (Result r:rs){
- Cell[] cells = r.rawCells();
- //遍历具体数据
- for (Cell c : cells){
- System.out.print("行键为:"+Bytes.toString(CellUtil.cloneRow(c))+" ");
- System.out.print("列族为:"+Bytes.toString(CellUtil.cloneFamily(c))+" ");
- System.out.print("列名为:"+Bytes.toString(CellUtil.cloneQualifier(c))+" ");
- System.out.println("值为:"+Bytes.toString(CellUtil.cloneValue(c)));
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- //6、按照表名、ROWKEY获取一行数据
- public static void getRow(String tableName,String rowkey) throws IOException {
- Connection connection = ConnectionFactory.createConnection(conf);
- //拿到表对象
- Table t = connection.getTable(TableName.valueOf(tableName));
-
- //扫描指定数据需要实例对象Get
- Get get = new Get(Bytes.toBytes(rowkey));
- //可加过滤条件
- get.addFamily(Bytes.toBytes("info"));
- Result rs = t.get(get);
- //遍历
- Cell[] cells = rs.rawCells();
- for (Cell c : cells){
- System.out.print("行键为:"+ Bytes.toString(CellUtil.cloneRow(c))+" ");
- System.out.print("列族为:"+Bytes.toString(CellUtil.cloneFamily(c))+" ");
- System.out.print("列名:"+Bytes.toString(CellUtil.cloneQualifier(c))+" ");
- System.out.println("值为:"+Bytes.toString(CellUtil.cloneRow(c))+" ");
- }
- }
- //7.删除表中一行数据
- public static void deleteRow(String tableName,String rowkey,String cf ){
- try {
- //对表操作需要使用HbaseAdmin
- Connection connection = ConnectionFactory.createConnection(conf);
- Table t = connection.getTable(TableName.valueOf(tableName));
- //1.表如果存在,请输入其他表名
- if (!isExist(tableName)) {
- System.out.println("表不存在");
- } else {
- //1.根据rowkey删除数据
- Delete delete = new Delete(Bytes.toBytes(rowkey));
- //2.删除
- t.delete(delete);
- System.out.println("删除成功");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- //8.删除多行数据
- public static void deleteAll(String tableName,String... rowkeys){
- try {
- //对表操作需要使用HbaseAdmin
- Connection connection = ConnectionFactory.createConnection(conf);
- Table t = connection.getTable(TableName.valueOf(tableName));
- //1.表如果存在,请输入其他表名
- if (!isExist(tableName)) {
- System.out.println("表不存在");
- } else {
- //1.把delete封装到集合
- List<Delete> list = new ArrayList<Delete>();
- //2.遍历
- for (String row:rowkeys){
- Delete d = new Delete(Bytes.toBytes(row));
- list.add(d);
- }
- t.delete(list);
- System.out.println("删除成功");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- //主程序
- public static void main(String[] args) throws IOException {
- // System.out.println(isExist("emp"));
- createTable("yangmi","info");
- //deleteTable("tony");
- addRow("yangmi","101","info","age","20");
- //deleteRow("yangmi","101","info");
- //deleteAll("emp","1001","1002");
- //scanAll("yangmi");
- getRow("yangmi","101");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。