使用scala将base64解码为ascii

thtygnil  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(730)

我有一个hbase表,它为其中一列获取base64值。我正在用所有列的数据类型作为字符串读取自定义模式中的hbase表。我想将spark dataframe中的相同值解码为ascii,并用dataframe中列的较早编码值替换该值。如何做到这一点?
Dataframe值(输入):

rO0ABXQAUk9ybmF0ZSBlbWJlbGxpc2htZW50cyBwZXJmZ
WN0bHkgY29tcGxlbWVudCB0aGUgb2gtc28tY29tZm9ydGFibGUgcm9wZS1saWtlIGZvb3RiZWQ=

ascii值(需要输出):

tROrnate embellishments perfectly complement the oh-so-comfortable rope-like footbed
7ajki6be

7ajki6be1#

您可以在scala类中使用java编解码api

// Base64 encode
  val text = "tROrnate embellishments perfectly complement the oh-so-comfortable rope-like footbed"
  val bytesEncoded = java.util.Base64.getEncoder.encode(text.getBytes())
  println(bytesEncoded)

  // Base64 decode
  val bytesToDecode="rO0ABXQAUk9ybmF0ZSBlbWJlbGxpc2htZW50cyBwZXJmZWN0bHkgY29tcGxlbWVudCB0aGUgb2gtc28tY29tZm9ydGFibGUgcm9wZS1saWtlIGZvb3RiZWQ="
  val textDecoded = new String(java.util.Base64.getDecoder.decode(bytesToDecode))
  println(textDecoded)

相关问题