环境准备

新建项目后在pom.xml中添加依赖:

<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.</version>
</dependency> <dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>

HBaseAPI

获取Configuration对象

public static Configuration conf;

static{

//使用HBaseConfiguration的单例方法实例化

conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", "192.168.9.102");

conf.set("hbase.zookeeper.property.clientPort", "2181");

}

判断表是否存在

public static boolean isTableExist(String tableName) throws MasterNotRunningException,

ZooKeeperConnectionException, IOException{

//在HBase中管理、访问表需要先创建HBaseAdmin对象

//Connection connection = ConnectionFactory.createConnection(conf);

//HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();

HBaseAdmin admin = new HBaseAdmin(conf);

return admin.tableExists(tableName);

}

创建表

public static void createTable(String tableName, String... columnFamily) throws

MasterNotRunningException, ZooKeeperConnectionException, IOException{

HBaseAdmin admin = new HBaseAdmin(conf);

//判断表是否存在

if(isTableExist(tableName)){

System.out.println("表" + tableName + "已存在");

//System.exit(0);

}else{

//创建表属性对象,表名需要转字节

HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));

//创建多个列族

for(String cf : columnFamily){

descriptor.addFamily(new HColumnDescriptor(cf));

}

//根据对表的配置,创建表

admin.createTable(descriptor);

System.out.println("表" + tableName + "创建成功!");

}

}

删除表

public static void dropTable(String tableName) throws MasterNotRunningException,

ZooKeeperConnectionException, IOException{

HBaseAdmin admin = new HBaseAdmin(conf);

if(isTableExist(tableName)){

admin.disableTable(tableName);

admin.deleteTable(tableName);

System.out.println("表" + tableName + "删除成功!");

}else{

System.out.println("表" + tableName + "不存在!");

}

}

向表中插入数据

public static void addRowData(String tableName, String rowKey, String columnFamily, String

column, String value) throws IOException{

//创建HTable对象

HTable hTable = new HTable(conf, tableName);

//向表中插入数据

Put put = new Put(Bytes.toBytes(rowKey));

//向Put对象中组装数据

put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));

hTable.put(put);

hTable.close();

System.out.println("插入数据成功");

}

删除多行数据

public static void deleteMultiRow(String tableName, String... rows) throws IOException{

HTable hTable = new HTable(conf, tableName);

List<Delete> deleteList = new ArrayList<Delete>();

for(String row : rows){

Delete delete = new Delete(Bytes.toBytes(row));

deleteList.add(delete);

}

hTable.delete(deleteList);

hTable.close();

}

6.2.7 获取所有数据

public static void getAllRows(String tableName) throws IOException{

HTable hTable = new HTable(conf, tableName);

//得到用于扫描region的对象

Scan scan = new Scan();

//使用HTable得到resultcanner实现类的对象

ResultScanner resultScanner = hTable.getScanner(scan);

for(Result result : resultScanner){

Cell[] cells = result.rawCells();

for(Cell cell : cells){

//得到rowkey

System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));

//得到列族

System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));

System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));

System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));

}

}

}

6.2.8 获取某一行数据

public static void getRow(String tableName, String rowKey) throws IOException{

HTable table = new HTable(conf, tableName);

Get get = new Get(Bytes.toBytes(rowKey));

//get.setMaxVersions();显示所有版本

//get.setTimeStamp();显示指定时间戳的版本

Result result = table.get(get);

for(Cell cell : result.rawCells()){

System.out.println("行键:" + Bytes.toString(result.getRow()));

System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));

System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));

System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));

System.out.println("时间戳:" + cell.getTimestamp());

}

}

6.2.9 获取某一行指定“列族:列”的数据

public static void getRowQualifier(String tableName, String rowKey, String family, String

qualifier) throws IOException{

HTable table = new HTable(conf, tableName);

Get get = new Get(Bytes.toBytes(rowKey));

get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));

Result result = table.get(get);

for(Cell cell : result.rawCells()){

System.out.println("行键:" + Bytes.toString(result.getRow()));

System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));

System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));

System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));

}

}

