更新时间:2023-07-12 来源:黑马程序员 浏览量:

在main()方法中,首先使用filter()算子过滤aggregationRDD每一行数据中行为类型为加入购物车和购买的数据,只保留行为类型为查看的数据,然后使用mapToPair()算子对过滤结果进行转换,获取每一行数据中品类被查看次数和品类ID数据,最终将转换结果加载到getViewCategoryRDD.具体代码如下。
JavaPairRDD<String, Integer> getViewCategoryRDD=aggregationRDD
.filter (new Function<Tuple2<Tuple2<String, String>, Integer>
, Boolean> () {
@override
public Boolean call(Tuple2<Tuple2<String;String>
, Integer> tuple2) throws Exception {
//获取行为类型
String action =tuple2._1._2;
return action.equals("view");
}
}).mapToPair(
new PairFunction<Tuple2<Tuple2<String, String>
, Integer>, String, Integer>() {
@override
public Tuple2<String, Integer>
call(Tuple2<Tuple2<String, String>, Integer> tuple2)
throws Exception {
return new Tuple2<> (tuple2._1._1:tuple2 ._2):
}
});上述代码中,第9行通过equals()方法判断获取的行为类型是否为view(查看)并将判断城果作为返回值,若返回值为True则进行后续转换操作。
在main()方法中,首先使用filter()算子过滤aggregationRDD每一行数据中行为类型为查看和购买的数据,只保留行为类型为加入购物车的数据,然后使用mapToPair()算子对过滤结果进行转换,获取每一行数据中品类被加入购物车次数和品类ID数据,最终将转换结果加载到getCartCategoryRDD,具体代码如下。
JavaPairRDD<String,Integer> getCartCategoryRDD =aggregationRDD
.filter (new Function<Tuple2<Tuple2<String, String>, Integer>
, Boolean>() {
@override
public Boolean call(Tuple2<Tuple2<String, String>
, Integer> tuple2) throws Exception {
String action =tuple2._1._2:
return action.equals("cart"):
}
}).mapToPair(new PairFunction<Tuple2<Tuple2<String, String>
,Integer>,String,Integer>(){
@oerride
public Tuple2<String, Integer>
call (Tuple2<Tuple2<String, String>, Integer> tuple2)
throws Exception {
return new Tuple2<> (tuple2._1._1,tuple2._2);
}
}):上述代码中,第9行通过equals()方法判断获取的行为类型是否为view(查看)并将判断城果作为返回值,若返回值为True则进行后续转换操作。
在main()方法中,首先使用filter()算子过滤aggregationRDD每一行数据中行为类型为查看和购买的数据,只保留行为类型为加入购物车的数据,然后使用mapToPair()算子对过滤结果进行转换,获取每一行数据中品类被加入购物车次数和品类ID数据,最终将转换结果加载到getCartCategoryRDD,具体代码如下。
JavaPairRDD<String,Integer> getCartCategoryRDD =aggregationRDD
.filter(new Function<Tuple2<Tuple2<String, String>, Integer>
, Boolean>() {
@override
public Boolean call(Tuple2<Tuple2<String, String>
, Integer> tuple2) throws Exception {
String action =tuple2._1._2:
return action.equals("cart"):
}
}).mapToPair(new PairFunction<Tuple2<Tuple2<String, String>
,Integer>,String,Integer>(){
@oerride
public Tuple2<String, Integer>
call (Tuple2<Tuple2<String, String>, Integer> tuple2)
throws Exception {
return new Tuple2<> (tuple2._1._1,tuple2._2);
}
});上述代码中,第8行通过equals()方法判断获取的行为类型是否为cart(加入购物车)并将判断结果作为返回值,若返回值为True则进行后续转换操作。
使用filter()算子过滤aggregationRDD每一行数据中行为类型为查看和加入购物车的数据,只保留行为类型为购买的数据,然后使用apToPair()算子对过滤结果进行转换,获取每一行数据中品类被购买次数和品类ID数据,最终将转换结果加载到getPurchaseCategoryRDD,具体代码如下。
上述代码中,第8行通过equals()方法判断获取的行为类型是否为purchase(购买)并将判断结果作为返回值,若返回值为True则进行后续转换操作。