1.idea创建项目
2.启动hadoop、zookeeper、hbase
[cevent@hadoop207 hadoop-2.7.2]$
sbin/start-dfs.sh
[cevent@hadoop207 hadoop-2.7.2]$
sbin/start-yarn.sh
[cevent@hadoop207 zookeeper-3.4.10]$
bin/zkServer.sh start
[cevent@hadoop208 zookeeper-3.4.10]$
bin/zkServer.sh start
[cevent@hadoop209 zookeeper-3.4.10]$
bin/zkServer.sh start
[cevent@hadoop207 hbase-1.3.1]$
bin/start-hbase.sh
[cevent@hadoop207 hbase-1.3.1]$ jps
3423 DataNode
4767 Jps
4695 HRegionServer
3921 NodeManager
3803 ResourceManager
4340 QuorumPeerMain
4547 HMaster
3306 NameNode
3579 SecondaryNameNode
3.HBaseClient
package com.cevent.hbase;/**
* Created by Cevent on 2020/6/21.
*/
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.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
/**
* @author cevent
* @description
* @date 2020/6/21 10:53
*/
public class HBaseClient {
private Connection connection;
/**
* 操作前执行
* @throws IOException
*/
@Before
public void before() throws IOException{
//1.获取配置hbase-configuration
Configuration configuration= HBaseConfiguration.create();
//2.配置zookeeper
configuration.set("hbase.zookeeper.quorum","hadoop207.cevent.com");
configuration.set("hbase.zookeeper.property.clientPort","2181");
//3.获取connection
connection= ConnectionFactory.createConnection(configuration);
}
/**
* 操作之后关闭
*/
@After
public void after() throws IOException {
connection.close();
}
/**
* 1.建表
*/
@Test
public void createHBaseTable() throws IOException {
//4.获取HMaster
Admin admin=connection.getAdmin();
//5.建表名-表描述器
TableName tableName=TableName.valueOf("c_dev_info");
//6.建表,判断,放入master
if(!admin.tableExists(tableName)){
HTableDescriptor hTableDescriptor=new HTableDescriptor(tableName);
//6.建表列族-列族描述器
HColumnDescriptor hColumnDescriptor=new HColumnDescriptor("info");
//7.添加表列族
hTableDescriptor.addFamily(hColumnDescriptor);
admin.createTable(hTableDescriptor);
}
//关闭流
admin.close();
}
/**
* 2.删除表
*/
@Test
public void deleteHBaseTable() throws IOException {
//1.获取Master
Admin admin=connection.getAdmin();
TableName tableName=TableName.valueOf("c_dev_info");
//2.判断存在,删除
if(admin.tableExists(tableName)){
//删除之前需先关闭使用table
admin.disableTable(tableName);
admin.deleteTable(tableName);
}else{
System.out.println("表不存在!");
}
admin.close();
}
//插入数据调用方法
public void addData(Table table,String rowKey,String columnFamily,String columnQualifier,String value) throws IOException{
Put put=new Put(rowKey.getBytes());
put.add(
new KeyValue(
rowKey.getBytes(),
columnFamily.getBytes(),
columnQualifier.getBytes(),
value.getBytes()
)
);
table.put(put);
}
//调用插入
@Test
public void addDataCell() throws IOException{
Table c_dev_info=connection.getTable(TableName.valueOf("c_dev_info"));
addData(c_dev_info,"2023","info","name","kakaxi");
addData(c_dev_info,"2024","info","name","鲁峰");
addData(c_dev_info,"2025","info","name","鬼吹灯");
addData(c_dev_info,"2023","info","age","18");
addData(c_dev_info,"2024","info","age","58");
addData(c_dev_info,"2025","info","age","998");
c_dev_info.close();
}
/**
* 3.插入数据
*/
@Test
public void addCell() throws IOException {
//1.获取表
Table c_dev_info=connection.getTable(TableName.valueOf("c_dev_info"));
//2.建立插入对象:描述插入动作的对象,可添加时间戳(插入数据时,预设时间戳)("2021".getBytes(),100)
Put put=new Put("2022".getBytes());
put.add(
new KeyValue(
"2022".getBytes(), //rowkey
"info".getBytes(), //列族
"name".getBytes(), //列限定名
"kafka".getBytes() //列值
)
);
put.add(
new KeyValue(
"2021".getBytes(), //rowkey
"info".getBytes(), //列族
"effects".getBytes(), //列限定名
"zookeeper".getBytes() //列值
)
);
c_dev_info.put(put);
c_dev_info.close();
}
/**
* 删除数据:HBase按行存储,按行删除
*/
@Test
public void deleteCell() throws IOException {
Table c_dev_info=connection.getTable(TableName.valueOf("c_dev_info"));
//描述删除,不加入addColumns,直接删除info列族全部数据
Delete delCell=new Delete("2021".getBytes());
//指定删除行,指定列族和限定名
delCell.addColumns("info".getBytes(),"gender".getBytes());
c_dev_info.delete(delCell);
c_dev_info.close();
}
/**
* 查询数据
*/
@Test
public void getCell() throws IOException {
Table c_dev_info=connection.getTable(TableName.valueOf("c_dev_info"));
Get getCell=new Get("2021".getBytes());
//指定列族
getCell.addColumn("info".getBytes(),"name".getBytes());
//获取全部信息
Result result=c_dev_info.get(getCell);
//打印cell数组
Cell [] cells= result.rawCells();
System.out.println("c_dev_info-表头:rowKey \t columnFamily \t columnQualifier \t value");
for (Cell cell:cells){
String rowKey= Bytes.toString(CellUtil.cloneRow(cell)) ;
String columnFamily=Bytes.toString(CellUtil.cloneFamily(cell));
String columnQualifier=Bytes.toString(CellUtil.cloneQualifier(cell));//限定符
String value=Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("c_dev_info-内容: "+rowKey+"\t"+columnFamily+"\t"+columnQualifier+"\t"+value);
}
c_dev_info.close();
}
/**
* 查数据
*/
@Test
public void scanCell() throws IOException {
Table c_dev_info=connection.getTable(TableName.valueOf("c_dev_info"));
//描述scan的动作对象
//Scan scanCell=new Scan();
Scan scanCell=new Scan("2021".getBytes(),"2025".getBytes());
//获取scan对象
ResultScanner resultScanner=c_dev_info.getScanner(scanCell);
//遍历对象集合
for (Result result:resultScanner){
System.out.println("c_dev_info-scan-表头:rowKey \t columnFamily \t columnQualifier \t value");
for(Cell cell:result.rawCells()){
String rowKey= Bytes.toString(CellUtil.cloneRow(cell)) ;
String columnFamily=Bytes.toString(CellUtil.cloneFamily(cell));
String columnQualifier=Bytes.toString(CellUtil.cloneQualifier(cell));
String value=Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("c_dev_info-scan-内容: "+rowKey+"\t"+columnFamily+"\t"+columnQualifier+"\t"+value);
}
}
}
}
4.实现
5.shell查询
[cevent@hadoop207 hbase-1.3.1]$ hbase shell
hbase(main):004:0> list
TABLE
c_dev_info
c_student
2 row(s) in 0.0120 seconds
=> ["c_dev_info",
"c_student"]
hbase(main):008:0> scan "c_dev_info"
ROW COLUMN+CELL
2021 column=info:name,
timestamp=1592712559944, value=cevent
2023 column=info:age,
timestamp=1592718701740, value=18
2023 column=info:name,
timestamp=1592718701721, value=kakaxi
2024 column=info:age,
timestamp=1592718701747, value=58
2024 column=info:name,
timestamp=1592718701732, value=\xE9\xB2\x81\xE
5\xB3\xB0
2025 column=info:age,
timestamp=1592718701751, value=998
2025 column=info:name,
timestamp=1592718701736, value=\xE9\xAC\xBC\xE
5\x90\xB9\xE7\x81\xAF
4 row(s) in 0.0290 seconds