【MapReduce】猫眼电影数据库传输数据
文章目录
- 猫眼电影数据库传输数据
- 1.数据集样式
- 分析
- 2.封装数据库Bean阶段
- 3.Maper阶段
- 4.Reducer阶段
- 5.Driver阶段
- 6.结果展示
猫眼电影数据库传输数据
1.数据集样式
分析
将数据存储到数据库中,按照“,”号拆分数据集;定义Bean对象,封装字段属性、自定义比较(按照排名输出到数据库);Map阶段获取数据,对每个字段处理(去重双引号),封装;Reducer阶端输出。
返回顶部
2.封装数据库Bean阶段
创建数据库表格存储数据
create table film
(
ranks int(255),
name varchar(255),
actors varchar(255),
grade varchar(255),
number varchar(255),
common varchar(255)
);
package 猫眼电影数据处理;import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class SQLBean implements WritableComparable<SQLBean>, DBWritable {// 封装数据库记录字段private int ranks;private String name;private String actors;private String grade;private String number;private String common;@Overridepublic int compareTo(SQLBean o) {if (this.ranks > o.ranks){return 1;}else if (this.ranks < o.ranks){return -1;} else{return 0;}}public void set(int rank, String name, String actors, String grade, String number, String common) {this.ranks = rank;this.name = name;this.actors = actors;this.grade = grade;this.number = number;this.common = common;}@Overridepublic void write(DataOutput out) throws IOException {out.writeInt(ranks);out.writeUTF(name);out.writeUTF(actors);out.writeUTF(grade);out.writeUTF(number);out.writeUTF(common);}@Overridepublic void readFields(DataInput in) throws IOException {this.ranks = in.readInt();this.name = in.readUTF();this.actors = in.readUTF();this.grade = in.readUTF();this.number = in.readUTF();this.common = in.readUTF();}@Overridepublic void write(PreparedStatement statement) throws SQLException {statement.setInt(1,this.ranks);statement.setString(2,this.name);statement.setString(3,this.actors);statement.setString(4,this.grade);statement.setString(5,this.number);statement.setString(6,this.common);}@Overridepublic void readFields(ResultSet resultSet) throws SQLException {this.ranks = resultSet.getInt(1);this.name = resultSet.getString(2);this.actors = resultSet.getString(3);this.grade = resultSet.getString(4);this.number = resultSet.getString(5);this.common = resultSet.getString(6);}@Overridepublic String toString() {return "SQLBean{" +"ranks='" + ranks + '\'' +", name='" + name + '\'' +", actors='" + actors + '\'' +", grade='" + grade + '\'' +", number='" + number + '\'' +", common='" + common + '\'' +'}';}public int getRanks() {return ranks;}public void setRanks(int ranks) {this.ranks = ranks;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getActors() {return actors;}public void setActors(String actors) {this.actors = actors;}public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}public String getCommon() {return common;}public void setCommon(String common) {this.common = common;}}
返回顶部
3.Maper阶段
package 猫眼电影数据处理;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;public class SQLMap extends Mapper <LongWritable, Text, IntWritable,SQLBean>{SQLBean v = new SQLBean();IntWritable k = new IntWritable();@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {// 获取一行数据进行拆分String[] fields = value.toString().split(",");// 对数据进行处理 --- 去除引号for (int i = 0; i < fields.length ; i++) {fields[i] = fields[i].substring(1,fields[i].length()-1);}// 封装sqlk.set(Integer.parseInt(fields[0]));v.set(Integer.parseInt(fields[0]),fields[1],fields[2],fields[3],fields[4],fields[5]);// 写出context.write(k,v);System.out.println(k);System.out.println(fields.length);}
}
返回顶部
4.Reducer阶段
package 猫眼电影数据处理;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;public class SQLReducer extends Reducer<IntWritable,SQLBean,SQLBean,NullWritable> {@Overrideprotected void reduce(IntWritable key, Iterable<SQLBean> values, Context context) throws IOException, InterruptedException {// 写出for (SQLBean bean:values){context.write(bean,NullWritable.get());}}
}
返回顶部
5.Driver阶段
package 猫眼电影数据处理;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;
import org.apache.hadoop.mapreduce.lib.db.DBOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;public class SQLDriver {public static void main(String[] args) {try {// 获取jobConfiguration conf = new Configuration();// 创建数据库连接
// DBConfiguration.configureDB(
// conf,
// "com.mysql.jdbc.Driver",
// "jdbc:mysql://192.168.64.178:3306/school",
// "root","123456"
// );DBConfiguration.configureDB(conf,"com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/mr","root","123456");Job job = Job.getInstance(conf);// 配置job.setMapperClass(SQLMap.class);job.setReducerClass(SQLReducer.class);job.setJarByClass(SQLDriver.class);job.setMapOutputKeyClass(IntWritable.class);job.setMapOutputValueClass(SQLBean.class);job.setOutputKeyClass(SQLBean.class);job.setOutputValueClass(NullWritable.class);// 配置输入文件路径job.setInputFormatClass(TextInputFormat.class);job.setOutputFormatClass(DBOutputFormat.class);Path in = new Path("G:\\Projects\\IdeaProject-C\\MapReduce\\src\\main\\java\\猫眼电影数据处理\\data\\douban.txt");FileInputFormat.setInputPaths(job,in);String[] fields = {"ranks","name","actors","grade","number","common"};DBOutputFormat.setOutput(job,"film",fields);// 提交System.exit(job.waitForCompletion(true) ? 0:1);} catch (Exception e){e.printStackTrace();}}
}
返回顶部
6.结果展示
返回顶部
踩的坑:
- 报错:java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 5
- 报错:java.lang.Exception: java.io.IOException: Initialization of all the collectors failed.
- 报错:java.lang.Exception: java.io.IOException: Could not create connection to database server.
【MapReduce】猫眼电影数据库传输数据相关推荐
- 猫眼电影票房爬取到MySQL中_猫眼电影爬取(一):requests+正则,并将数据存储到mysql数据库...
前面讲了如何通过pymysql操作数据库,这次写一个爬虫来提取信息,并将数据存储到mysql数据库 1.爬取目标 爬取猫眼电影TOP100榜单 要提取的信息包括:电影排名.电影名称.上映时间.分数 2 ...
- 猫眼电影MySQL数据库怎么写_猫眼电影和电影天堂数据csv和mysql存储
字符串常用方法 #去掉左右空格 'hello world'.strip() #'hello world'#按指定字符切割 'hello world'.split(' ') #['hello','wor ...
- 【JAVA爬虫】爬取猫眼电影TOP100并将数据存入数据库
前几天的简单写了个利用JSOUP进行JAVA爬虫,里面有谈到后续版本会更新数据库操作,所以这次来更新了. 版本更新 此次的版本里数据爬取部分新增了[电影主演-star]和[电影评分-score]部分, ...
- python爬取电影网站存储于数据库_python爬虫 猫眼电影和电影天堂数据csv和mysql存储过程解析...
字符串常用方法 # 去掉左右空格 'hello world'.strip() # 'hello world' # 按指定字符切割 'hello world'.split(' ') # ['hello' ...
- Python之requests+xpath爬取猫眼电影并写入数据库(图文教程)
文章目录 一.pyhton连接mysql数据库 二.用xpath抓取有用信息 说几个比较容易掉坑的地方 一 二 三 效果 一.pyhton连接mysql数据库 我是写了一个py文件来封装一下,然后在爬 ...
- 猫眼html源码,50 行代码教你爬取猫眼电影 TOP100 榜所有信息
点击上方"CSDN",选择"置顶公众号" 关键时刻,第一时间送达! 今天,手把手教你入门 Python 爬虫,爬取猫眼电影 TOP100 榜信息. 作者 | 丁 ...
- 猫眼电影评论_电影的人群意见和评论家的意见一样好吗?
猫眼电影评论 Ryan Bellgardt's 2018 movie, The Jurassic Games, tells the story of ten death row inmates who ...
- 50 行代码教你爬取猫眼电影 TOP100 榜所有信息
点击上方"CSDN",选择"置顶公众号" 关键时刻,第一时间送达! 今天,手把手教你入门 Python 爬虫,爬取猫眼电影 TOP100 榜信息. 作者 | 丁 ...
- python爬虫实战——猫眼电影案例
python爬虫实战--猫眼电影案例 ·背景 笔者上一篇文章<基于猫眼票房数据的可视化分析>中爬取了猫眼实时票房数据,用于展示近三年电影票房概况.由于数据中缺少导演/演员/编剧阵容等信 ...
- python爬虫猫眼电影票房_python爬取猫眼电影top100排行榜
爬取猫眼电影TOP100(http://maoyan.com/board/4?offset=90) 1). 爬取内容: 电影名称,主演, 上映时间,图片url地址保存到mariadb数据库中; 2). ...
最新文章
- 鸟哥学习笔记---网络安全基础
- window 下Eclipse c++的开发环境配置
- 牵引力教育学校分析UI设计师的薪酬水平
- 自定义SpringBoot Starter实现
- 文末送书丨深度迁移学习方法的基本思路
- 碰碰车司机教你Linux下使用nmon分析系统性能
- 二级分类_免费获取2021年二级造价工程师考试大纲
- 每日Ubuntu小技巧 - 使用TeamViewer连接远程桌面
- JSP四大域对象与九大内置对象
- IT销售素质 -- 自信进取
- 从零开始用python处理excel数据_Python对Excel的操作
- php 判断用户是否刷新,如何在php和ajax中创建一个注册页面,它会在不刷新页面的情况下检查某个用户名是否已经存在? - php...
- HCIE-Security Day20:GRE协议:实验(一)配置基于静态路由的GRE隧道
- 前端性能优化(十一)
- Cent OS 6.X 开机错误修复
- 酷Q萌萌机器人_替代qqbot,使用酷q机器人实现qq机器人
- 这样部署防病毒网关才妙啊!2000字详解奉上
- USBCAN接口卡打开失败收不到数据常见问题分析
- 5.13 利用图层的矢量蒙版打造浪漫情调 [原创Ps教程]
- 便利蜂创始人数字化经验分享:如何用全链路数字化 重塑零售业