0

I have written Hive UDF in Java for decoding the information, for that we used the below code.

public Text evaluate(Text str) throws Exception {
        byte[] keyBytes = (SALT + KEY).getBytes("UTF8");
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        keyBytes = messageDigest.digest(keyBytes);
        keyBytes = java.util.Arrays.copyOf(keyBytes, 16);
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        String decryptedString = Base64.encodeBase64String(cipher.doFinal(str.getBytes()));
        return new Text(decryptedString);
    }

The below query is executed successfully with UDF.

Hive Query to check UDF is working

below is the Gender table where I'm executing the UDF

Whole Gender table

When I execute the query on the gender table with UDF, I'm getting the null and new line in it. Please find the below screenshot with highlighted areas. Query with UDF

I'm expecting the result in one line only. Please help and let me know where I'm wrong.

Sachin
  • 184
  • 1
  • 1
  • 7
  • Try to replace `Text` with `String`. Also, what happens if you run the following query `select ToDeidentId(gender) from demo.gender`? – serge_k Jun 30 '20 at 17:15
  • Can you try to do same in Hive console, not Hue UI? To make sure it's not a bug from Hue's side – GoodDok Jun 30 '20 at 20:01
  • @serge_k `select ToDeidentId(gender) from demo.gender` show the same result with newline – Sachin Jul 01 '20 at 06:30
  • @GoodDok I tried the Hive console as well result is the same (Extra column and new line) – Sachin Jul 01 '20 at 06:31
  • How about replacing return type `Text` to `String` and returning just `decryptedString`? does it help? – serge_k Jul 01 '20 at 15:42
  • When I changed the execution engine to Spark: set hive.execution.engine=spark; then the above query ran successfully. Does anyone know the reason of this behavior? – Sachin Jul 28 '20 at 16:45

0 Answers0