环境准备(jdk已有):
hadoop:
export HADOOP_HOME=/home/soft/hadoop
export PATH=$PATH:$HADOOP_HOME/bin
export PATH=$PATH:$HADOOP_HOME/sbin
zookeeper:
dataDir=/home/data/zookeeper/data
dataLogDir=/home/data/zookeeper/log
hbase:
export HBASE_MANAGES_ZK=false
export JAVA_HOME=/usr/java/jdk1.8.0_131/
<configuration>
<!-- 如果hbase配置HBASE_MANAGES_ZK=false,则必须配置此项否则hbase还是会启动自带zk -->
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<!-- hbase存放数据目录 -->
<property>
<name>hbase.rootdir</name>
<value>file:///home/data/hbase</value>
</property>
<!-- ZooKeeper数据文件路径 -->
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/zookeeper/data</value>
</property>
<property>
<name>hbase.unsafe.stream.capability.enforce</name>
<value>false</value>
<description>
Controls whether HBase will check for stream capabilities (hflush/hsync).
</description>
</property>
</configuration>
#hbase
export HBASE_HOME=/home/soft/hbase
export PATH=$HBASE_HOME/bin:$PATH
create 'userinfo', 'info'
hvie
(flume skin为hbase时用到hive的jar,需要把hive/lib目录下的hbase开头的jar复制到flume/lib下,否则无法使用)
flume:
#flume
export FLUME_HOME=/home/soft/flume
export PATH=$PATH:$FLUME_HOME/bin
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# 配置源
a1.sources.r1.type = avro
a1.sources.r1.port = 44444
a1.sources.r1.bind = localhost
# channel 为内存
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.sources.r1.channels = c1
#sinks 为hbase
a1.sinks.k1.type = org.apache.flume.sink.hbase.HBaseSink
#hbase表名
a1.sinks.k1.table = userinfo
#hbase列簇
a1.sinks.k1.columnFamily =info
#通配符(数据格式为1#小明#18 )
a1.sinks.k1.serializer.regex =(.*)#(.*)#(.*)
a1.sinks.k1.serializer = org.apache.flume.sink.hbase.RegexHbaseEventSerializer
a1.sinks.k1.channel = c1
#列名
a1.sinks.k1.serializer.colNames = ROW_KEY,name,age
# 索引为0,即第一列
a1.sinks.k1.serializer.rowKeyIndex = 0
flume-ng agent --conf conf --conf-file conf/flumeHbase.conf --name a1 -Dflume.root.logger=INFO,console
开始测试
用RPC客户端写入数据到flmue:
1.依赖
<dependency>
<groupId>org.apache.flume</groupId>
<artifactId>flume-ng-core</artifactId>
<version>1.9.0</version>
</dependency>
2.工具类
public class FlumeUtils {
private static RpcClient client;
private static String hostname="127.0.0.1";
private static int port=44444;
public static void sendDataToFlume(String data) {
if(client==null || !client.isActive()){
client = RpcClientFactory.getDefaultInstance(hostname, port);
}
Event event = EventBuilder.withBody(data, Charset.forName("UTF-8"));
try {
client.append(event);
} catch (EventDeliveryException e) {
client.close();
client = null;
client = RpcClientFactory.getDefaultInstance(hostname, port);
}
}
public static void sendDataToFlumeList(List<String> list){
if(client==null || !client.isActive()){
client = RpcClientFactory.getDefaultInstance(hostname, port);
}
List<Event> events=new ArrayList<>();
for(String data:list){
Event event = EventBuilder.withBody(data, Charset.forName("UTF-8"));
events.add(event);
}
try {
client.appendBatch(events);
} catch (EventDeliveryException e) {
client.close();
client = null;
client = RpcClientFactory.getDefaultInstance(hostname, port);
}
}
public static void cleanUp() {
client.close();
}
}
3.请求action
@GetMapping("test")
public String test(){
List<String> list=new ArrayList<>();
int key=1001;
for (int i = 0; i < 200; i++) {
key+=i;
list.add(JSON.toJSONString(key+"#"+(name+i)+"#"+i));
}
FlumeUtils.sendDataToFlumeList(list);
return "success";
}
查看是否写入成功
1.hbase shell
2 scan ‘userinfo’ 可见已写入hbase