美文网首页
spark 中创建外部表工具方法

spark 中创建外部表工具方法

作者: 夜空最亮的9星 | 来源:发表于2019-08-28 15:16 被阅读0次
    
    import scala.collection.JavaConversions._
    import java.util
    
    /**
      *
      *
      *
      * def tag_categoryTableColumns() = {
      *
      * val columnsMap = new util.LinkedHashMap[String, String]()
      *     columnsMap.put("tag_id", "string")
      *     columnsMap.put("tag_group", "string")
      *     columnsMap.put("lable_lv1", "string")
      *     columnsMap.put("lable_lv2", "string")
      *     columnsMap.put("lable_lv3", "string")
      *     columnsMap.put("lable_lv4", "string")
      *     columnsMap.put("show_name", "string")
      *
      *
      * columnsMap
      * }
      *
      */
    
    
    object HiveDBUtils {
    
    
      /**
        * 创建外部分区表
        * CREATE EXTERNAL TABLE IF NOT EXISTS app(tag_id string,lable_lv1 string,lable_lv2 string,lable_lv3 string,lable_lv4 string) PARTITIONED BY (year string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LOCATION '/user/app'
        */
    
      def createExternalTableWithPartition(tableName:String,columnsMap :util.LinkedHashMap[String, String],partitionMap:java.util.LinkedHashMap[String, String],location:String,terminator:String) ={
    
        val colums = parseForTableColoums(columnsMap)
    
        val partition = parseForTableColoums(partitionMap)
        val createHqlStatement: String = "CREATE EXTERNAL TABLE IF NOT EXISTS "
          .concat(tableName)
          .concat(colums)
          .concat(" PARTITIONED BY ")
          .concat(partition)
          .concat(" ROW FORMAT DELIMITED FIELDS TERMINATED BY ")
          .concat("'")
          .concat(terminator)
          .concat("'")
          .concat(" LOCATION ")
          .concat("'")
          .concat(location)
          .concat("'")
        createHqlStatement
    
      }
    
    
      /**
        * 创建无分区外部表
        *
        * CREATE EXTERNAL TABLE IF NOT EXISTS app(tag_id string,tag_group string,lable_lv1 string,lable_lv2 string,lable_lv3 string,lable_lv4 string,show_name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY  ','  LOCATION  '/user/app'
        */
    
    
    
    
      def createExternalTableNoPartition(tableName:String,columnsMap :util.Map[String, String],location:String,terminator:String) ={
    
        val columns = parseForTableColoums(columnsMap)
    
        val createHqlStatement: String = "CREATE EXTERNAL TABLE IF NOT EXISTS "
          .concat(tableName)
          .concat(columns)
          .concat(" ROW FORMAT DELIMITED FIELDS TERMINATED BY ")
          .concat(" '")
          .concat(terminator)
          .concat("' ")
          .concat(" LOCATION ")
          .concat(" '")
          .concat(location)
          .concat("' ")
        createHqlStatement
    
      }
    
      def parseForTableColoums(keywords: java.util.Map[String, String]) ={
    
        var keywordsHandled: String = ""
        for ((field, desc) <- keywords) {
    
          keywordsHandled += field
            .concat(" ")
            .concat(keywords.get(field))
            .concat(",")
        }
    
        keywordsHandled = keywordsHandled.reverse.replaceFirst(",", "").reverse
    
        "(".concat(keywordsHandled).concat(")")
    
      }
    
      def parseForTableColoumsName(keywords: java.util.Map[String, String]) = {
    
        var keywordsHandled: String = ""
    
        for ((field, desc) <- keywords) {
          keywordsHandled += field
            .concat(",")
        }
    
        keywordsHandled = keywordsHandled.reverse.replaceFirst(",", "").reverse
    
        " ".concat(keywordsHandled).concat(" ")
    
      }
    
    

    相关文章

      网友评论

          本文标题:spark 中创建外部表工具方法

          本文链接:https://www.haomeiwen.com/subject/mvtmectx.html