在hive中添加加密udf,测试报错:
select encrypt_sm2("aa","04AD9356466C7A505B3B2E18F2484E1F096108FA19C0F61C707A808EDF7C132BC3CE33E63D2CC6D77FB0A172004F8F5282CEADE22ED9628A02FE8FD85AF1EFE8B3");
Error: Error while compiling statement: FAILED: SemanticException [Error 10014]: Line 1:7 Wrong arguments '"04AD9356466C7A505B3B2E18F2484E1F096108FA19C0F61C707A808EDF7C132BC3CE33E63D2CC6D77FB0A172004F8F5282CEADE22ED9628A02FE8FD85AF1EFE8B3"': org.apache.hadoop.hive.ql.metadata.HiveException: Unable to execute method public java.lang.String com.hive.data.encrypt.sm2.EncryptSm2.evaluate(java.lang.String,java.lang.String):tried to access method org.bouncycastle.math.ec.ECPoint$AbstractFp.<init>(Lorg/bouncycastle/math/ec/ECCurve;Lorg/bouncycastle/math/ec/ECFieldElement;Lorg/bouncycastle/math/ec/ECFieldElement;)V from class com.hive.data.encrypt.sm2.SM2 (state=42000,code=10014)
加密算法包版本:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.57</version>
</dependency>
根据异常,发现方法调用没权限,本地测试是正常的,猜测应该是包冲突了;
linux上查看是否有其他jar包:
locate bcprov
发现hadoop lib里有该jar包,版本不一致,变更版本后,有些方法不兼容了,遂查询了下是否兼容两个包的方法;
找了下,可以变更下包名,兼容加载:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<!-- 改为true -->
<createDependencyReducedPom>true</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<!-- Maven 的生命周期 -->
<phase>package</phase>
<goals>
<!-- 插件目标 -->
<goal>shade</goal>
</goals>
<configuration>
<!-- <minimizeJar>true</minimizeJar>-->
<!-- 配置多版本 jar 包中类路径的重命名
-->
<relocations>
<relocation>
<pattern>org.bouncycastle</pattern>
<shadedPattern>org.new.bouncycastle</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
参考:
【复盘】记录一次加解密包遇到的坑,tried to access method org.bouncycastle.math_ecpoint.fp 1.68版本_qxlxi的博客-CSDN博客
https://qxlxi.blog.csdn.net/article/details/123342190?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_default&utm_relevant_index=2