kudu 查看元数据信息

时间:2021-08-20 14:34:40
package com.lala.lala.pipe.dbinfo

import org.apache.kudu.client.KuduClient

import com.lala.lala.common.query.option.KuduOptions
import collection.JavaConverters._
import scala.util.Try

class KuduShow(map: Map[String, String]) {
  private val master = map(KuduOptions.KUDU_MASTER)
  private lazy val tableName = map(KuduOptions.TBL)
  private val client = KuduShow.getClient(master)

  def getTbs: String = Try(client.getTablesList.getTablesList.asScala.mkString(",")).getOrElse("master错误或网络不可用")

  def getCols: String = {
    val table = client.openTable(tableName)
    table.getSchema
      .getColumns.asScala
      .map(col => s"${col.getName}:${col.getType.getName}")
      .mkString(",")
  }
}

object KuduShow {
  var client: KuduClient = null

  def getClient(master: String): KuduClient = {
    if (client == null || master != client.getMasterAddressesAsString) {
      client = new KuduClient.KuduClientBuilder(master).build
    }
    client
  }

}