1)安装Intellij idea
这款开发工具收费,要注册
http://idea.lanyus.com/
这里找注册码
安装完成后输入注册码,然后需要在本地hosts文件修改
C:\Windows\System32\drivers\etc\hosts
加上这个
0.0.0.0 account.jetbrains.com
2)Intellij idea新建Maven工程
在pom.xml中添加hbase依赖
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
在src/main/resources文件夹下添加三个配置文件
hdfs-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<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>
<!--备用namenode的节点配置 -->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>bigdata122:50070</value>
</property>
<!-- block副本的数量配置-->
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
<!-- block大小配置-->
<property>
<name>dfs.blocksize</name>
<value>134217728</value>
</property>
</configuration>
core-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://bigdata121:9000</value>
</property>
</configuration>
hbase-site.xml
<configuration>
<!-- 设置namenode所在位置 通过rootdir设置 也就是设置hdfs中存放的路径 -->
<property>
<name>hbase.rootdir</name>
<value>hdfs://bigdata121: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>bigdata121:2181,bigdata122:2181,bigdata123:2181,bigdata124:2181,bigdata125:2181</value>
</property>
<!-- hbase的元数据信息存储在zookeeper的位置 -->
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/root/hd/zookeeper-3.4.10/zkData</value>
</property>
</configuration>
3)在C:\Windows\System32\drivers\etc\hosts文件中加上hdfs集群的ip
192.168.252.121 bigdata121
192.168.252.122 bigdata122
192.168.252.123 bigdata123
192.168.252.124 bigdata124
192.168.252.125 bigdata125
这样idea才能找到集群
Hbase擅长存储数据,并不擅长计算分析数据
可以结合其他组件(mapreduce/spark)
Hbase官方提供了hbase-api
Hbase-MR操作
官方Hbase-Mapreduce
基本api命名操作如下:
package com.itstaredu.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 HbaseTest {
//获取配置信息
public static Configuration conf;
static{
conf = HBaseConfiguration.create();
}
//1.判断一张表是否存在
public static boolean isExist(String tableName){
//对表操作需要使用HbaseAdmin
try {
Connection connection = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
//2.在hbase创建表
public static void createTable(String tableName,String... columnfamily){
try {
//对表操作需要使用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("表已经创建成功!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
//3.删除hbase的表
public static void deleteTable(String tableName) {
try {
//对表操作需要使用HbaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
//1.表如果存在,请输入其他表名
if (!isExist(tableName)) {
System.out.println("表不存在");
} else {
//2.如果表存在,删除
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("表删除了");
}
} catch (IOException e) {
e.printStackTrace();
}
}
//4.添加数据put 'user','rowkey','info:name','tony'
public static void addRow(String tableName,String rowkey,String cf,String column,String value){
try {
//对表操作需要使用HbaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
Table t = connection.getTable(TableName.valueOf(tableName));
//1.表如果存在,请输入其他表名
if (!isExist(tableName)) {
System.out.println("表不存在");
} else {
//2.用put方式加入数据
Put p = new Put(Bytes.toBytes(rowkey));
//3.加入数据
p.addColumn(Bytes.toBytes(cf),Bytes.toBytes(column),Bytes.toBytes(value));
t.put(p);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//5.删除表中一行数据
public static void deleteRow(String tableName,String rowkey,String cf ){
try {
//对表操作需要使用HbaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
Table t = connection.getTable(TableName.valueOf(tableName));
//1.表如果存在,请输入其他表名
if (!isExist(tableName)) {
System.out.println("表不存在");
} else {
//1.根据rowkey删除数据
Delete delete = new Delete(Bytes.toBytes(rowkey));
//2.删除
t.delete(delete);
System.out.println("删除成功");
}
} catch (IOException e) {
e.printStackTrace();
}
}
//6.删除多行数据
public static void deleteAll(String tableName,String... rowkeys){
try {
//对表操作需要使用HbaseAdmin
Connection connection = ConnectionFactory.createConnection(conf);
Table t = connection.getTable(TableName.valueOf(tableName));
//1.表如果存在,请输入其他表名
if (!isExist(tableName)) {
System.out.println("表不存在");
} else {
//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);
System.out.println("删除成功");
}
} catch (IOException e) {
e.printStackTrace();
}
}
//7.扫描表数据 scan全表扫描
public static void scanAll(String tableName){
try {
//对表操作需要使用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.print("行键为:"+Bytes.toString(CellUtil.cloneRow(c))+" ");
System.out.print("列族为:"+Bytes.toString(CellUtil.cloneFamily(c))+" ");
System.out.print("列名为:"+Bytes.toString(CellUtil.cloneQualifier(c))+" ");
System.out.println("值为:"+Bytes.toString(CellUtil.cloneValue(c)));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void getRow(String tableName,String rowkey) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
//拿到表对象
Table t = connection.getTable(TableName.valueOf(tableName));
//1.扫描指定数据需要实例对象Get
Get get = new Get(Bytes.toBytes(rowkey));
//2.可加过滤条件
get.addFamily(Bytes.toBytes("info1"));
Result rs = t.get(get);
//3.遍历
Cell[] cells = rs.rawCells();
for (Cell c : cells){
System.out.print("行键为:"+Bytes.toString(CellUtil.cloneRow(c))+" ");
System.out.print("列族为:"+Bytes.toString(CellUtil.cloneFamily(c))+" ");
System.out.print("列名:"+Bytes.toString(CellUtil.cloneQualifier(c))+" ");
System.out.println("值为:"+Bytes.toString(CellUtil.cloneRow(c))+" ");
}
}
public static void main(String[] args) throws IOException {
// System.out.println(isExist("emp"));
//createTable("yangmi","info");
//deleteTable("tony");
//addRow("yangmi","101","info","age","20");
//deleteRow("yangmi","101","info");
//deleteAll("emp","1001","1002");
//scanAll("yangmi");
getRow("yangmi","1001");
}
}