Hive 中自定义函数实现墨卡托和经纬度相互转换

package com.cloudera.udf;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import java.text.DecimalFormat;


/**
 * 经纬度和墨卡托相互转换
 */
 @Description(name = "mercator_lnglat_convert",
        value = "mercator_lnglat_convert(String a , String b, boolean flag) ==> return result ",
        extended = "Example:\n"
                + " mercator_lnglat_convert(\"12727039.383734727\", \"3579066.6894065146\") ==> \"114.3289400||30.5857480\" \n"
                + " mercator_lnglat_convert(\"12727039.383734727\", \"3579066.6894065146\", true) ==> \"114.3289400||30.5857480\" \n"
                + " mercator_lnglat_convert(\"12727039.383734727\", \"3579066.6894065146\", 7) ==> \"114.3289400||30.5857480\" \n"
                + " mercator_lnglat_convert(\"12727039.383734727\", \"3579066.6894065146\", true, 9) ==> \"114.328940016||30.585748004\" \n"
                + " mercator_lnglat_convert(\"114.32894\", \"30.585748\", false, 9) ==> \"12727039.383734727||3404789.892891386\";"
)
public class MercatorAndLngLatInterconversion extends UDF {

    /**
     * 地球半径
     */
    private final static double EARTH_RADIUS = 6378137.0;

    private final static double EARTH_SEMI_PERIMETER = 20037508.34;

    private final static int DEFAULT_LEN = 7;

    private final Text result = new Text();

    /**
     * @param flag  true 为经纬度转墨卡托,false  为墨卡托转经纬度
     * @param num   保留小数点后面的位数
     */
    public Text evaluate(String value1, String value2, boolean flag, int num) {
        String x = "";
        String y = "";
        try {
            String pattern = "#." + String.format("%0" + num + "d", 0);
            DecimalFormat format = new DecimalFormat(pattern);
            if (flag) {
                x = format.format(getLng(Double.valueOf(value1)));
                y = format.format(getLat(Double.valueOf(value2)));
            } else {
                x = format.format(getMercatorX(Double.valueOf(value1)));
                y = format.format(getMercatorX(Double.valueOf(value2)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.set(x + "||" + y);
        return result;
    }

    public Text evaluate(String value1, String value2) {
        return evaluate(value1, value2, true, DEFAULT_LEN);
    }

    public Text evaluate(String value1, String value2, int num) {
        return evaluate(value1, value2, true, num);
    }
    public Text evaluate(String value1, String value2, boolean flag) {
        return evaluate(value1, value2, flag, DEFAULT_LEN);
    }

    /**
     * 经度转墨卡托坐标 X
     */
    private double getMercatorX(double lng) {
        double mercator_X = lng * Math.PI / 180 * EARTH_RADIUS;
        return mercator_X;
    }

    /**
     * 纬度转墨卡托坐标 Y
     */
    private double getMercatorY(double lat) {
        double a = lat * Math.PI / 180;
        double mercator_Y = EARTH_RADIUS / 2 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
        return mercator_Y;
    }

    /**
     * 墨卡托转经度
     */
    private double getLng(double poi_X) {
        double lng = poi_X / EARTH_SEMI_PERIMETER * 180;
        return lng;
    }

    /**
     * 墨卡托转纬度
     */
    private double getLat(double poi_Y) {
        double mmy = poi_Y / EARTH_SEMI_PERIMETER * 180;
        double lat = 180 / Math.PI * (2 * Math.atan(Math.exp(mmy * Math.PI / 180)) - Math.PI / 2);
        return lat;
    }
}

具体使用方法参考:https://blog.csdn.net/weixin_43215250/article/details/84837350

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Hive实现RSA加密和解密的自定义函数,你需要使用Hive的UDF(用户定义的函数)功能,并结合Java的RSA加密和解密算法。 下面是一个基本的示例,展示如何在Hive实现RSA加密和解密的自定义函数: 1. 创建一个Java类,例如`RSACrypto.java`,实现RSA加密和解密的逻辑。 ```javaimport java.security.*; import javax.crypto.*; import java.util.Base64; public class RSACrypto { public static String encrypt(String plainText, String publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey))); cipher.init(Cipher.ENCRYPT_MODE, pubKey); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } public static String decrypt(String encryptedText, String privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey))); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText)); return new String(decryptedBytes); } } ``` 2. 编译`RSACrypto.java`文件,并将生成的`.class`文件打包成`jar`文件。 ```shelljavac RSACrypto.javajar cf rsacrypto.jar RSACrypto.class``` 3. 将`rsacrypto.jar`文件上传到Hive的服务器上。 4. 在Hive创建一个函数,使用`CREATE FUNCTION`语句。 ```sqlCREATE FUNCTION rsa_encrypt AS 'com.example.RSACrypto' USING JAR 'hdfs:///path/to/rsacrypto.jar'; CREATE FUNCTION rsa_decrypt AS 'com.example.RSACrypto' USING JAR 'hdfs:///path/to/rsacrypto.jar'; ``` 5. 使用自定义函数进行RSA加密和解密。 ```sqlSELECT rsa_encrypt('Hello World', '<public_key>') AS encrypted_text; SELECT rsa_decrypt('<encrypted_text>', '<private_key>') AS decrypted_text; ``` 请确保替换`<public_key>`和`<private_key>`为实际的RSA公钥和私钥。还要注意,这只是一个简单的示例,实际上,您可能需要处理更复杂的情况,例如密钥管理和编码/解码方式等。 希望这个示例能帮助您实现Hive使用自定义函数进行RSA加密和解密。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值