上一篇文章中 我使用 maxmind的免费库开发了一个waterdrop的 插件,测试数据发现,国内的有些市级还是不准确,而且香港并不是显示中国,这就不友好了。

找了一下,发下 ip2region 这个很不错。https://github.com/lionsoul2014/ip2region

我使用这个库,然后使用之前的代码,稍加修改,测试了一下,效果还是不错的。基本没有再出现空的值。

关于查询效率上,如作者所说,memsearch最快,我测试了确实如此,但是会出现一开始的一些spark流的批次效率稍差一些,慢慢地会提上去

package com.student

import io.github.interestinglab.waterdrop.apis.BaseFilter
import com.typesafe.config.{Config, ConfigFactory}
import org.apache.spark.SparkFiles
import org.apache.spark.sql.{Dataset, Row, SparkSession}
import org.apache.spark.sql.functions.{col, udf} import scala.collection.JavaConversions._
import org.lionsoul.ip2region.DbConfig
import org.lionsoul.ip2region.DbSearcher
import scala.collection.JavaConversions._
import org.lionsoul.ip2region.DbConfig
import org.lionsoul.ip2region.DbSearcher object SearcherWrapper extends Serializable {
@transient lazy val searcher = {
val config = new DbConfig
val dbfile = SparkFiles.get("ip2region.db")
val searcher = new DbSearcher(config, dbfile)
searcher
}
} class IP2Region2 extends BaseFilter { var config: Config = ConfigFactory.empty() /**
* Set Config.
**/
override def setConfig(config: Config): Unit = {
this.config = config
} /**
* Get Config.
**/
override def getConfig(): Config = {
this.config
} override def checkConfig(): (Boolean, String) = { val requiredOptions = List("source_field")
val nonExistsOptions: List[(String, Boolean)] = requiredOptions.map { optionName =>
(optionName, config.hasPath(optionName))
}.filter { p =>
!p._2
} if (nonExistsOptions.length == 0) {
(true, "")
} else {
(false, "please specify setting as non-empty string")
} } override def prepare(spark: SparkSession): Unit = { val defaultConfig = ConfigFactory.parseMap(
Map(
"source_field" -> "raw_message",
"target_field" -> "__ROOT__"
)
) config = config.withFallback(defaultConfig) } override def process(spark: SparkSession, df: Dataset[Row]): Dataset[Row] = { val srcField = config.getString("source_field") val ip2region=udf{ip:String => ip2Location2(ip)} import org.apache.spark.sql.functions.split df.withColumn("__region__", ip2region(col(srcField)))
.withColumn("__country__",split(col("__region__"),"\\|")(0))
.withColumn("__province__",split(col("__region__"),"\\|")(2))
.withColumn("__city__",split(col("__region__"),"\\|")(3))
.withColumn("__isp__",split(col("__region__"),"\\|")(4)) } def ip2Location2(ip: String) = {
try {
val searcher = SearcherWrapper.searcher
val response = searcher.memorySearch(ip) response.getRegion
}
catch {
case ex: Exception =>
// ex.printStackTrace()
""
}
} }

geoip ip2region2 with spark的更多相关文章

  1. spark streaming 使用geoIP解析IP

    1.首先将GEOIP放到服务器上,如,/opt/db/geo/GeoLite2-City.mmdb 2.新建scala sbt工程,测试是否可以顺利解析 import java.io.Fileimpo ...

  2. spark geoip

    import java.io.File import scala.io.Source import com.sanoma.cda.geoip.MaxMindIpGeo import com.sanom ...

  3. spark操作geoip的domain数据库

    val ipv4 = sc.textFile("hdfs://hbase11:9000/sparkTsData/GeoIP2-Domain-Blocks-IPv4.csv").ma ...

  4. geoip scala api

    #!/bin/bash /home/hadoop/spark-1.6.2/bin/spark-shell --master spark://hbase11:7077 --executor-memory ...

  5. 使用Java编写并运行Spark应用程序

    我们首先提出这样一个简单的需求: 现在要分析某网站的访问日志信息,统计来自不同IP的用户访问的次数,从而通过Geo信息来获得来访用户所在国家地区分布状况.这里我拿我网站的日志记录行示例,如下所示: 1 ...

  6. Spark踩坑记——Spark Streaming+Kafka

    [TOC] 前言 在WeTest舆情项目中,需要对每天千万级的游戏评论信息进行词频统计,在生产者一端,我们将数据按照每天的拉取时间存入了Kafka当中,而在消费者一端,我们利用了spark strea ...

  7. Spark RDD 核心总结

    摘要: 1.RDD的五大属性 1.1 partitions(分区) 1.2 partitioner(分区方法) 1.3 dependencies(依赖关系) 1.4 compute(获取分区迭代列表) ...

  8. spark处理大规模语料库统计词汇

    最近迷上了spark,写一个专门处理语料库生成词库的项目拿来练练手, github地址:https://github.com/LiuRoy/spark_splitter.代码实现参考wordmaker ...

  9. Hive on Spark安装配置详解(都是坑啊)

    个人主页:http://www.linbingdong.com 简书地址:http://www.jianshu.com/p/a7f75b868568 简介 本文主要记录如何安装配置Hive on Sp ...

随机推荐

  1. Servlet过虑器

    过滤器是在请求的预处理和后处理时调用的对象. 主要用于执行转换,日志记录,压缩,加解密,输入验证等过滤任务. servlet过滤器是可插拔的,即它在web.xml文件中定义,如果从web.xml文件中 ...

  2. Gartner:阿里云位列全球云数据库市场份额前三,数据库未来需上云

    近日,国际权威研究机构Gartner发布 <The Future of the Database Management System (DBMS) Market Is Cloud>报告,鲜 ...

  3. 用div漂浮快实现与表单无关的多文件上传功能。

    我项目有这个需求,多文件上传,而且要及时显示到表单上,这样的话就不能与表单相关. 由于我对前端不熟,我就实现了这么一个功能,通过button触发一个div漂浮块,然后多文件上传,之后通过js把文件名显 ...

  4. jmeter循环和计数器

  5. 使用 EnumWindows 找到满足你要求的窗口 - walterlv

    原文:使用 EnumWindows 找到满足你要求的窗口 - walterlv 使用 EnumWindows 找到满足你要求的窗口 2019-04-30 13:11 在 Windows 应用开发中,如 ...

  6. python 数据的写入

  7. 树状数组(Binary Index Tree)

    一维BIT(单点更新,区间求和): Problem - 1166 #include <iostream> #include <algorithm> #include <c ...

  8. 【阿里云新品发布·周刊】第13期:链路追踪 Tracing Analysis 商业化首发

    点击订阅新品发布会! 新产品.新版本.新技术.新功能.价格调整,评论在下方,下期更新!关注更多内容,了解更多 最新发布 链路追踪 Tracing Analysis 商业化首发 2019年6月12日15 ...

  9. 只要是使用函数file_get_contents访问 https 的网站都要开启

    使用file_get_contents();报错failed to open stream: Unable to find the socket transport "ssl" - ...

  10. cmd导入比较大的sql脚本

    osql -S jack_c -d yourdb -U sa -P 123 -i E:\user.sql 注意: sql脚本里面一定要先创建数据库或者use到某个数据库,然后再cmd执行脚本