几天前,我决定让selenium webdriver(第三方软件包)在hadoop的mapreduce框架中运行 . 我遇到了一个问题 . Map 步骤冻结在 new FirefoxDriver();
中 . FirefoxDriver类位于名为 selenium-server-standalone-2.38.0.jar
的第三方jar中 . 如果有人有经验或兴趣,我需要你的帮助!
一些细节:
-
问题详情
-
为了在命令行中运行代码,我使用"Xvfb"来停止Firefox图形界面 . 然后我在开始时说的问题出现了 . 我通过tasktraker的日志找到代码冻结在
this.driver = new FirefoxDriver(ffprofile);
虽然代码冻结了,但是firefox已经设置好了,我用ps -A | grep firefox
检查它 -
环境:
ubuntu 10.04 32bit; Hadoop的1.2.0; Mozilla Firefox 17.0.5;硒 - 服务器 - 独立-2.38.0.jar; xvfb的;
-
提示
-
(1)Hadoop以Pesudo分布式运行;
-
(2)当我在Eclipse中运行代码时,每件事都没问题 . 火狐按计划弹出 . (我最后会展示演示代码);
-
(3)如果你跑到
org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms
,使用commadps -A | grep firefox
来检查是否设置了一些firefox,并使用killall firefox
. -
(4)让代码在命令行中运行 . 也许你会遇见
Error no display specified
你可以使用Xvfb :99 -ac 2>/dev/null &
安装xvfb
并设置xvfb
. 在设置之前xvfb
在HADOOP_HOME/conf/hadoop-env.sh
末尾附加一行export DISPLAY=:99
-
代码演示
public class MapRunnerNewFirefox extends Configured implements Tool, MapRunnable{
public static final Logger LOG = LoggerFactory.getLogger(MapRunnerNewFirefox.class);
@Override
public void configure(JobConf conf) {
}
@Override
public void run(RecordReader recordReader,
OutputCollector output, Reporter reporter) throws IOException {
LongWritable key = new LongWritable(-1);// shouldn't be null ,otherwise the recordReader will report nullpointer err;
Text val = new Text("begin text"); // same as up line;
int i = 0;
reporter.progress();
while(recordReader.next(key, val)){
if(LOG.isInfoEnabled()){
LOG.info("key: "+key.toString()+" val: "+val.toString());
}
String temp = "ao";
NewFirefox ff = new NewFirefox("/home/cc/firefox/firefox/firefox");
output.collect(new Text("get-"+key.toString()), new Text(temp));
}
}
@Override
public int run(String[] args) throws Exception {
if(LOG.isInfoEnabled()) {
LOG.info("set maprunner conf");
}
Path urlDir = new Path(args[0]);
Path resultDir = new Path(args[1] + System.currentTimeMillis());
JobConf job = new JobConf(getConf());
job.setNumMapTasks(1);
job.setJobName("hello maprunners");
job.setInputFormat(TextInputFormat.class);
FileInputFormat.addInputPath(job, urlDir);
job.setMapRunnerClass(MapRunnerNewFirefox.class);
FileOutputFormat.setOutputPath(job, resultDir);
job.setOutputFormat(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
JobClient.runJob(job);
return 0;
}
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
int res = ToolRunner.run(conf, new MapRunnerNewFirefox(), args);
System.exit(res);
}
}
public class NewFirefox {
private WebDriver driver;
private static final Logger LOG = LoggerFactory.getLogger(NewFirefox.class);
public NewFirefox(String firefoxPath){
if(LOG.isInfoEnabled()){
LOG.info("firefox ****0");
}
System.setProperty("webdriver.firefox.bin", firefoxPath);
if(LOG.isInfoEnabled()){
LOG.info("firefox ****1");
}
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");
if(LOG.isInfoEnabled()){
LOG.info("firefox ****2");
}
this.driver = new FirefoxDriver(ffprofile);
if(LOG.isInfoEnabled()){
LOG.info("firefox ****3");
}
this.driver.quit();
if(LOG.isInfoEnabled()){
LOG.info("firefox quit");
}
}
}