spark1.x-spark-sql-数据倾斜解决方案

聚合源数据

过滤导致倾斜的key where条件

提高shuffle并行度 spark.sql.shuffle.partitions

  sqlContext.setConf("spark.sql.shuffle.partitions","1000")
   // 默认的并行度 为 200 reducetask只有200

双重group by 改写SQL 改成两次Group by

给某个字段加上随机前缀 random_prefix()
实现UDAF
call(String,Integer)
randNum+”_”+val
局部聚合 去掉随机前缀
拿到值
再进行一次 全局的聚合
多Key RDD 拆开了
在映射成一张表

reduce join 转换为map jon

将表做成 RDD 手动去实现 mapjoin
SPark sql内置的map join
默认有一个小表 在10M以内
默认就会将该表进行broadcast 然后执行map join
调节这个阈值 20M 50M 甚至1G

    sqlContext.setConf("spark.sql.autoBroadcastJoinThreshold", "20971520");

采样倾斜Key并单独进行join

强spark-core
随机key与扩容表
sparksql 转化为sparkcore
product info 加上随机数 进行扩容10倍
sql做子查询

        JavaRDD<Row> rdd = sqlContext.sql("select * from product_info").javaRDD();
        JavaRDD<Row> flattedRDD = rdd.flatMap(new FlatMapFunction<Row, Row>() {

            private static final long serialVersionUID = 1L;

            @Override
            public Iterable<Row> call(Row row) throws Exception {
                List<Row> list = new ArrayList<Row>();

                for(int i = 0; i < 10; i ++) {
                    long productid = row.getLong(0);
                    String _productid = i + "_" + productid;

                    Row _row = RowFactory.create(_productid, row.get(1), row.get(2));
                    list.add(_row);
                }

                return list;
            }

        });

        StructType _schema = DataTypes.createStructType(Arrays.asList(
                DataTypes.createStructField("product_id", DataTypes.StringType, true),
                DataTypes.createStructField("product_name", DataTypes.StringType, true),
                DataTypes.createStructField("product_status", DataTypes.StringType, true)));

        DataFrame _df = sqlContext.createDataFrame(flattedRDD, _schema);
        _df.registerTempTable("tmp_product_info");

        String _sql =
                "SELECT "
                    + "tapcc.area,"
                    + "remove_random_prefix(tapcc.product_id) product_id,"
                    + "tapcc.click_count,"
                    + "tapcc.city_infos,"
                    + "pi.product_name,"
                    + "if(get_json_object(pi.extend_info,'product_status')=0,'自营商品','第三方商品') product_status "
                + "FROM ("
                    + "SELECT "
                        + "area,"
                        + "random_prefix(product_id, 10) product_id,"
                        + "click_count,"
                        + "city_infos "
                    + "FROM tmp_area_product_click_count "
                + ") tapcc "
                + "JOIN tmp_product_info pi ON tapcc.product_id=pi.product_id ";
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值