题目
以下是RNG S8 8强赛失败后,官微发表道歉微博下一级评论
字段 字段含义
index 数据id
child_comment 回复数量
comment_time 评论时间
content 评论内容
da_v 微博个人认证
like_status 赞
pic 图片评论url
user_id 微博用户id
user_name 微博用户名
vip_rank 微博会员等级
stamp 时间戳
需求
1.1、在kafak中创建rng_comment主题,设置2个分区2个副本
1.2、数据预处理,把空行过滤掉
1.3、请把给出的文件写入到kafka中,根据数据id进行分区,id为奇数的发送到一个分区中,偶数的发送到另一个分区
1.4、使用Spark Streaming对接kafka
1.5、使用Spark Streaming对接kafka之后进行计算
在mysql中创建一个数据库rng_comment
在数据库rng_comment创建vip_rank表,字段为数据的所有字段
在数据库rng_comment创建like_status表,字段为数据的所有字段
在数据库rng_comment创建count_conmment表,字段为 时间,条数
1.5.1、查询出微博会员等级为5的用户,并把这些数据写入到mysql数据库中的vip_rank表中
1.5.2、查询出评论赞的个数在10个以上的数据,并写入到mysql数据库中的like_status表中
1.5.3、分别计算出2018/10/20 ,2018/10/21,2018/10/22,2018/10/23这四天每一天的评论数是多少,并写入到mysql数据库中的count_conmment表中
数据预处理的代码
import org.apache.spark.rdd.RDD
import org.apache.spark.{
SparkConf, SparkContext}
object DataPreprocess {
def main(args: Array[String]): Unit = {
val spark: SparkConf = new SparkConf().setMaster("local").setAppName("dealdata")
val sc: SparkContext = new SparkContext(spark)
sc.setLogLevel("WARN")
val filelines: RDD[String] = sc.textFile("G:\\bigdata_note02\\spark\\作业09\\4.14号练习题\\data\\rng_comment.txt")
filelines.filter(x => if (x.trim.length > 0) true else false).filter(arr => {
val strings: Array[String] = arr.split("\t")
if (strings.length == 11) {
true
} else {
false
}
// 保存文件时进行分区
}).coalesce(1).saveAsTextFile("G:\\bigdata_note02\\spark\\作业09\\4.14号练习题\\data\\output")
}
}
数据生产到kafka主题的代码
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.io.*;
import java.util.Properties;
public class KafkaProduce0416 {
public static void main(String[] args) throws IOException {
Properties props = new Properties();
props.put("bootstrap.servers", "node01:9092,node02:9092,node03:9092");
props.put("acks", "all");
props.put("retries", 2);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
File inputFile = new File("G:\\bigdata_note02\\spark\\作业09\\4.14号练习题\\data\\output\\part-00000");
BufferedReader bufferedReader = new BufferedReader(new FileReader(inputFile));
String line = null;
int parttion = 0;
while ((line = bufferedReader.readLine()) != null) {
try {
if (Integer.parseInt(line.split("\t")[0]) % 2 == 0) {
parttion = 0;
} else {
parttion = 1;
}
} catch (Exception e) {
continue;
}
producer.send(new ProducerRecord<String, String>("rng_comment", parttion, String.valueOf(parttion), line));
}
bufferedReader.close();
producer.close();
}
}