hive的常见数据处理方法
下面函数依赖的包
import org.apache.hadoop.fs.Path
import org.apache.spark.sql.SparkSession
- 获取hive表的所有字段
def getTableCols(spark: SparkSession, table: String): List[String] ={
spark.sql(s"select * from ${table} limit 1").columns.toList
}
//方法2
def getHiveTableFieldList(sparkSession: SparkSession, table: String): List[String] = {
sparkSession.sql(s"select * from $table").head.schema.fieldNames.toList
}
- 获取hive表的最新分区健
def getHiveTablePartiton(sparkSession: SparkSession, table: String): (String, String) = {
val df = sparkSession.sql(s"SHOW PARTITIONS $table")
val partiton = df.sort(desc("partition")).take(1).head
val partitonSplit = partiton.get(0).toString.split("=")
(partitonSplit(0), partitonSplit(1))
}
- 只保存 相同groupKey下,按field降序后的第一个记录