为了方便以后查看,总结了一些常用的java操作hbase的代码:

package com.mcq;

import static org.hamcrest.CoreMatchers.describedAs;
import static org.hamcrest.CoreMatchers.nullValue; import java.io.IOException;
import java.io.PushbackInputStream; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScannable;
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes; public class TestHbase {
private static Admin admin = null;
private static Connection connection = null;
private static Configuration conf = null;
static {
// HBase配置文件
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.1.103");
// 获取连接对象
try {
connection = ConnectionFactory.createConnection(conf);
// 获取HBase管理员对象
admin = connection.getAdmin();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} private static void close(Connection conn, Admin admin) throws IOException {
if (conn != null) {
conn.close();
}
if (admin != null) {
admin.close();
}
} // 判断表是否存在
public static boolean tableExist(String tableName) throws IOException {
boolean tableExists = admin.tableExists(TableName.valueOf(tableName));
return tableExists;
} // 创建表
public static void createTable(String tableName, String... cfs) throws IOException {
if (tableExist(tableName)) {
System.out.println("表已存在");
return;
}
// cfs是列族,官方建议一个表一个,但可以有多个
// 创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String cf : cfs) {
// 创建列描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
// hColumnDescriptor.setMaxVersions(3);//设置版本数
hTableDescriptor.addFamily(hColumnDescriptor);
}
// 创建表操作
admin.createTable(hTableDescriptor);
} // 删除表
public static void deleteTable(String tableName) throws IOException {
if (!tableExist(tableName)) {
System.out.println("表不存在");
return;
}
// 使表不可用(下线)
admin.disableTable(TableName.valueOf(tableName));
// 执行删除操作
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("表已删除");
} // 增、改
public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
// 获取表对象
// HTable table=new HTable(conf,TableName.valueOf(tableName)); 已过时
Table table = connection.getTable(TableName.valueOf(tableName));
// 创建put对象
Put put = new Put(Bytes.toBytes(rowKey));
// 添加数据
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
// 执行添加操作
table.put(put);
} // 删
public static void delete(String tableName, String rowKey, String cf, String cn) throws IOException {
// 获取table对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 创建delete对象
Delete delete = new Delete(Bytes.toBytes(rowKey));// 删除整个列族
delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));// 删除所有版本
// delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));不推荐,只删除最新的版本
// 执行删除操作
table.delete(delete);
table.close();
} // 查——全表扫描,只能获取最新版本
public static void scanTable(String tableName) throws IOException {
// 获取table对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 构建扫描器
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan); // 遍历数据并打印
for (Result result : resultScanner) { // rowkey
Cell[] cells = result.rawCells();
for (Cell cell : cells) { // cell
System.out.println("RK:" + Bytes.toString(CellUtil.cloneRow(cell)) + ",CF:"
+ Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:"
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:"
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
table.close();
} // 查——获取指定列族
public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
// get.addFamily(cf);
// get.setMaxVersions();//不传参默认是表结构内的maxversions
get.setMaxVersions(2);
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) { // cell
System.out.println("RK:" + Bytes.toString(CellUtil.cloneRow(cell)) + ",CF:"
+ Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:"
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:"
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
} public static void main(String[] args) throws IOException {
// 判断表是否存在
// System.out.println(tableExist("student"));
// System.out.println(tableExist("staff")); // 创建表
// createTable("staff", "info");
// System.out.println(tableExist("staff")); // 删除表
// deleteTable("staff"); // 增、改
// putData("student", "1001", "info", "name", "mcq"); // 删
// delete("student", "1001", "info", "name"); // 查——全表扫描
// scanTable("student"); //查——获取指定列族
getData("student", "1001","info","name"); // 关闭资源
close(connection, admin);
}
}

HBase常用的JAVA API操作的更多相关文章

  1. HDFS 05 - HDFS 常用的 Java API 操作

    目录 0 - 配置 Hadoop 环境(Windows系统) 1 - 导入 Maven 依赖 2 - 常用类介绍 3 - 常见 API 操作 3.1 获取文件系统(重要) 3.2 创建目录.写入文件 ...

  2. HBase学习笔记——Java API操作

    1.1.  配置 HBaseConfiguration 包:org.apache.hadoop.hbase.HBaseConfiguration 作用:通过此类可以对HBase进行配置 用法实例: C ...

  3. hadoop2-HBase的Java API操作

    Hbase提供了丰富的Java API,以及线程池操作,下面我用线程池来展示一下使用Java API操作Hbase. 项目结构如下: 我使用的Hbase的版本是 hbase-0.98.9-hadoop ...

  4. 已看1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架、多线程(并发编程)、I/O(NIO)、Socket、JDBC、XML、反射等。[泛型]\

    1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架.多线程(并发编程).I/O(NIO).Socket.JDBC.XML.反射等.[泛型]\1* ...

  5. java api操作

    java api操作 导入开发包 将hbase安装包中lib下包导入java项目   创建表   Configuration conf = HBaseConfiguration.create(); c ...

  6. hive-通过Java API操作

    通过Java API操作hive,算是测试hive第三种对外接口 测试hive 服务启动 package org.admln.hive; import java.sql.SQLException; i ...

  7. 使用Java API操作HDFS文件系统

    使用Junit封装HFDS import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org ...

  8. Kafka系列三 java API操作

    使用java API操作kafka 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...

  9. Hadoop之HDFS(三)HDFS的JAVA API操作

    HDFS的JAVA API操作 HDFS 在生产应用中主要是客户端的开发,其核心步骤是从 HDFS 提供的 api中构造一个 HDFS 的访问客户端对象,然后通过该客户端对象操作(增删改查)HDFS ...

随机推荐

  1. 必学PHP类库/常用PHP类库大全,php 类库分类-收集

    依赖管理( Dependency Management ) 用于依赖管理的包和框架 Composer / Packagist - 一个包和依赖管理器. Composer Installers - 一个 ...

  2. [Linux] 纯净ubuntu快速搭建宝塔面板

    宝塔官方建议是纯净的系统,我使用docker运行一个ubuntu容器,模拟一个纯净的系统,这样也不会影响到我的其他服务. docker run --name baota -id -p 8888:888 ...

  3. 元素无法定位问题 NoSuchElementException: Message: no such element: Unable to locate element 解决方法

    定位网页上某个按钮时,总是报错元素定位不到,具体如下:NoSuchElementException: Message: no such element: Unable to locate elemen ...

  4. python使用face_recognition包的环境设置

    在使用face_recognition包进行人脸识别时,环境是非常重要的,但是网上办法特别纷杂,今天介绍一种特别简单的办法,希望能帮助到大家,少走些坑. 1.首先应该下载dlib安装包(例如:dlib ...

  5. pugixml的简单使用

    一.简介 pugixml的官方主页为:http://pugixml.org/ pugixml是一个很棒的XML操作库, 它很轻量,只有三个文件(pugiconfig.hpp   pugixml.cpp ...

  6. 忘记IBM服务器的登录IP

    问题描述: 一台服务器安装了winserver2003系统,经过漫长的加电启动,能进入到win2003的登录界面,提示ctrl+alt+del登录界面,但是发现键盘失灵了,无法键入ctrl+alt+d ...

  7. luoguP2824 [HEOI2016/TJOI2016]排序(二分答案做法)

    题意 这题的思路实在巧妙. 首先我们肯定无法对区间进行sort,那么考虑如何使得sort简化. 问:如果给的序列是一个0-1序列,让你区间排序,那么怎么做? 答:建一颗线段树维护sum,求出当前区间中 ...

  8. redis数据查看工具

    Redis缓存数据库目前已大量的应用,广泛用于存储session信息,权限信息,交易作业等热数据.但是Redis存在的数据可视化不便.Redis的数据查看维护困难.Redis状态监控运维不易等问题.使 ...

  9. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  10. Unreal Engine 4 系列教程 Part 9:AI教程

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...