HBase简单API
一、使用IDEA的maven工程,工程结构如下:
二、maven的依赖pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.hbasetest</groupId>
<artifactId>HbaseTest</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies> </project>
三、hbase-site.xml,在HBase集群的{HBASE_HOME}/conf目录下下载到本地,放到resources资源目录下
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration>
<!-- 设置namenode所在位置 通过rootdir设置 也就是设置hdfs中存放的路径 -->
<property>
<name>hbase.rootdir</name>
<value>hdfs://hd09-1:9000/hbase</value>
</property> <!-- 是否开启集群 -->
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property> <!-- 0.98 后的新变动,之前版本没有.port,默认端口为 60000 -->
<property>
<name>hbase.master.port</name>
<value>16000</value>
</property> <!-- zookeeper集群的位置 -->
<property>
<name>hbase.zookeeper.quorum</name>
<value>hd09-1:2181,hd09-2:2181,hd09-3:2181</value>
</property> <!-- hbase的元数据信息存储在zookeeper的位置 -->
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/root/hd/zookeeper-3.4.10/zkData</value>
</property>
</configuration>
四、core-site.xml,在Hadoop集群的{HADOOP_HOME}/etc/hadoop目录下下载到本地,放到resources资源目录下
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Put site-specific property overrides in this file. --> <configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://hd09-1:9000</value>
</property>
</configuration>
五、hdfs-site.xml,在Hadoop集群的{HADOOP_HOME}/etc/hadoop目录下下载到本地,放到resources资源目录下
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Put site-specific property overrides in this file. --> <configuration>
<property>
<name>dfs.namenode.name.dir</name>
<value>/root/hd/dfs/name</value>
</property> <property>
<name>dfs.datanode.data.dir</name>
<value>/root/hd/dfs/data</value>
</property> <property>
<name>dfs.namenode.secondary.http-address</name>
<value>hd09-2:50090</value>
</property>
</configuration>
六、修改本地 C:\Windows\System32\drivers\etc\hosts 文件,在文件最下面加上
192.168.146.132 hd09-1
192.168.146.133 hd09-2
192.168.146.134 hd09-3
七、HbaseAPI 类
package com.demo.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 java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class HbaseAPI { //配置信息
public static Configuration conf; //获取配置信息
static {
//alt + enter
conf = HBaseConfiguration.create();
} //1.判断一张表是否存在
public static boolean isExist(String tableName) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); return admin.tableExists(TableName.valueOf(tableName));
} //2.在HBase集群创建表 create 'user','info','info1'
public static void createTable(String tableName,String... columnFamily) throws IOException {
//对表操作需要用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); //1.表如果存在 请输入其他表名
if (isExist(tableName)){
System.out.println("表已经存在,请输入其它表名");
}else{
//2.注意,创建表的话 需要创建一个描述器
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); //3.创建列族
for (String cf : columnFamily) {
htd.addFamily(new HColumnDescriptor(cf));
} //4.创建表
admin.createTable(htd);
System.out.println("表已创建成功!");
}
} //3.删除HBase中的表
public static void deleteTable(String tableName) throws IOException{
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); //1.如果表存在 删除 否则打印不存在
//需要先指定表不可用 再删除
if (isExist(tableName)){
//2.指定不可用
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
}else {
System.out.println("表不存在,请重新输入表名!");
}
} //4.添加数据put 'user','rowKey'
public static void addRow(String tableName, String rowkey, String cf, String column, String value) throws IOException {
//对表操作需要使用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//拿到表对象
Table t = connection.getTable(TableName.valueOf(tableName));
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); //1.用put方式加入数据
Put p = new Put(Bytes.toBytes(rowkey));
//2.加入数据
p.addColumn(Bytes.toBytes(cf),Bytes.toBytes(column),Bytes.toBytes(value));
t.put(p);
} //5.删除表中一行数据
public static void deleteRow(String tableName, String rowkey, String cf) throws IOException {
//对表操作需要用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//拿到表对象
Table t = connection.getTable(TableName.valueOf(tableName)); //1.根据rowkey删除数据
Delete d = new Delete(Bytes.toBytes(rowkey));
//2.删除
t.delete(d);
} //6.删除多行数据
public static void deleteAll(String tableName, String... rowkeys) throws IOException {
//对表操作需要用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//拿到表对象
Table t = connection.getTable(TableName.valueOf(tableName)); //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);
} //7.扫描表数据 scan全表扫描
public static void scanAll(String tableName) throws IOException {
//对表操作需要用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.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c)));
System.out.println("列族为:" + Bytes.toString(CellUtil.cloneFamily(c)));
System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c)));
}
}
} //8.扫描指定的数据
public static void getRow(String tableName, String rowkey) throws IOException {
//对表操作需要用HBaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//拿到表对象
Table t = connection.getTable(TableName.valueOf(tableName)); //1.扫描指定数据需要实例Get
Get g = new Get(Bytes.toBytes(rowkey));
//2.可加过滤条件
g.addFamily(Bytes.toBytes("info")); Result rs = t.get(g);
Cell[] cells = rs.rawCells(); //3.遍历
//遍历具体数据
for (Cell c : cells) {
System.out.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c)));
System.out.println("列族为:" + Bytes.toString(CellUtil.cloneFamily(c)));
System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c)));
}
} public static void main(String[] args) throws IOException {
// System.out.println(isExist("emp11"));
// createTable("zhaosi","henshuai","feichangshuai");
// createTable("zhaosi","info"); // deleteTable("zhaosi");
// createTable("yangmi","info");
// addRow("yangmi","101","info","age","18"); // deleteRow("yangmi","101","info");
// deleteAll("emp","1001","1002r");
// scanAll("yangmi");
getRow("lisi","102");
}
}
HBase简单API的更多相关文章
- Hbase客户端API基础小结笔记(未完)
客户端API:基础 HBase的主要客户端接口是由org.apache.hadoop.hbase.client包中的HTable类提供的,通过这个类,用户可以完成向HBase存储和检索数据,以及删除无 ...
- Phoenix(sql on hbase)简单介绍
Phoenix(sql on hbase)简单介绍 介绍: Phoenix is a SQL skin over HBase delivered as a client-embedded JDBC d ...
- HBase Python API
HBase Python API HBase通过thrift机制可以实现多语言编程,信息通过端口传递,因此Python是个不错的选择 吐槽 博主在Mac上配置HBase,奈何Zoomkeeper一直报 ...
- HBase编程 API入门系列之create(管理端而言)(8)
大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门系列之put(客户端而言)(1) 就知道,在这篇博文里,我是在HBase Shell里创建HBase表的. 这里,我带领大家,学习更高 ...
- hbase java api样例(版本1.3.1,新API)
hbase版本:1.3.1 目的:HBase新API的使用方法. 尝试并验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(客户端缓 ...
- HBase编程 API入门系列之HTable pool(6)
HTable是一个比较重的对此,比如加载配置文件,连接ZK,查询meta表等等,高并发的时候影响系统的性能,因此引入了“池”的概念. 引入“HBase里的连接池”的目的是: 为了更高的,提高程序的并发 ...
- 第四部分 数据搜索之使用HBASE的API实现条件查询
因为数据清洗部分需要用到Mapreduce,所以先解决hbase的问题,可以用命令先在hbase存一下简单的数据进行查询,之后只要替换数据就可以实现了原本功能 在看该部分前,确保Hase API看了, ...
- HBase伪分布式环境下,HBase的API操作,遇到的问题
在hadoop2.5.2伪分布式上,安装了hbase1.0.1.1的伪分布式 利用HBase的API创建个testapi的表时,提示 Exception in thread "main&q ...
- 使用hbase的api创建表时出现的异常
/usr/lib/jvm/java-7-openjdk-amd64/bin/java -Didea.launcher.port=7538 -Didea.launcher.bin.path=/usr/l ...
随机推荐
- 利用eclipse的search功能搜索当前项目的源文件
当你项目的源文件太多,文件组织结构太复杂的的时候,有时候希望google来帮你一把?给个关键字就把相关的搜索结果给出来? eclipse的search功能基本上就可以完成这个任务,文件搜索,甚至JAV ...
- maven配置nexus
setting配置: <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the ...
- EMC、Pure和NetApp推新品,NAS闪存场景在哪里
Hardy 架构师技术联盟 All Flash/SSD存储的趋势势不可挡,未来在NAS服务上也是如此,眼下已经有非常多家初创厂商支持全SSD的NAS存储服务,包含EMC Isilion也推出了全闪存节 ...
- 记一次安装多版本php的四个雷区,你踩着了吗
记一次安装多版本php的四个雷区,你踩着了吗 技术小疯子关注3人评论740人阅读2018-06-29 15:00:30 记一次安装多版本的php的四个雷区,你踩着了吗 需求:公司需要在同一台服 ...
- ci 框架插入时返回插入的id号
$this->db->insert('goods',$data); $gid=$this->db->insert_id('goods'); return $gid;
- python 处理抓取网页乱码
python 处理抓取网页乱码问题一招鲜 相信用python的人一定在抓取网页时,被编码问题弄晕过一阵 前几天写了一个测试网页的小脚本,并查找是否包含指定的信息. 在html = urllib2. ...
- VC++ 设置桌面壁纸
Windows Shell API提供了接口IActiveDesktop来完成墙纸的设置. //IActiveDesktop 接口方法表 (详情参见MSDN) AddDesktopItem AddDe ...
- Python爬虫(五)
源码: import requests from lxml import etree from my_mysql import MysqlConnect mc = MysqlConnect(','ho ...
- PHP的只是结构图
- LAMP环境如何配置多个域名访问
背景: 公司有多个项目想要挂载在一个服务器上,因此需要多个域名来访问不同的网站,其实就是一个阿里云服务器,一个ip对应于多个域名 lamp环境: centos版本:命令查看centos的版本号:rpm ...