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.soft863</groupId>
<artifactId>hbasedemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<hadoop.version>3.2.0</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
</project>
log4j.properties
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
创建HbaseApp类
package com.soft863;
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.Collection;
public class HbaseApp {
private static Admin admin;
// connection 用来获取表
private static Connection connection;
public static void main(String[] args) throws IOException {
init();
String[] cols = new String[1];
cols[0] = "cf1";
listTables();
getData("test", "rk1", "cftest", "a");
createTable("demo", cols);
deleteTable("ttt");
listTables();
addColumnFamily("test", "tcf1");
deleteColumnFamily("test", "tcf1");
scanData("test", "rk1", "rk3", "cftest", "a");
insertRow("test", "sdf", "cftest", "col1", "hadoop");
deleteRow("test", "sdf", "cftest", "col1");
}
//初始化连接
private static void init() throws IOException {
//得到Configuration,用于加载需要连接HBase的各项配置
Configuration configuration = HBaseConfiguration.create();
// 指定 zookeeper集群的地址
configuration.set("hbase.zookeeper.quorum", "hadoop100,hadoop101,hadoop102");
//创建一个连接
connection = ConnectionFactory.createConnection(configuration);
//得到 admin,一切和表相关的操作(创建、删除表,修改表 等)都要通过admin
admin = connection.getAdmin();
}
public static void createTable(String tableNameStr, String[] cols) throws IOException {
//定义表名
TableName tableName = TableName.valueOf(tableNameStr);
//判断是否表已经存在
if (admin.tableExists(tableName)) {
System.out.println("talbe is exists!");
} else {
// 创建hTableDescriptor,把 表名 传进去
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
// 遍历每个列族
for (String col : cols) {
//创建HColumnDescriptor,发现需要放进去 列族的名字
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
// 怎么把指定的列族添加到表里呢?通过 addFamily,而需要传 HColumnDescriptor 类型参数
hTableDescriptor.addFamily(hColumnDescriptor);
}
//通过admin 创建表,发现需要 HTableDescriptor
admin.createTable(hTableDescriptor);
}
}
public static void deleteTable(String tbName) throws IOException {
TableName tableName = TableName.valueOf(tbName);
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
}
public static void listTables() throws IOException {
HTableDescriptor[] hTableDescriptors = admin.listTables();
for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
System.out.println("表名:" + hTableDescriptor.getNameAsString());
Collection<HColumnDescriptor> families = hTableDescriptor.getFamilies();
for (HColumnDescriptor columnDescriptor : families) {
System.out.println("列族:" + columnDescriptor.getNameAsString());
}
}
}
public static void addColumnFamily(String tbName, String columnFamily) throws IOException {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFamily);
//设置最大版本数
//hColumnDescriptor.setMaxVersions(5);
admin.addColumn(TableName.valueOf(tbName), hColumnDescriptor);
}
public static void deleteColumnFamily(String tableNameStr, String columnFamily) throws IOException {
//在删除前要先停用表
TableName tableName = TableName.valueOf(tableNameStr);
admin.disableTable(tableName);
admin.deleteColumn(tableName, columnFamily.getBytes());
}
//插入数据
public static void insertRow(String tableName, String rowKey, String colFamily, String col, String val)
throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
table.put(put);
//批量插入
/* List<Put> putList = new ArrayList<Put>();
puts.add(put);
table.put(putList);*/
table.close();
}
//删除数据
public static void deleteRow(String tableName, String rowkey, String colFamily, String col) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
//删除指定列族
//delete.addFamily(Bytes.toBytes(colFamily));
//删除指定列
delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
//如果不指定列族和列,就是删除一行
table.delete(delete);
//批量删除
/* List<Delete> deleteList = new ArrayList<Delete>();
deleteList.add(delete);
table.delete(deleteList);*/
table.close();
}
public static void getData(String tableName, String rowkey, String colFamily, String col) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
TableName tableName1 = TableName.valueOf(tableName);
admin.enableTable(tableName1);
Get get = new Get(Bytes.toBytes(rowkey));
//获取指定列族数据
//get.addFamily(Bytes.toBytes(colFamily));
//获取指定列数据
//get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
Result result = table.get(get);
showCell(result);
table.close();
}
//格式化输出
public static void showCell(Result result) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("Row Name:" + new String(CellUtil.cloneRow(cell)) + " ");
System.out.println("Timetamp:" + cell.getTimestamp() + " ");
System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.println("column Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
}
}
public static void scanData(String tableName, String startRow, String stopRow, String family, String column)
throws IOException {
TableName tableName1 = TableName.valueOf(tableName);
admin.enableTable(tableName1);
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
//指定开始行,结束行
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(stopRow));
//指定列族、列
scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
// //只指定列族
// scan.addFamily(Bytes.toBytes(family));
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
showCell(result);
}
table.close();
}
}