hadoop综合实验(对日志的处理mapreduce保存到csv)

综合实验:网站访问日志采集、处理及分析

实验步骤
注:截图必须使用实验机的带水印截图功能,题目中要求截图但未提供的视为未做

一、使用Flume导入日志数据

数据文件下载地址:

wget "http://10.90.3.2/files/web_log/xaa.log"
wget "http://10.90.3.2/files/web_log/xab.log"
wget "http://10.90.3.2/files/web_log/xac.log"

新建一个待监控的文件夹,放入三个日志文件我这里是lhx

1、(代码)创建flume配置,文件内容及部分属性注释(注意路径):

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# 定义源: spooldir
a1.sources.r1.type = spooldir
# 指定监控目录(本地路径)
a1.sources.r1.spoolDir = /home/ubuntu/lhx
a1.sources.r1.decodeErrorPolicy = IGNORE

# Describe the sink(hdfs路径)
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path =  /flume/2015070214/ 
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.useLocalTimeStamp = true
a1.sinks.k1.hdfs.rollSize = 0
a1.sinks.k1.hdfs.rollCount = 0

# Use a channel which buffers events in memory
a1.channels.c1.type = memory

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

2、(截图)运行flume配置的命令(运行前需启动hadoop):

cd /opt/hadoop/sbin
hadoop namenode -format
./start-all.sh

3、(截图)日志导入到hdfs后,查看hdfs目录内容:

flume-ng agent  -f  /home/ubuntu/flume.conf  -n  a1

4、(截图)选取一个文件查看前几条内容:

hdfs -dfs -cat /flume/2015070214/ | head -10

二、使用MapReduce对日志数据进行预处理

1、(代码)map和reduce文件:

def tq():#方法二以及导入csv
    with open('test.csv', 'w', encoding='utf-8', newline="") as f:
      for line in sys.stdin:
        line = line.strip().replace('[','').replace(']','').replace('"','').replace('- - ','').replace(' -','-')
        ss = line.strip().split(' ')
        if len(ss)==7:
            line = line.strip().replace('/ ','/').replace(' ',',')
            f.write(line)
            f.write('\n')
        else:
            line = line.strip().replace(' ',',')
            f.write(line)
            f.write('\n')
        if len(ss)==7:
            kong =','
            line = ''.join(ss[0]+kong+ss[1]+kong+ss[2]+kong+ss[3]+ss[4]+kong+ss[5]+kong+ss[6])
            f.write(line)
            f.write('\n')
tq()     
#查看- 测试
#hdfs dfs -cat /flume/2015070214/* |wc -l 
#直接执行
#hdfs dfs -cat /flume/2015070214/* | python map.py 
def gx():
    for line in sys.stdin:
        line = line.strip().replace('[','').replace(']','').replace('"','').replace('- - ','').replace(' -','-')
        ss = line.strip().split(' ')
        if len(ss)==7:
            line = line.strip().replace('/ ','/').replace(' ',',')
            print(line)
        else:
            line = line.strip().replace(' ',',')
            print(line)
        if len(ss)==7:
            kong =','
            line = ''.join(ss[0]+kong+ss[1]+kong+ss[2]+kong+ss[3]+ss[4]+kong+ss[5]+kong+ss[6])
gx()            

2、(截图)用hadoop-streaming运行MapReduce的命令、及运行时的部分截图:

$HADOOP_HOME/bin/hadoop jar \
$HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-2.7.3.jar \
-files "/home/ubuntu/map.py" \
-input /flume01/2015070214/* \
-output /flume/2015070214/out00 \
-mapper "python map.py"

在这里插入图片描述

csv 导入

hdfs dfs -cat /flume/2015070214/* | python map.py 

在这里插入图片描述

导入csv(方法二必看)

hdfs dfs -cat /flume/2015070214/* | python map.py  >test.csv

在这里插入图片描述

3、(截图)查看几条产出的数据记录截图:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南师大蒜阿熏呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值