启动hbase (web 页面 hdp1:16010)
start-hbase
./hbase shell 客户端
package Test01;
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.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
/**
* @author jiasongfan
* @date 2022/7/4
* @apiNote
*/
public class TestHbase {
Connection conn =null;
@Before
public void info() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hdp1,hdp2,hdp3");
conn= ConnectionFactory.createConnection(conf);
}
//创建表空间
@Test
public void createNameSpace() throws IOException {
Admin admin = conn.getAdmin();
NamespaceDescriptor ns2 = NamespaceDescriptor.create("ns2").build();
admin.createNamespace(ns2);
admin.close();
}
//创建表
@Test
public void createTable() throws IOException {
Admin admin = conn.getAdmin();
HTableDescriptor table = new HTableDescriptor(TableName.valueOf("ns2:stu"));
HColumnDescriptor info = new HColumnDescriptor(Bytes.toBytes("info"));
table.addFamily(info);
admin.createTable(table);
admin.close();
}
//添加内容
@Test
public void putData() throws IOException {
Table table = conn.getTable(TableName.valueOf("ns2:stu"));
Put put = new Put(Bytes.toBytes("1001"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
table.put(put);
table.close();
}
//获取内容
@Test
public void getData() throws IOException {
Table table = conn.getTable(TableName.valueOf("ns2:stu"));
Get get = new Get(Bytes.toBytes("1001"));
Result result = table.get(get);
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] r = CellUtil.cloneRow(cell);
byte[] f = CellUtil.cloneFamily(cell);
byte[] q = CellUtil.cloneQualifier(cell);
byte[] v = CellUtil.cloneValue(cell);
System.out.println(Bytes.toString(r)+","+Bytes.toString(f)+","+Bytes.toString(q)+","+Bytes.toString(v));
}
table.close();
}
//扫描全表
@Test
public void scanTable() throws IOException {
Table table = conn.getTable(TableName.valueOf("ns2:stu"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
Result result = scanner.next();
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] r = CellUtil.cloneRow(cell);
byte[] f = CellUtil.cloneFamily(cell);
byte[] q = CellUtil.cloneQualifier(cell);
byte[] v = CellUtil.cloneValue(cell);
System.out.println(Bytes.toString(r)+","+Bytes.toString(f)+","+Bytes.toString(q)+","+Bytes.toString(v));
}
table.close();
}
//删除数据
@Test
public void deleteData() throws IOException {
Table table = conn.getTable(TableName.valueOf("ns2:stu"));
Delete delete = new Delete(Bytes.toBytes("1001"));
table.delete(delete);
table.close();
}
//删除表
@Test
public void deleteTable() throws IOException {
Admin admin = conn.getAdmin();
admin.disableTable(TableName.valueOf("ns2:stu"));
admin.deleteTable(TableName.valueOf("ns2:stu"));
admin.close();
}
//删除表空间
@Test
public void deleteNamespace() throws IOException {
Admin admin = conn.getAdmin();
admin.deleteNamespace("ns2");
admin.close();
}
}
过滤器
@Test
public void filter1() throws IOException {
Scan scan = new Scan();
SingleColumnValueFilter f1 = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("score"), CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(60)));
f1.setFilterIfMissing(true);
scan.setFilter(f1);
printscan(scan);
}
输出
public void printscan(Scan scan ) throws IOException {
Table t_student = conn.getTable(TableName.valueOf("t_student"));
ResultScanner scanner = t_student.getScanner(scan);
Result result = scanner.next();
while (result!=null){
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] r = CellUtil.cloneRow(cell);
byte[] f = CellUtil.cloneFamily(cell);
byte[] q = CellUtil.cloneQualifier(cell);
byte[] v = CellUtil.cloneValue(cell);
if(Bytes.toString(q).equals("name")){
System.out.println(Bytes.toString(r)+","+Bytes.toString(f)+","+Bytes.toString(q)+","+Bytes.toString(v));
}else {
System.out.println(Bytes.toString(r)+","+Bytes.toString(f)+","+Bytes.toString(q)+","+Bytes.toInt(v));
}
}
result = scanner.next();
}
t_student.close();
}
过滤器链
new filterlist()
sink到hbase
class MySinkHbase1 extends RichSinkFunction[wData]{
var conn: Connection = null
var table: Table =null
override def invoke(value: wData, context: SinkFunction.Context): Unit = {
val put = new Put(Bytes.toBytes(value.key))
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("start"),Bytes.toBytes(value.start+""))
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("end"),Bytes.toBytes(value.end+""))
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("key"),Bytes.toBytes(value.key))
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("avg"),Bytes.toBytes(value.avg+""))
table.put(put)
}
override def open(parameters: Configuration): Unit = {
val conf= HBaseConfiguration.create()
conf.set("hbase.zookeeper.quorum", "hdp1,hdp2,hdp3")
conn = ConnectionFactory.createConnection(conf)
table=conn.getTable(TableName.valueOf("stu"))
}
override def close(): Unit = {
conn.close()
table.close()
}
}
sql到hbase
package Test01
/**
* @author jiasongfan
* @date 2022/7/4
* @apiNote
*/
import org.apache.flink.table.api.{EnvironmentSettings, TableEnvironment}
object TableSinkHbase {
def main(args: Array[String]): Unit = {
val settings = EnvironmentSettings
.newInstance()
.inStreamingMode()
//.inBatchMode()
.build()
val tEnv = TableEnvironment.create(settings)
tEnv.executeSql(
"""
|CREATE TABLE t_stu (
| rowkey String,
| info ROW<name String>
|) WITH (
| 'connector' = 'hbase-1.4',
| 'table-name' = 'ns2:stu',
| 'zookeeper.quorum' = 'hdp1:2181,hdp2:2181,hdp3:2181'
|)
|
|""".stripMargin)
//数据从hbase 来
// tEnv.executeSql(
// """
// |select rowkey,info.name from t_stu
// |""".stripMargin).print()
//写入到hbase去
//从文件中来
tEnv.executeSql(
"""
|CREATE TABLE t_stu1 (
| sid String,
| name STRING
|) WITH (
| 'connector' = 'filesystem', -- required: specify the connector
| 'path' = 'file:///D:\E\month9class\day7-3\data\a.txt', -- required: path to a directory
| 'format' = 'csv'
|)
|""".stripMargin)
// 写入到hbase
// tEnv.executeSql(
// """
// |select * from t_stu1
// |""".stripMargin).print()
tEnv.executeSql(
"""
|CREATE TABLE t_stu2 (
| rowkey String,
info ROW<name String>
|) WITH (
| 'connector' = 'hbase-1.4',
| 'table-name' = 'ns2:stu',
| 'zookeeper.quorum' = 'hdp1:2181,hdp2:2181,hdp3:2181'
|)
|""".stripMargin)
tEnv.executeSql(
"""
|insert into t_stu2
|select sid,row(name) from t_stu1
|""".stripMargin)
}
}