说明
监控本地文件,将变化数据 上传 Hive 在 HDFS 目录上
准备
hive 要启动 mysql
hive 要启动 metastore
https://www.malaoshi.top/show_1IX2HLjutY5g.html
hive 创建表
创建外部表
cd /program/hive-3.1.2/bin
./hive
create external table t_log
(
datetime string,
ip string,
msg string
)
row format delimited fields terminated by ',' ;
通过 spark thrift 服务创建外部表
创建外部表需要指定 location 路径
create external table t_log
(
datetime string,
ip string,
msg string
)
row format delimited fields terminated by ','
LOCATION '/user/hive/warehouse/t_log'
;
查看 表位置
http://hadoop1:9870/explorer.html#/user/hive/warehouse/t_log
如下:
/user/hive/warehouse/t_log
下面会用到
编写配置文件
登录 hadoop1,
cd /program/flume-1.9.0/conf
vim test_hive.conf
内容如下:
# 定义agent名字为a1,相当于变量名,可任意起
# 设置 sources、sinks、channels的名字
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# 配置source类型为taildir
a1.sources.r1.type = TAILDIR
a1.sources.r1.filegroups = f1
# 监控该文件
a1.sources.r1.filegroups.f1 = /test_flume/1.log
# 配置 sink
a1.sinks.k1.type = hdfs
# hdfs目录,即:上面查看的位置
a1.sinks.k1.hdfs.path = hdfs://hadoop1:8020/user/hive/warehouse/t_log
# 不压缩
a1.sinks.k1.hdfs.fileType = DataStream
# 文件后缀
a1.sinks.k1.hdfs.fileSuffix = .log
# 间隔 60s 后,如果有数据,就新建文件
a1.sinks.k1.hdfs.rollInterval = 60
# 如果写入内容达到120m(要低于hadoop的128m),就新建文件
a1.sinks.k1.hdfs.rollSize = 125829120
# 配置 channel 类型为内存
a1.channels.c1.type = memory
# 内存队列最大为1000,可以存储1000个event
a1.channels.c1.capacity = 1000
# 一个事务中从source接收的events数量或发送给sink的events数量最大为100
a1.channels.c1.transactionCapacity = 100
# 将source 和 sink 绑定到channel上
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
启动
进入 flume 根目录:
cd /program/flume-1.9.0/
执行下面命令(必须在上面目录中才能执行):
bin/flume-ng agent -n a1 -c conf -f conf/test_hive.conf -Dflume.root.logger=INFO,console
解释:
https://www.malaoshi.top/show_1IX258AKgb2F.html
执行结果
会打印一大堆信息,看到下面字样,表示启动成功:
2021-10-20 22:59:04,284 (lifecycleSupervisor-1-4) [INFO - org.apache.flume.instrumentati on.MonitoredCounterGroup.register(MonitoredCounterGroup.java:119)] Monitored counter gro up for type: SOURCE, name: r1: Successfully registered new MBean.
2021-10-20 22:59:04,285 (lifecycleSupervisor-1-4) [INFO - org.apache.flume.instrumentati on.MonitoredCounterGroup.start(MonitoredCounterGroup.java:95)] Component type: SOURCE, n ame: r1 started
测试
修改 1.log 文件
修改 /test_flume/1.log 文件:
增加下面内容:
2021-10-05 16:30:40,192.168.1.200,李雷
2021-10-05 18:30:40,192.168.1.200,lucy
2021-10-06 16:30:40,192.168.1.200,李雷点击了iphone广告
2021-10-06 18:30:40,192.168.1.200,lucy点击lv广告
在 hive 中查询表
在 hive 命令环境中,查询表:
select * from t_log
查询结果:
2021-10-05 16:30:40 192.168.1.200 李雷
2021-10-05 18:30:40 192.168.1.200 lucy
2021-10-06 16:30:40 192.168.1.200 李雷点击了iphone广告
2021-10-06 18:30:40 192.168.1.200 lucy点击lv广告
Time taken: 0.187 seconds, Fetched: 4 row(s)