数仓项目中geohash字典的构建(下)

数仓项目中geohash字典的构建(下)

剩下将经纬转成geohash码mysql弄不了只能用spark了
/**
*

  • 将sql中写好的经纬度字段在spark中生成geohash编码
  • 预备:在子模块中传入geohash的依赖
  • 1.读取mysql中geohasg/tmp 表格
  • 2.使用GeoHash.geoHashStringWithCharacterPrecision(lat,lng,5)算法转换
  • 3.保存结果
  • 4.关流
    */
object dict {
  def main(args: Array[String]): Unit = {
    //构造spark(创建一个spark会话)
    val spark: SparkSession = SparkSession.builder()
      .appName(this.getClass.getSimpleName)
      .master("local[*]")
      .getOrCreate()
import spark.implicits._
    //读取mysql
    //new 一个mysql性能
    val pro = new Properties()
    pro.setProperty("user","root")
    pro.setProperty("password","123456")
    //jdbc参数(地址,表名,属性)
    val df: DataFrame = spark.read.jdbc("jdbc:mysql://doit01:3306/mysql","tmp",pro)

    //转整geohash编码
    //最后再用var res接收一下!
    var res =df.map(row=>{

      //取出这一行的经纬度
      val lng: Double = row.getAs[Double]("lng")
      val lat: Double = row.getAs[Double]("lat")
      val province: String = row.getAs[String]("province")
      val city: String = row.getAs[String]("city")
      val district: String = row.getAs[String]("district")

      //调用算法(最长的那个方法)
    val geohash: String = GeoHash.geoHashStringWithCharacterPrecision(lat,lng,5)
      //组装返回结果
      (geohash,province,city,district)
    }).toDF("gro","province","city","district")

    //保存结果
    res.write.parquet("data/dict/geo_dict/output")


    spark.stop()
  }
}

注意:在这个编译的过程中可能会有两个报错
1.NoSuchMethodError
找不到这个方法错误
多为版本冲突导致的
2.No suitable driver
没有合适的驱动
少一个依赖或者少一个jar包

发布了48 篇原创文章 · 获赞 11 · 访问量 1518

猜你喜欢

转载自blog.csdn.net/weixin_45896475/article/details/104272669