44-1.Spark大型电商项目-用户访问session分析-top10热门品类之获取session访问过的所有品类

目录

代码

UserVisitSessionAnlyzeSpark.java


本篇文章将记录用户访问session分析-top10热门品类之获取session访问过的所有品类。

代码

UserVisitSessionAnlyzeSpark.java

	/**
	 * 获取top10热门品类
	 * @param filteredSessionid2AggrInfoRDD
	 * @param sessionid2actionRDD
	 */
	private static void getTop10Category(  
			JavaPairRDD filteredSessionid2AggrInfoRDD,
			JavaPairRDD sessionid2actionRDD) {
		/**
		 * 第一步:获取符合条件的session访问过的所有品类
		 */
		
		// 获取符合条件的session的访问明细
		JavaPairRDD sessionid2detailRDD = filteredSessionid2AggrInfoRDD
				.join(sessionid2actionRDD)
				.mapToPair(new PairFunction>, String, Row>() {
		
					private static final long serialVersionUID = 1L;

					@Override
					public Tuple2 call(
							Tuple2> tuple) throws Exception {
						return new Tuple2(tuple._1, tuple._2._2);
					}
					
				});
		
		// 获取session访问过的所有品类id
		// 访问过:指的是,点击过、下单过、支付过的品类
		JavaPairRDD categoryidRDD = sessionid2detailRDD.flatMapToPair(
				
				new PairFlatMapFunction, Long, Long>() {

					private static final long serialVersionUID = 1L;

					@Override
					public Iterable> call(
							Tuple2 tuple) throws Exception {
						Row row = tuple._2;
						
						List> list = new ArrayList>();
						
						Long clickCategoryId = row.getLong(6);
						if(clickCategoryId != null) {
							list.add(new Tuple2(clickCategoryId, clickCategoryId));   
						}
						
						String orderCategoryIds = row.getString(8);
						if(orderCategoryIds != null) {
							String[] orderCategoryIdsSplited = orderCategoryIds.split(",");  
							for(String orderCategoryId : orderCategoryIdsSplited) {
								list.add(new Tuple2(Long.valueOf(orderCategoryId),
										Long.valueOf(orderCategoryId)));
							}
						}
						
						String payCategoryIds = row.getString(10);
						if(payCategoryIds != null) {
							String[] payCategoryIdsSplited = payCategoryIds.split(",");  
							for(String payCategoryId : payCategoryIdsSplited) {
								list.add(new Tuple2(Long.valueOf(payCategoryId),
										Long.valueOf(payCategoryId)));
							}
						}
						
						return list;
					}
					
				});
	}

 

你可能感兴趣的:(大数据,spark,电商用户行为分析)