题干:
某大型电商公司从后台服务器收集到30W条的日志用户行为数据,经过数据初步清洗得到数据如下表sale_user.zip,假如你是该公司一员开发工程师,需要你利用大数据集群为公司存储、计算、分析这些数据,并给出分析结果。需求如下:
1.在Linux系统下将用户行为数据sale_user.zip将解压(解压后文件为sale_user.csv)。(8分)
采用MR程序完成数据清洗:
a) 将数据集中的标题行删除;
b) 在数据集中添加一个表示省份的列province,同时为每一行在该列上生成一个随机省份值,如"北京","上海","广州","深圳";(2分)
c) 将数据集中的time字段中的小时数字去掉,只保留年-月-日;(2分)
d) 输出路径为hdfs上:hdfs://master:9000/员工姓名,例如zhangsan (root用户需要给输出目录赋予权限,参考命令如:hadoop fs -chmod -R 777 /员工姓名 )。(2分)
2.Hive数据分析。
a) 创建hive外部表user_action_external_hive,location指向第一步处理后数据的存储目录hdfs://master:9000/员工姓名;(3分)
b) 使用Hive分析统计不同地区的用户在网站上各种行为的次数,即浏览总次数、加入购物车总次数、收藏总次数、购买总次数,并将结果写入一个新建的hive表,表名为user_action_stat。(5分)
3.将数据从Hive导出到元数据库MySQL。
a) 在MySQL中创建一个以员工名(如zhangsan)命名的数据库,在该库中创建一个表action_stat。利用Sqoop工具,将Hive中的user_action_stat中数据导入action_stat表中;(6分)
b) 在MySQL中,查询action_stat表中前10条记录。(2分)
提示:
sale_user.zip部分数据如下(具体样本数据,考试时发放),共5个字段:
user_iditem_idbehavior_typeitem_categorytime10001082285259775140762014-12-08 18100010824368907155032014-12-12 12100010824368907155032014-12-12 121000108253616768197622014-12-02 15
数据集结构为:
user_id:用户id
item_id:商品id
behavior_type:用户行为类型,包括浏览、收藏、加购物车、购买,对应值分别为1,2,3,4。
item_category:商品分类。
time:用户操作时间(格式为:年-月-日 小时)。
「hive数据分析—电商数据分析」https://www.aliyundrive.com/s/4UaUqNVaoXR
项目实施:
先将源数据进行解压处理,将文件放到要执行的文件位置。
package com.cqcvc.hive;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class LMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context)
throws IOException, InterruptedException {
if (key.get() != 0) {
context.write(value, NullWritable.get());
}
}
}
package com.cqcvc.hive;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class LReduce extends Reducer<Text, NullWritable, Text, NullWritable> {
@Override
protected void reduce(Text key, Iterable<NullWritable> values,
Reducer<Text, NullWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {
String s = key.toString();
String[] provinces = { "北京", "上海", "广州", "深圳" };
String[] arr = s.split(",");
String time = arr[4];
try {
SimpleDateFormat d1 = new SimpleDateFormat("yyyy-mm-dd hh");
Date dd = d1.parse(time.toString());
SimpleDateFormat d2 = new SimpleDateFormat("yyyy年mm月dd日");
String dd1 = d2.format(dd);
String k = arr[0] + "," + arr[1] + "," + arr[2] + "," + arr[3] + "," + dd1 + ","+ provinces[(int) (Math.random() * 4)];
context.write(new Text(k), NullWritable.get());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
package com.cqcvc.hive;
import java.io.IOException;
import java.util.Date;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class LDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException,
InterruptedException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.87.201:9000");
System.setProperty("HADOOP_USER_NAME", "root");
Job job = Job.getInstance(conf);
job.setJarByClass(LDriver.class);
job.setMapperClass(LMapper.class);
job.setReducerClass(LReduce.class);
job.setNumReduceTasks(1);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
Path[] pathsArr = { new Path("/data/sale_user.csv") };
FileInputFormat.setInputPaths(job, pathsArr);
FileOutputFormat.setOutputPath(job, new Path("/yuangong"));
job.waitForCompletion(true);
}
}
导入集群执行MR的jar包文件:
这里可以将编写好的MR放到Hadoop的集群上去使用也可以直接在eclipse上使用,打包的方法:
eclipse–>Export–>java–>JAR file
找到需要打包的三个mapreduce的java程序,勾选上然后找到select the export destination:
设置jar包导出的位置,点击Next两次然后找到select the class ot the application entry point:
选择导出的包的main执行的那个class程序也就是我们的Driver。
打包后可以直接将jar包文件上传到hadoop集群上去,使用命令hadoop jar 包文件的名称.jar来执行包文件。
create EXTERNAL table user1(
user_id String,
item_id String,
behavior_type String,
item_category string,
time string,
province string)
row format delimited fields terminated by ',' ;
这里出现了一个问题:
FAILED: ParseException line 1:22 cannot recognize input near 'user' '(' 'user_id' in table name
解决方法:
换一个数据表的名字如user1,因为识别问题导致程序识别的时候一直显示出错是在user附近。
使用hive sql命令:
load data inpath '/yuangong' into table user1;
将数据导入到user1数据表中去,如果出现错误,就将hadoop集群上的/yuangong文件删除,重新运行一次外部表数据会自动导入到数据表中去。
题目:
使用Hive分析统计不同地区的用户在网站上各种行为的次数,即浏览总次数、加入购物车总次数、收藏总次数、购买总次数,并将结果写入一个新建的hive表,表名为user_action_stat。
先建立需要存储数据的表:
create table user_action_stat(
province string,
user_id int,
Look int,
Collection int,
Cart int,
Buy int)
row format delimited fields terminated by ',';
然后书写insert语句将内容插入到表中去:
insert overwrite table user_action_stat
select province,
user_id,sum(if(behavior_type=1,1,0)),
sum(if(behavior_type=2,1,0)),
sum(if(behavior_type=3,1,0)),
sum(if(behavior_type=4,1,0))
from user1 group by province,user_id;
将得到的表存放到MySQL数据库中去(我们使用的是Sqoop工具):
sqoop export \
--connect jdbc:mysql://slave1/zhangsan?characterEncoding=utf8 \
--username root \
--password 123456 \
--table action_stat \
--export-dir /Hive_WH/chengzhi.db/user_action_stat
最后在MySQL中编写sql查询语句:
SELECT * FROM action_stat LIMIT 10;
Original: https://blog.csdn.net/qq_58132795/article/details/122481959
Author: 干饭小龚
Title: hive实训项目之电商数据分析
相关阅读
Title: tensorflow的详细安装(包含jupyter notebook)
安装Anaconda
在官网https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/下载Anaconda
自己用就选Just ME,我这里是对所有用户都可以
完成等待安装
安装完成就打开菜单栏的Anaconda Prompt
进入之后可以输入conda --version来查看下载的版本,我这里下载的是4.8.2版本的
; 安装tensorflow
添加镜像源通道
首先要加一个国内图源频道,一般都是默认的外图连接,下载会很慢。之前使用的是清华大学的图片源,但现在设置了权限,安装后可能会出现问题,可以使用以下命令来删除权限。
[En]
First of all, it is necessary to add a domestic image source channel, which is generally the default foreign image connection, and the download will be very slow. The image source of Tsinghua University was used before, but now the permissions are set and there may be problems after installation, so you can use the following command to remove permissions.
conda config
之后再来添加镜像源通道
conda config
conda config
conda config
conda config
conda config
接下来就要开始创建环境了输入
conda create -n tensorflow python=3.7
在正常情况下,不出问题的话是会显示以下,他告诉你启动tensorflow用activate tensorflow,退出用deactivate tensorflow,接下来我们就要进入tensorflow了
但是有的时候会报错的可能性我自己遇到的统计了一下
WARNING: A newer version of conda exists.
=
current version: 4.8.2
latest version: 4.12.0
也许你之前下载的时候发现了一个错误,然后当你再次下载时,你会继续报告它。原因是您之前下载的内容已断开连接,尚未清理。如果你再下载一次,你不知道到哪里去下载。
[En]
Maybe you found an error when you downloaded it before, and then you will continue to report it when you download it again. The reason is that what you downloaded before is disconnected and hasn't been cleaned up. If you download it again, you don't know where to download it.
报错如下所示:
conda clean all
这时候就会发现removing删除之前已经下载的文件包,之后再来下载就可以了
激活tensorflow
输入activate tensorflow,从(base)的前缀变成(tensorflow)的就代表激活成功
安装tensorflow
进入tensorflow之后,输入命令下载tensorflow,这里可以指定tensorflow的版本(tensorflow==2.0.0),但是有的时候在库里寻找固定版本时间比较长,所以我建议不固定版本,让他自己匹配合适的版本
pip install tensorflow
这样就代表下载完成了,如果不放心,可以使用conda info --envs来查看一下,看看自己是否下载成功,显示tensorflow就表示成功了
conda info
安装jupyter notebook
接下来就简单了,直接利用install安装,选择y,等待安装即可
; 输入jupyter notebook自动跳转到浏览器
在tensorflow里输入jupyter notebook就会自动跳转到浏览器
这个时候很多人都不知道在哪里打开命令执行页面,在右上角有一个New,选择我们下载好的python3.7版本,就进入命令页面了
测试
输入命令看是否执行成功
import tensorflow as tf
print(tf.__version__)
tf1版本中用此代码测试:
import tensorflow as tf
sess = tf.Session()
a = tf.constant(10)
b= tf.constant(12)
sess.run(a+b)
若出现22表明环境安装成功
tf2版本中⽤用以下代码测试:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
sess = tf.compat.v1.Session()
a = tf.constant(10)
b= tf.constant(12)
sess.run(a+b)
若出现22表明环境安装成功
我这里结果显示的是22,安装成功
Original: https://blog.csdn.net/PEABRAIND/article/details/123883430
Author: 独宠。
Title: tensorflow的详细安装(包含jupyter notebook)

ubuntu18.04安装 ORB_SLAM2

Linux上安装tensorflow

CUDA之Stream介绍

记一次失败的《将视频中的音频转换成文字》的经历

tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.

Github代码复现——SimCLR跑自己的数据集(TensorFlow2)

TensorFlow系列——feature_column特征工具说明

6个实用的 Python 自动化脚本,告别加班,你学会了吗?

【论文笔记】Recommendations as Treatments: Debiasing Learning and Evaluation

tensorflow模型固化

Tensorflow加速方法参考

Ubuntu 20.04 安装 tensorflow-gpu

远程公司内网服务器【内网穿透】

虚拟麦克风音频输入_硅麦克风电路连接指南
