spark自定义函数

背景

在日常使用spark的过程中,我们经常会使用到各种自定义的函数,目的是为了更好的对数据进行各种处理,本文就简答的记录一些spark中的自定义函数

spark 自定义函数

首先hive的数据类型binary字节数组对应使用java写用户自定义函数udf时的数据类型是BytesWritable,下面我们写几个自定义的函数,作为练习用
// 1.Array数组包含函数

        /**
         * 判断source数组是否包含全部的目标dest数组的元素
         * */
        public class ArrayContainsUDF extends UDF {

            public boolean evaluate(ArrayList<String> sourceArray, ArrayList<String> destArray) {
                if (null == destArray || destArray.size() == 0) {
                    return false;
                }
                if (null == sourceArray || sourceArray.size() == 0) {
                    return false;
                }
                return sourceArray.containsAll(destArray);
            }
        }

// 2.binary数组相与并返回结果的binary


        /**
         * 获取两个字节数组的每个字节相与的结果数组
         * */
        public class BinaryAndUDF extends UDF {

            public BytesWritable evaluate(BytesWritable sourceBytes, BytesWritable destBytes) {
                if (null == sourceBytes || sourceBytes.getLength() == 0) {
                    return null;
                }
                if (null == destBytes || destBytes.getLength() == 0) {
                    return null;
                }
                if(sourceBytes.getLength() != destBytes.getLength()){
                    return null;
                }
                byte[] sourceArr = sourceBytes.getBytes();
                byte[] destArr = destBytes.getBytes();
                byte[] result = new byte[sourceBytes.getLength()];
                for (int i = 0; i < sourceArr.getLength(); i++) {
                    result[i] = sourceArr[i] & destArr[i];
                }
                return new BytesWritable(result);
            }
        }

// 3.binary数组相或并返回结果的binary

        /**
         * 获取两个字节数组的每个字节相或的结果数组
         * */
        public class BinaryOrUDF extends UDF {

            public BytesWritable evaluate(BytesWritable sourceBytes, BytesWritable destBytes) {
                if (null == sourceBytes || sourceBytes.getLength() == 0) {
                    return null;
                }
                if (null == destBytes || destBytes.getLength() == 0) {
                    return null;
                }
                if(sourceBytes.getLength() != destBytes.getLength()){
                    return null;
                }
                byte[] sourceArr = sourceBytes.getBytes();
                byte[] destArr = destBytes.getBytes();
                byte[] result = new byte[sourceBytes.getLength()];
                for (int i = 0; i < sourceArr.getLength(); i++) {
                    result[i] = sourceArr[i] | destArr[i];
                }
                return new BytesWritable(result);
            }
        }

// 4.Array数组转换成Bitmap结构的binary

        /**
         * Bitmap的字节数组转成对应的Array[INT]
         * */
        public class BitmapBinaryToArrayUDF extends UDF {

            public ArrayList<Integer> evaluate(BytesWritable bitmapBinary) {
                if(null == bitmapBinary || bitmapBinary.getLength() == 0) {
                    return null;
                }
                RoaringBitmap roaringBitmap = bytesArrToRoaringbitmap(bitmap.getBytes());
                ArrayList<Integer> list = Lists.newArrayList(roaringBitmap.iterator());
                return list;
            }

        }

// 5.Bitmap结构的binary类型转换成对应的Array


    /**
     * Array【INT】数组转成bitmap的binary字节数组
     * */
    public class ArrayToBitmapUDF extends UDF {

        public BytesWritable evaluate(ArrayList<Integer> array) {
            if (null == array || array.size() == 0) {
                return null;
            }
            RoaringBitmap roaringBitmap = new RoaringBitmap();
            for (Integer val : array) {
                roaringBitmap.add(val.intValue());
            }
            return new BytesWritable(roaringBitmap2Bytes(roaringBitmap));
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值