作业三:华为云大数据实时分析处理——Flume 日志采集实验
作业三:华为云大数据实时分析处理 —— Flume 日志采集实验
python
coding:utf-8
"""模拟生成商品销售测试数据。
用法:
python autodatagen.py <输出文件路径> <生成记录条数>
示例:
python autodatagen.py "/tmp/flume_spooldir/test.txt" 100
"""
import random
import string
import sys
import time
大写、小写字母集合,用于随机生成用户名与邮箱
ALPHABET_UPPER = string.ascii_uppercase
ALPHABET_LOWER = string.ascii_lowercase
商品编号、门店编号、购物行为、购买日期的候选集合
GOODS_NO_LIST = ["220902", "430031", "550012", "650012", "532120",
"230121", "250983", "480071", "580016", "950013",
"152121", "230121"]
STORE_ID_LIST = ["313012", "313013", "313014", "313015", "313016",
"313017", "313018", "313019", "313020", "313021",
"313022", "313023"]
SHOPPING_TYPE_LIST = ["pv", "buy", "cart", "fav", "scan"]
BUY_DATE_LIST = ["2019-08-01", "2019-08-02", "2019-08-03", "2019-08-04",
"2019-08-05", "2019-08-06", "2019-08-07"]
已使用的行键,保证行键唯一
rowkey_used_list = []
def get_random(instr, length):
"""从给定序列中随机取 length 个字符并拼接成字符串。"""
return ''.join(random.sample(instr, length))
def get_random_rowkey():
"""生成行键:两位随机数 + 10 位时间戳,并保证不重复。"""
while True:
num = random.randint(0, 99)
rowkey = str(num).zfill(2) + str(int(time.time()))
if rowkey not in rowkey_used_list:
rowkey_used_list.append(rowkey)
return rowkey
def get_random_name(length):
"""生成指定长度的随机用户名。"""
return string.capwords(get_random(ALPHABET_LOWER, length))
def get_random_age():
"""生成 18~60 岁的随机年龄。"""
return str(random.randint(18, 60))
def get_random_sex():
"""随机返回性别。"""
return random.choice(["woman", "man"])
def get_random_goods_price():
"""生成商品价格:1~999 的整数部分加两位小数部分。"""
price_int = random.randint(1, 999)
price_decimal = random.randint(1, 99)
return str(price_int) + "." + str(price_decimal)
def get_random_tel():
"""生成 11 位手机号:常见号段 + 8 位随机数字。"""
prefix_list = ["130", "131", "132", "133", "134", "135", "136",
"137", "138", "139", "147", "150", "151", "152",
"153", "155", "156", "157", "158", "159", "186",
"187", "188"]
return random.choice(prefix_list) + ''.join(random.sample('0123456789', 8))
def get_random_email(length):
"""生成随机邮箱地址。"""
alphabet_list = ALPHABET_LOWER + ALPHABET_UPPER
email_list = ["163.com", "126.com", "qq.com", "gmail.com", "huawei.com"]
return get_random(alphabet_list, length) + "@" + random.choice(email_list)
def get_random_record():
"""按既定字段顺序拼接一条完整的销售记录(CSV 格式)。"""
field_list = [
get_random_rowkey(), # 行键
get_random_name(5), # 用户名
get_random_age(), # 年龄
get_random_sex(), # 性别
random.choice(GOODS_NO_LIST), # 商品编号
get_random_goods_price(), # 商品价格
random.choice(STORE_ID_LIST), # 门店编号
random.choice(SHOPPING_TYPE_LIST), # 购物行为
get_random_tel(), # 联系电话
get_random_email(10), # 邮箱
random.choice(BUY_DATE_LIST), # 购买日期
]
return ",".join(field_list)
def write_record_to_file(file_path, record_count):
"""将 record_count 条记录覆盖写入指定文件,每条记录占一行。"""
with open(file_path, 'w', encoding='utf-8') as file_obj:
for _ in range(record_count):
file_obj.write(get_random_record() + "\n")
if name == "main":
# sys.argv[1] 为输出文件路径,sys.argv[2] 为生成条数
write_record_to_file(sys.argv[1], int(sys.argv[2]))
运行与结果查看:
bash
cd /opt/client
python autodatagen.py "/tmp/flume_spooldir/test.txt" 100
more /tmp/flume_spooldir/test.txt
wc -l /tmp/flume_spooldir/test.txt

2.2 作业心得
编写脚本时我把原来散落在代码里的候选数据(商品编号、门店编号、购物行为等)提取为全大写常量,把每条记录的拼接改为列表加 join,可读性和可维护性都更好;同时使用 with open(...) 管理文件句柄,避免异常时文件未关闭。
在调试中我注意到两个细节:random.sample 要求取样长度不能超过序列长度,否则抛 ValueError;行键用“随机数 + 时间戳”仍可能重复,因此需要集合并查重。通过这次作业,我巩固了 Python 的函数拆分、常量命名和命令行参数 sys.argv 的使用,也理解了为什么实时计算实验要先用脚本模拟数据源——这样可以在没有真实业务系统的情况下反复验证采集与计算链路。

浙公网安备 33010602011771号