【问题标题】:Accessing Maxmind Geo API in Hadoop using Distributed Cache使用分布式缓存访问 Hadoop 中的 Maxmind Geo API
【发布时间】:2014-08-07 22:14:09
【问题描述】:

我正在编写一个 MapReduce 作业来分析 Web 日志。我的代码旨在将 IP 地址映射到地理位置,为此我使用 Maxmind Geo API(https://github.com/maxmind/geoip-api-java)。我的代码有一个 LookupService 方法,该方法需要带有 ip 的数据库文件到位置匹配。我正在尝试使用分布式缓存传递此数据库文件。我尝试了两种不同的方式

案例1:

运行从 HDFS 传递文件的作业,但它总是抛出错误“FILE NOT FOUND

sudo -u hdfs hadoop jar \
 WebLogProcessing-0.0.1-SNAPSHOT-jar-with-dependencies.jar \
GeoLocationDatasetDriver /user/hdfs/input /user/hdfs/out_put \
/user/hdfs/GeoLiteCity.dat 

sudo -u hdfs hadoop jar \
WebLogProcessing-0.0.1-SNAPSHOT-jar-with-dependencies.jar \
GeoLocationDatasetDriver /user/hdfs/input /user/hdfs/out_put \
hdfs://sandbox.hortonworks.com:8020/user/hdfs/GeoLiteCity.dat

驱动类代码:

Configuration conf = getConf();
Job job = Job.getInstance(conf);
job.addCacheFile(new Path(args[2]).toUri()); 

映射器类代码:

public void setup(Context context) throws IOException
{
URI[] uriList = context.getCacheFiles();
Path database_path = new Path(uriList[0].toString());
LookupService cl = new LookupService(database_path.toString(),
            LookupService.GEOIP_MEMORY_CACHE | LookupService.GEOIP_CHECK_CACHE);
}

案例 2: 通过 -files 选项从本地文件系统传递文件来运行代码。 Error: Null Pointer exception 在 LookupService cl = new LookupService(database_path)

行中
sudo -u hdfs hadoop jar  \
WebLogProcessing-0.0.1-SNAPSHOT-jar-with-dependencies.jar \
com.prithvi.mapreduce.logprocessing.ipgeo.GeoLocationDatasetDriver \
-files /tmp/jobs/GeoLiteCity.dat /user/hdfs/input /user/hdfs/out_put \
GeoLiteCity.dat

驱动代码:

Configuration conf = getConf();
Job job = Job.getInstance(conf);
String dbfile = args[2];
conf.set("maxmind.geo.database.file", dbfile);

映射器代码:

public void setup(Context context) throws IOException
{
  Configuration conf = context.getConfiguration();
  String database_path = conf.get("maxmind.geo.database.file");
  LookupService cl = new LookupService(database_path,
            LookupService.GEOIP_MEMORY_CACHE | LookupService.GEOIP_CHECK_CACHE);
}

我在所有任务跟踪器中都需要这个数据库文件来完成这项工作。任何人都可以建议我这样做的正确方法吗?

【问题讨论】:

    标签: hadoop mapreduce geoip distributed-cache


    【解决方案1】:

    尝试这样做:

    从驱动程序中使用Job 对象指定文件在 HDFS 中的位置:

    job.addCacheFile(new URI("hdfs://localhot:8020/GeoLite2-City.mmdb#GeoLite2-City.mmdb"));
    

    其中,# 表示要由 hadoop 创建的别名(符号链接)

    之后,您可以通过 setup() 方法从 Mapper 访问该文件:

    @Override
    protected void setup(Context context) {
      File file = new File("GeoLite2-City.mmdb");
    }
    

    这是一个例子:

    【讨论】:

    • 嗨 Ashrith,我看到你正在使用 Ip-Geo2。感谢您的解决方案,就像一个魅力
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多