我正在编写一个 MapReduce 作业来分析 Web 日志。我的代码旨在将 IP 地址映射到地理位置,为此我使用 Maxmind Geo API(https://github.com/maxmind/geoip-api-java)。我的代码有一个 LookupService 方法,该方法需要带有 ip 到位置匹配的数据库文件。我正在尝试使用分布式缓存传递此数据库文件。我尝试以两种不同的方式做到这一点
情况1:
运行从 HDFS 传递文件的作业,但它总是抛出一个错误,说“未找到文件”
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 选项从本地文件系统传递文件来运行代码。错误: 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);
}
我在所有任务跟踪器中都需要这个数据库文件来完成这项工作。有人可以建议我这样做的正确方法吗?