HbaseResultHelper.scala 960 B

123456789101112131415161718192021222324252627282930313233
  1. package com.winhc.bigdata.flink.implicits
  2. import org.apache.hadoop.hbase.client.Result
  3. import org.apache.hadoop.hbase.util.Bytes
  4. import scala.collection.JavaConverters._
  5. import scala.collection.mutable.Map
  6. /**
  7. * @author: XuJiakai
  8. * @date: 2021/8/31 17:29
  9. */
  10. case class HbaseResultHelper(result: Result) {
  11. def toJsonString(): String = {
  12. if (result == null || result.isEmpty) {
  13. null
  14. } else {
  15. val map: Map[String, String] = Map()
  16. var rowkey: String = null
  17. for (cell <- result.listCells().asScala) {
  18. if (rowkey == null) {
  19. rowkey = Bytes.toString(cell.getRowArray, cell.getRowOffset, cell.getRowLength)
  20. }
  21. val key = Bytes.toString(cell.getQualifierArray, cell.getQualifierOffset, cell.getQualifierLength)
  22. val v = Bytes.toString(cell.getValueArray, cell.getValueOffset, cell.getValueLength)
  23. map += key-> v
  24. }
  25. map += "rowkey" -> rowkey
  26. map.toJson
  27. }
  28. }
  29. }