HBaseAPI的更多相关文章

  1. HBase Shell手动移动Region

    在生产环境中很有可能有那么几个Region比较大,但是都运行在同一个Regionserver中. 这个时候就需要手动将region移动到负载低的Regionserver中. 步骤: 1.找到要移动的r ...

  2. 1、Spark 通过api,hfile两种形式获取hbase数据,简单样例

    pom内容: <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-se ...

  3. UDP接收百万级数据的解决方案

    小序 到新公司不久,就接到一个任务:有个发送方,会通过udp发送一些信息,然后服务接收到信息后保存到数据库的一张表A,保存的这些数据在经过一系列处理,处理完成后累积到另一张表B,然后清空处理的表A的数 ...

  4. 记一次Hbase查询速度优化经历

    项目背景: 在这次影像系统中,我们利用大数据平台做的是文件(图片.视频等)批次的增删改查,每个批次都包含多个文件,上传完成以后要添加文件索引(文件信息及批次信息),由于在Hbase存储的过程中,每个文 ...

  5. Hbase java api

    export JAVA_HOME=/home/hadoop/app/jdk1.8.0_144export HADOOP_HOME=/home/hadoop/app/hadoop-2.4.1export ...

  6. HBase的Shell命令和JavaAPI

    HBase的shell操作和JavaAPI的使用: Shell 表操作 创建表 create 'student','info' #表名 列族 插入表 put 'student','1001','inf ...

  7. 大数据入门第十四天——Hbase详解(二)基本概念与命令、javaAPI

    一.hbase数据模型 完整的官方文档的翻译,参考:https://www.cnblogs.com/simple-focus/p/6198329.html 1.rowkey 与nosql数据库们一样, ...

  8. 快速理解 Phoenix : SQL on HBASE

    转自:http://blog.csdn.net/colorant/article/details/8645081 ==是什么 == 目标Scope EasyStandard SQL access on ...

  9. 大数据分析系统Hadoop的13个开源工具

    Hadoop是由Apache基金会开发的一个大数据分布式系统基础架构,最早版本是2003年原Yahoo!DougCutting根据Google发布的学术论文研究而来. 用户可以在不了解分布式底层细节的 ...

随机推荐

  1. java ++前缀

    public class Sample { public static void main(String[] args) { , num2 = ; , num4 = ; ++num1; System. ...

  2. mysql 类型自动化转换问题

    mysql 类型自动化转换问题 背景  有个业务需求,使用到find_in_set函数,简单贴下,如下: SELECT FIND_IN_SET('b','a,b,c,d'); //返回值为2,即第2个 ...

  3. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext之解决办法

    错误产生背景:将之前用Eclipse写的Blog项目迁移到Idea上面.Ecilpse项目一直是没有问题的. 错误原因分析:原因是项目依赖中引入的jpa,另外也与Idea比较智能也有关系 解决办法: ...

  4. mybatis使用associaton进行分步查询

    Employee类 public class Employee { private Integer id; private String lastName; private String email; ...

  5. mvn手动上传jar到本地仓库

    mvn install:install-file -Dfile=G:\elastic-project\workspace\out\artifacts\xxl_job_core_jar\xxl-job- ...

  6. 我的周记10——“知行合一"

    印象中有个名人说过一句名言:与其游手好闲地学习,不如学习游手好闲 来自 玉伯 .  字是真的好看,有风格 现在已经是第十篇周记了,写着写着慢慢偏离了初衷,但庆幸的是坚持下来写.我相信在用心写好每篇周记 ...

  7. 基于 HTML5 换热站可视化应用

    换热站是整个热网系统中最核心的环节,它将一侧蒸汽或高温水通过热交换器换成可以直接进入用户末端的采暖热水.换热站控制系统是集中供热监控系统的核心部分,换热站控制系统既可独立工作,也可以接受调度中心的监督 ...

  8. PyCharm创建Django项目并连接mysql数据库

    0. 安装Django pip install django 1. 新建项目 注:我的Pycharm为社区版,创建项目时,File->New Project- 显示为: ​ 注意勾选内容. 如果 ...

  9. 【05】Jenkins:用户权限管理

    写在前面的话 在一个企业研发部门内部,可能存在多个运维人员,而这些运维人员往往负责不同的项目,但是有可能他们用的又是同一个 Jenkins 的不同用户.那么我们就希望实现一个需求,能够不同的用户登录 ...

  10. yml 字典列表

    观察: --- # 一位职工记录 name: Example Developer job: Developer skill: Elite employed: True foods: - Apple - ...