DbSearcher解析ip地址

1.maven导入
<dependency>
            <groupId>org.lionsoul</groupId>
            <artifactId>ip2region</artifactId>
            <version>1.7.2</version>
</dependency>
2.编写代码
//db地址使用hdfs地址
val ipSearch = new DbSearcher(new DbConfig(),"/ip2region.db")
val region: String = ipSearch.binarySearch("127.0.0.1").getRegion
3.下载ip地址库

码云下载地址:https://gitee.com/lionsoul/ip2region/blob/master/data/ip2region.db

4.spark广播文件或者文件夹
val path = "/user/iteblog/ip.txt"
//如果path是一个目录,那么我们可以设置recursive为true
sc.addFile(path)
valrdd =sc.parallelize((0 to 10))
rdd.foreach{
     index =>
    val path = SparkFiles.get(path)
}

你可能感兴趣的:(大数据,scala)