一、使用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的更多相关文章

  1. Hbase客户端API基础小结笔记(未完)

    客户端API:基础 HBase的主要客户端接口是由org.apache.hadoop.hbase.client包中的HTable类提供的,通过这个类,用户可以完成向HBase存储和检索数据,以及删除无 ...

  2. Phoenix(sql on hbase)简单介绍

    Phoenix(sql on hbase)简单介绍 介绍: Phoenix is a SQL skin over HBase delivered as a client-embedded JDBC d ...

  3. HBase Python API

    HBase Python API HBase通过thrift机制可以实现多语言编程,信息通过端口传递,因此Python是个不错的选择 吐槽 博主在Mac上配置HBase,奈何Zoomkeeper一直报 ...

  4. HBase编程 API入门系列之create(管理端而言)(8)

    大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门系列之put(客户端而言)(1) 就知道,在这篇博文里,我是在HBase Shell里创建HBase表的. 这里,我带领大家,学习更高 ...

  5. hbase java api样例(版本1.3.1,新API)

    hbase版本:1.3.1 目的:HBase新API的使用方法. 尝试并验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(客户端缓 ...

  6. HBase编程 API入门系列之HTable pool(6)

    HTable是一个比较重的对此,比如加载配置文件,连接ZK,查询meta表等等,高并发的时候影响系统的性能,因此引入了“池”的概念. 引入“HBase里的连接池”的目的是: 为了更高的,提高程序的并发 ...

  7. 第四部分 数据搜索之使用HBASE的API实现条件查询

    因为数据清洗部分需要用到Mapreduce,所以先解决hbase的问题,可以用命令先在hbase存一下简单的数据进行查询,之后只要替换数据就可以实现了原本功能 在看该部分前,确保Hase API看了, ...

  8. HBase伪分布式环境下,HBase的API操作,遇到的问题

    在hadoop2.5.2伪分布式上,安装了hbase1.0.1.1的伪分布式 利用HBase的API创建个testapi的表时,提示  Exception in thread "main&q ...

  9. 使用hbase的api创建表时出现的异常

    /usr/lib/jvm/java-7-openjdk-amd64/bin/java -Didea.launcher.port=7538 -Didea.launcher.bin.path=/usr/l ...

随机推荐

  1. pyqt加载图片

    使用QPixmap可以加载图片,但是图片只能是标准二进制文件格式: bmp,gif,ico,jpeg,jpg,mng,pbm,pgm,png,ppm,svg,svgz,tga,tif,tiff,xbm ...

  2. CSS学习笔记(4)--选择器(w3school)

    CSS3 选择器 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. "CSS" 列指示该属性是在哪个 CSS 版本中定义的.(CSS1.CSS2 还是 CSS3.) ...

  3. Extjs,实现树形结构的总结

    工作总结,用extjs.mybatis.springMVC实现树形显示班级 前台extjs实现树形代码如下: xtype : 'combotree', fieldLabel : '部门名称', nam ...

  4. BlueZ--内核层+应用层

    BlueZ 1.Kernel层实现: bluetooth协议栈有多层结构,最底层的硬件协议在硬件中就已经实现了.软件级别的协议实现,从HCI这一层开始实现. BlueZ对各层协议的实现是依托于Sock ...

  5. HeadFisrt 设计模式03 装饰者

    类应该对扩展开放, 对修改关闭. 所谓装饰者模式, 是指用其他的类来装饰某个类, 装饰者说白了就是使用 has-a 来代替 is-a 隐喻 咖啡店, 有很多种咖啡, 咖啡里还要增加一些 milk, 面 ...

  6. python的zipfile实现文件目录解压缩

    主要是 解决了压缩目录下 空文件夹 的压缩 和 解压缩问题 压缩文件夹的函数: # coding:utf- import os import zipfile def zipdir(dirToZip,s ...

  7. 核函数(kernel function)

    百度百科的解释: 常用核函数: 1.线性核(Linear Kernel): 2.多项式核(Polynomial Kernel): 3.径向基核函数(Radial Basis Function),也叫高 ...

  8. Servlet中Request的getAttribute getParameter 区别

    1.从更深的层次考虑,request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据. request.getParameter()方法返回Str ...

  9. 27Mybatis_一级缓存的实际应用场景

    正式开发,是将mybatis和spring进行整合开发,事务控制在service中. 一个service方法中包括 很多mapper方法调用. service{ //开始执行时,开启事务,创建SqlS ...

  10. 【BZOJ】1617: [Usaco2008 Mar]River Crossing渡河问题(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1617 裸dp,很好做. 设f[i]表示i头牛到对岸所需最小时间.sum[i]表示运i头牛到对岸的时间 ...