在此之前要配置好三节点的hadoop集群,zookeeper集群,并启动它们,然后再配置好HBase环境

本文只是HBase2.3.5API操作作相应说明,如果前面环境还没有配置好,可以翻看我之前的博客,欢迎留言交流

节点hadoop01

节点hadoop02

节点hadoop03

1 maven依赖

<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.3.5</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.3.5</version>
</dependency> <dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>C:/Program Files/Java/jdk1.8.0_261/lib/tools.jar</systemPath>
</dependency>

2 API操作

package com.hbase;

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.stringtemplate.v4.ST;
import scala.util.control.Exception; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class ConfigulationHBase { private static Configuration configuration; private static Connection connection; private static Admin admin; static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.161.141");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 判斷表是否存在
* <br>存在則返回true
* @param tableName
* @return
* @throws IOException
*/
public static boolean isTableExist(String tableName) throws IOException {
boolean b = admin.tableExists(TableName.valueOf(tableName));
return b;
} /**
* 創建表
*
* 参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。<br>
* 要求当HBase已经存在名为tableName的表时,先删除原有的表,然后再<br>
* 创建新的表 field:列族<br>
* @param tableName 表名
* @param fields 列族名
* @throws IOException
*/
public static void createTable(String tableName,String[] fields) throws IOException {
if(isTableExist(tableName)){
System.out.println(tableName + " table is alreadly exist...");
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println(tableName + " table is deleted...");
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for(String str:fields){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println(tableName + " table is created!");
admin.close();
} /**
* 添加数据
*
* 向表tableName,行键rowKey和fields字段指定的单元格中添加对应的值values<br>
* 例如:表名:student,行键:1001,添加的字段:info:name,添加的值:Janna<br>
* put 'student','1001','info:name','Janna'<br>
*
* @param tableName 表名
* @param rowKey 行键
* @param family 列族
* @param qualifier 列族值
* @param value 值
* @throws IOException
*/
public static void addRow(String tableName, String rowKey, String family, String qualifier, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
byte[] rowKeyAsBytes = rowKey.getBytes();
Put put = new Put(rowKeyAsBytes);
put.addColumn(family.getBytes(), qualifier.getBytes(), value.getBytes());
table.put(put);
table.close();
admin.close();
} /**
* 删除一行或者多行数据
* @param tableName
* @param rows
* @throws IOException
*/
public static void deleteMultiRow(String tableName, String... rows) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
List<Delete> deleteList = new ArrayList<Delete>();
for (int i = 0; i < rows.length; i++) {
Delete delete = new Delete(rows[i].getBytes());
deleteList.add(delete);
}
table.delete(deleteList);
table.close();
admin.close();
} /**
*
* 查询某tableName所有的数据
* @param tableName 表名
* @throws IOException
*/
public static void getAllRows(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result:scanner){
Cell[] cells = result.rawCells();
for (Cell cell:cells){
System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("--------------------------------------");
}
System.out.println("=======================================");
}
table.close();
} /**
* 查询tableName表的rowKey行键的数据
* get 'student','1001'
* @param tableName 表名
* @param rowKey 行键
*/
public static void getRow(String tableName, String rowKey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
Result result = table.get(get);
for (Cell cell:result.rawCells()){
System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("--------------------------------------");
}
} /**
* 获取某表某行键某列族的值
*
* @param tableName 表名
* @param rowKey 行键
* @param famliy 列族
* @param qualifier 列族值
* @throws IOException
*/
public static void getRowQualifier(String tableName, String rowKey, String famliy, String qualifier) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(famliy.getBytes(), qualifier.getBytes());
Result result = table.get(get);
for (Cell cell:result.rawCells()){
System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("timestamp:" + cell.getTimestamp());
System.out.println("--------------------------------------");
}
} /**
* main 程序入口
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// 創建表
// String[] fileds = {"base_info","other_info"};
// createTable("football", fileds);
// 添加数据
// addRow("football", "11002", "base_info", "name", "lin");
// addRow("football", "11002", "base_info", "sex", "female");
// addRow("football", "11003", "base_info", "sex", "male");
// 删除数据
// deleteMultiRow("football", new String[]{"11002"});
// 得到所有的数据
// getAllRows("student");
// 查询某表某行键数据
// getRow("student","1001");
// 查询某表某行键某列族数据
getRowQualifier("student", "1001", "info", "name"); }
}

3 部分结果截图

仅供参考,有错误还请指出!

有什么想法,评论区留言,互相指教指教。

 

IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI的更多相关文章

  1. ZooKeeper-3.4.10分布式安装指南

    目录 目录 1 1. 前言 1 2. 约定 1 3. 安装步骤 2 3.1. 配置/etc/hosts 2 3.2. 设置myid 2 3.3. 修改conf/zoo.cfg 2 3.4. 修改/bi ...

  2. Ubuntu16.4 zookeeper-3.4.10 单机多实例部署

    上传 zookeeper-3.4.10.tar.gz 到服务器 root@temple-:/usr/local/temple/jar# ll total drwxr-xr-x root root 8月 ...

  3. linux安装配置zookeeper-3.4.10

    此文是基于上一篇文章:hadoop集群搭建 安装zookeeper: [在各个slave节点安装zookeeper] 下载地址:http://mirror.bit.edu.cn/apache/zook ...

  4. Linux上安装ZooKeeper并设置开机启动(CentOS7+ZooKeeper3.4.10)

    1下载Zookeeper 2安装启动测试 2.1上载压缩文件并解压 2.2新建 zookeeper配置文件 2.3安装JDK 2.4启动zookeeper 2.5查看zookeeper的状态 3将Zo ...

  5. Linux CentOS7下安装Zookeeper-3.4.10服务(最新)

    Linux CentOS7下安装Zookeeper-3.4.10服务(最新) 2017年10月27日 01:25:26 极速-蜗牛 阅读数:1933   版权声明:本文为博主原创文章,未经博主允许不得 ...

  6. shell脚本部署zookeeper-3.4.10 [含注释]

    文章目录 zk_install.sh conf/config conf/zoo_template.cfg package zk_install.sh #!/bin/bash base_path=$(c ...

  7. Hadoop-2.6.0 + Zookeeper-3.4.6 + HBase-0.98.9-hadoop2环境搭建示例

    1    基本信息 1.1     软件信息 hadoop-2.6.0 zookeeper-3.4.6 hbase-0.98.9-hadoop2 (以下示例中使用的操作系统是Centos 6.5,请将 ...

  8. ubuntu12.04+hadoop2.2.0+zookeeper3.4.5+hbase0.96.2+hive0.13.1伪分布式环境部署

    目录: 一.hadoop2.2.0.zookeeper3.4.5.hbase0.96.2.hive0.13.1都是什么? 二.这些软件在哪里下载? 三.如何安装 1.安装JDK 2.用parallel ...

  9. SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10.50.1600

    http://blog.csdn.net/feng19821209/article/details/8571571 SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10. ...

随机推荐

  1. SSM框架的配置整合(包含配置文件代码)

    由于SSM框架学习都要去网上或者以前的项目拷贝相同的代码,所以我在此把自己用到的配置文件全放在这里,帮助自己,帮助别人 首先开始前导入依赖和处理静态资源导出问题 <dependencies> ...

  2. CAS(Compare and Swap)无锁算法-学习笔记

    非阻塞同步算法与CAS(Compare and Swap)无锁算法 这篇问题对java的CAS讲的非常透彻! 锁的代价 1. 内核态的锁的时候需要操作系统进行一次上下文切换,加锁.释放锁会导致比较多的 ...

  3. django2中namespace和name的使用

    django2中namespace和name的使用   一.在Django <= 1.11 我们通过关键词namespace参数定义名称空间 1.projects/urls.py   from ...

  4. 视觉SLAM技术应用

    视觉SLAM技术应用 SLAM技术背景 SLAM技术全称Simultaneous localization and mapping,中文为"同时定位与地图构建".SLAM可以在未知 ...

  5. 将TVM集成到PyTorch上

    将TVM集成到PyTorch上 随着TVM不断展示出对深度学习执行效率的改进,很明显PyTorch将从直接利用编译器堆栈中受益.PyTorch的主要宗旨是提供无缝且强大的集成,而这不会妨碍用户.为此, ...

  6. ONNX 实时graph优化方法

    ONNX 实时graph优化方法 ONNX实时提供了各种图形优化来提高模型性能.图优化本质上是图级别的转换,从小型图简化和节点消除,到更复杂的节点融合和布局优化. 图形优化根据其复杂性和功能分为几个类 ...

  7. Kubeedge Edged概述

    Kubeedge Edged概述 Overview EdgeD是管理节点生命周期的边缘节点模块.它可以帮助用户在边缘节点上部署容器化的工作负载或应用程序.这些工作负载可以执行任何操作,从简单的远程遥测 ...

  8. InfluxDB总结

    一.简介 InfluxDB(时序数据库)influxdb是一个开源分布式时序.时间和指标数据库,使用 Go 语言编写,无需外部依赖.其设计目标是实现分布式和水平伸缩扩展,是 InfluxData 的核 ...

  9. 01:osi七层---基于TCP协议的套接字(socket)

    1 : osi 七层,tcp/ip 五层 1 cs架构和bs架构2 互联网3 osi七层 tcp/ip五层 -物理层   -网线.光纤        -数据链路层       -网卡        - ...

  10. noip模拟7[匹配·回家·寿司]

    这次考试状态好像还是没有回来,只拿了55pts,还全是第一题的功劳,就是一个小KMP,然后还让我给打错了 就很难受,while打成了if,然后wa掉45分考完立马拿回来了,悔死了,害 第二题爆零了,为 ...