《数据算法-Hadoop/Spark大数据处理技巧》读书笔记(五)——购物篮分析

本文是《数据算法-Hadoop/Spark大数据处理技巧》的读书笔记,重点介绍了购物篮分析的方法。首先解释了购物篮分析的原理,通过购物小票数据进行模式长度为3的商品组合枚举,接着进行数据拆分和分组,最后计算商品之间的关联度,例如:[[a, b], [d], 0.5], [c], [b], 1.0]等。" 6742547,207172,Linux中使用Wine安装及解决QQ2010问题,"['Linux', 'Wine', 'QQ', '软件安装', '问题解决']

这个购物篮分析要先理解他在算什么,我的理解是通过购物小票上的购物信息,在指定规则下穷举全部的购物关联性。
1.模型解释
①数据源
a、b、c、d分别代表一种商品,超市的购物小票数据如下(为了简单起见,取4条小票数据),每个小票中的商品必须按照一定的顺序进行排列,否则会出现重复计算的情况:
a,b,c
a,b,d
b,c
b,c
②定义模式长度为3,即最多只考虑3种商品的购买管理关系,在这样的条件下,枚举小票中所有存在的组合。在这个条件下,第一条就可以把[a,b,c]这张小票拆除如下可能:
[]
[a]
[b]
[c]
[a,b]
[a,c]
[b,c]
[a,b,c]
这步操作的实质就是对一张小票上的商品列表(排序过的),取全部项数小于等于3的组合。对所有小票执行这步操作之后且归并技术后,统计这些组合的出现次数:
([a, b],2)
([a, b, d],1)
([c],3)
([b, d],1)
([d],1)
([a],2)
([b, c],3)
([a, b, c],1)
([a, c],1)
([a, d],1)
([b],4)
③此时,已完成了对购物小票的第一次分组,然后,在此基础上对结果集再次进行模式拆分,拆分的模式key>=n-1。
即对([a,b],2)拆分为
([a,b],(null,2))
([a],([a,b],2))
([b],([a,b],2))
对([a,b,d],1)拆分为
([a,b,d],(null,1))
([a,b],([a,b,d],1))
([a,d],([a,b,d],1))
([b,d],([a,b,d],1))
其中,标为null的是此组合出现的总数。按照此规则的拆分结果为
([a],([a, b],2))
([b],([a, b],2))
([a, b],(null,2))
([a, b],([a, b, d],1))
([a, d],([a, b, d],1))
([b, d],([a, b, d],1))
([a, b, d],(null,1))
([c],(null,3))
([b],([b, d],1))
([d],([b, d],1))
([b, d],(null,1))
([d],(null,1))
([a],(null,2))
([b],([b, c],3))
([c],([b, c],3))
([b, c],(null,3))
([a, b],([a, b, c],1))
([a, c],([a, b, c],1))
([b, c],([a, b, c],1))
([a, b, c],(null,1))
([a],([a, c],1))
([c],([a, c],1))
([a, c],(null,1))
([a],([a, d],1))
([d],([a, d],1))
([a, d],(null,1))
([b],(null,4))
⑤然后进行groupByKey操作
⑥然后以此计算出key和其他商品的关联度。
[[a, b],[d],0.5),[a, b],[c],0.5)]
[[c],[b],1.0),[c],[a],0.3333333333333333)]
[[b, d],[a],1.0)]
[[d],[b],1.0),[d],[a],1.0)]
[[a],[b],1.0),[a],[c],0.5),[a],[d],0.5)]
[[b, c],[a],0.3333333333333333)]
[[a, c],[b],1.0)]
[[a, d],[b],1.0)]
[[b],[a],0.5),[b],[d],0.25),[b],[c],0.75)]
Driver源码:

package org.dataalgorithms.MyImplementation;


import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.*;
import org.dataalgorithms.utils.Combination;
import org.dataalgorithms.utils.Constants;
import scala.Tuple2;
import scala.Tuple3;

import java.util.*;

/**
 * Created by yang.liu on 2018/2/2.
 */
public class MarketBasketAnalyzeDriver {
   
   
    public static void main(final String[] args){
        final SparkConf conf = new SparkConf().setAppName("MBA").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(conf);
        JavaRDD<String> lines = sc.textFile(Constants.INPUT_PATH_PREFIX + "/MarketBasketAnalyze.txt");
        JavaPairRDD<List<String>, Integer> firstTransformRDD = lines.flatMap(new FlatMapFunction<String, List<String>>() {
            @Override
            public Iterable<List<String>> call(String line) throws Exception {
                String[] array = line.split(",");
                return Combination.findSortedCombanations(Arrays.asList(array), 3);
            }
        }).mapToPair(new PairFunction<List<String>, List<String>, Integer>() {
            @Override
            public Tuple2<List<String>, Integer> call(List<String> strings) throws Exception {
                
数据算法--HadoopSpark数据处理技巧 data algorithms部分主题⾃写scala程序实现 SecondarySort (chapter 1) data 2015,1,1,10 2015,1,2,11 2015,1,3,12 2015,1,4,13 2015,2,1,22 2015,2,2,23 2015,2,3,24 2015,2,4,25 2015,3,1,20 2015,3,2,21 2015,3,3,22 2015,3,4,23 2015,3,4,23 2015,1,3,12 2015,2,2,23 code import org.apache.spark.sql.SparkSession import org.apache.spark.{Partitioner, SparkConf} class SecondarySortPartitioner(val v: Int) extends Partitioner { override def numPartitions: Int = { v } override def getPartition(key: Any): Int = key match { case (k: String, v: Int) => math.abs(k.hashCode % numPartitions) case null => 0 case _ => math.abs(key.hashCode % numPartitions) } } object SecondarySort { def main(args: Array[String]): Unit = { System.setProperty("hadoop.home.dir", "C:\\winutils-master\\hadoop-2.6.3") val conf = new SparkConf().setMaster("local").setAppName("SecondarySort") val context = SparkSession.builder().config(conf).getOrCreate().sparkContext val rdd = context.textFile("C:\\Users\\IdeaProjects\\spark_learning\\test.txt") val step1 = rdd.map(line => line.split(",")) .map(line => ((line(0) + "-" + line(1), line(3).toInt), line(3).toInt)) val step2 = step1.repartitionAndSortWithinPartitions(new SecondarySortPartitioner(4)) .map { case (k, v: Int) => (k._1, v.toString) }.reduceByKey((x, y) => x + "," + y) step2.foreach(println) } } CommonFriends (chapter 8) data 100,200 300 400 500 600 200,100 300 400 300,100 200 400 500 400,100 200 300 500,100 300 600,100 code import org.apache.spark.{HashPartitioner, SparkConf} import org.apache.spark.sql.SparkSession import scala.collection.mutable.ArrayBuffer object CommonFriends { def main(args: Array[String]): Unit = { System.setProperty("hadoop.home.dir", "C:\\winutils-master\\hadoop-2.6.3") val conf = new SparkConf().setMaster("local").setAppName("CommonFriends") val spark = SparkSession.builder().config(conf).getOrCreate() import spark.implicits._ val context = SparkSession.builder().config(conf).getOrCreate().sparkContext val rdd = context
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值