一、依赖:
<?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>org.example</groupId>
<artifactId>pulsar-demo2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<pulsar.version>2.8.0</pulsar.version>
<jackson.version>2.10.5</jackson.version>
<!--<jackson.version>2.6.7</jackson.version>-->
</properties>
<dependencies>
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client-all</artifactId>
<version>${pulsar.version}</version>
</dependency>
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client-kafka</artifactId>
<version>${pulsar.version}</version>
</dependency>
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-spark</artifactId>
<version>${pulsar.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>2.4.0</version>
</dependency>-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.12</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.12</artifactId>
<version>1.13.6</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner-blink_2.12</artifactId>
<version>1.13.6</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-api-java</artifactId>
<version>1.13.6</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-csv</artifactId>
<version>1.13.6</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients_2.12</artifactId>
<version>1.13.6</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-hbase-1.4_2.12</artifactId>
<version>1.13.6</version>
</dependency>
</dependencies>
</project>
二、HBase中建表:
create 'hbasetable','family1','family2','family3','family4'
三、在一台服务器上开启nc
nc -lk 9999
四、运行,demo程序
package cn.edu.tju;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Csv;
import org.apache.flink.table.descriptors.FileSystem;
import org.apache.flink.table.descriptors.Schema;
import java.util.UUID;
public class FlinkHBase3 {
//nc 服务器地址
private static String HOST_NAME = "xx.xx.xx.xx";
private static int PORT = 9999;
private static String DELIMITER ="\n";
public static void main(String[] args) throws Exception{
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
DataStream<String> socketDataInfo = env.socketTextStream(HOST_NAME, PORT, DELIMITER);
SingleOutputStreamOperator<DataInfo> dataInfoStream = socketDataInfo.map(new MapFunction<String, DataInfo>() {
@Override
public DataInfo map(String value) throws Exception {
String[] stringList = value.split(",");
DataInfo dataInfo = new DataInfo(UUID.randomUUID().toString(), Long.parseLong(
stringList[0]), stringList[1], Double.parseDouble(stringList[2]));
return dataInfo;
}
});
Table dataTable = tableEnv.fromDataStream(dataInfoStream,"rowkey,ts,info,val");
tableEnv.createTemporaryView("dataTable", dataTable);
// 这里要配自己HBase的zookeeper地址
tableEnv.executeSql("CREATE TABLE flinkTable (\n" +
" rowkey STRING,\n" +
" family1 ROW<ts BIGINT, info STRING, val DOUBLE>,\n" +
" PRIMARY KEY (rowkey) NOT ENFORCED\n" +
") WITH (\n" +
" 'connector' = 'hbase-1.4',\n" +
" 'table-name' = 'hbasetable',\n" +
" 'zookeeper.quorum' = 'xx.xx.xx.xx:2181'\n" +
")");
tableEnv.executeSql("INSERT INTO flinkTable " +
"SELECT rowkey, ROW(ts,info,val) FROM dataTable");
env.execute("HBaseFlinkJob");
}
public static class DataInfo{
private String rowkey;
private Long ts;
private String info;
private double val;
public String getRowkey() {
return rowkey;
}
public void setRowkey(String rowkey) {
this.rowkey = rowkey;
}
public Long getTs() {
return ts;
}
public void setTs(Long ts) {
this.ts = ts;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public double getVal() {
return val;
}
public void setVal(double val) {
this.val = val;
}
@Override
public String toString() {
return "DataInfo{" +
"ts=" + ts +
", info='" + info + '\'' +
", val='" + val + '\'' +
'}';
}
public DataInfo( String rowkey, Long ts, String info, double val) {
this.rowkey = rowkey;
this.ts = ts;
this.info = info;
this.val = val;
}
public DataInfo() {
}
}
}
五、在nc窗口输入:
1689999832,dong,32.45
六、在HBase检查数据是否已经写入:
scan 'hbasetable'