java - 运行 Hadoop DbCountPageView.java

标签 java mysql jdbc hadoop

嗨,我正在尝试 Hadoop 提供的 DbCountPageView 示例,首先我简单地运行代码而不传递参数,它给了我一些数据库访问页面信息。在我尝试运行这个程序给出争论后,但它在 Eclipse 中给了我以下错误:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'testdb,root,'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:928)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1750)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1290)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2493)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.apache.hadoop.examples.DBCountPageView.createConnection(DBCountPageView.java:102)
    at org.apache.hadoop.examples.DBCountPageView.initialize(DBCountPageView.java:131)
    at org.apache.hadoop.examples.DBCountPageView.run(DBCountPageView.java:394)
    at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:65)
    at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:79)
    at org.apache.hadoop.examples.DBCountPageView.main(DBCountPageView.java:432)

我对 eclipse 的争论是“com.mysql.jdbc.Driver”“jdbc:mysql://localhost:3306/testDb”,“root”,“” 对于mysql数据库。我不知道什么错误,它给了我访问被拒绝错误,我没有向 mysql 服务器输入密码。请帮忙。

它是一个大型程序,位于hadoop安装包中:src->examples->org/apache/hadoop/example。

争论应该通过的代码如下

public int run(String[] args) throws Exception {

    String driverClassName = DRIVER_CLASS;
    String url = DB_URL;

    if(args.length > 1) {
      driverClassName = args[0];
      url = args[1]+","+"root"+","+"";
    }

    initialize(driverClassName, url);

    JobConf job = new JobConf(getConf(), DBCountPageView.class);

    job.setJobName("Count Pageviews of URLs");

    job.setMapperClass(PageviewMapper.class);
    job.setCombinerClass(LongSumReducer.class);
    job.setReducerClass(PageviewReducer.class);

    DBConfiguration.configureDB(job, driverClassName, url);

    DBInputFormat.setInput(job, AccessRecord.class, "Access"
        , null, "url", AccessFieldNames);

    DBOutputFormat.setOutput(job, "Pageview", PageviewFieldNames);

    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(LongWritable.class);

    job.setOutputKeyClass(PageviewRecord.class);
    job.setOutputValueClass(NullWritable.class);

    try {
      JobClient.runJob(job);

      boolean correct = verify();
      if(!correct) {
        throw new RuntimeException("Evaluation was not correct!");
      }
    } finally {
      shutdown();    
    }
    return 0;
  }

  public static void main(String[] args) throws Exception {

    int ret = ToolRunner.run(new DBCountPageView(), args);
    System.exit(ret);
  }

最佳答案

问题在于您将凭据传递到了错误的位置。 请尝试使用以下代码:

public int run(String[] args) throws Exception {

    String driverClassName = DRIVER_CLASS;
    String url = DB_URL;

    if(args.length > 1) {
      driverClassName = args[0];
      url = args[1];
    }

    initialize(driverClassName, url);

    JobConf job = new JobConf(getConf(), DBCountPageView.class);

    job.setJobName("Count Pageviews of URLs");

    job.setMapperClass(PageviewMapper.class);
    job.setCombinerClass(LongSumReducer.class);
    job.setReducerClass(PageviewReducer.class);

    DBConfiguration.configureDB(job, driverClassName, url, "root", "");

    DBInputFormat.setInput(job, AccessRecord.class, "Access"
    , null, "url", AccessFieldNames);

    DBOutputFormat.setOutput(job, "Pageview", PageviewFieldNames);

    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(LongWritable.class);

    job.setOutputKeyClass(PageviewRecord.class);
    job.setOutputValueClass(NullWritable.class);

    try {
      JobClient.runJob(job);

      boolean correct = verify();
      if(!correct) {
        throw new RuntimeException("Evaluation was not correct!");
      }
    } finally {
      shutdown();    
    }
    return 0;
}

public static void main(String[] args) throws Exception {
    int ret = ToolRunner.run(new DBCountPageView(), args);
    System.exit(ret);
}

关于java - 运行 Hadoop DbCountPageView.java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21698344/

相关文章:

mysql - 按日期从当前每条记录中获取上一条记录

java - 即使没有更新,JDBCexecuteBatch() 是否应该返回一个具有原始批处理长度的数组?

java - 带 ListView 的 Viewpager

mysql - 世界数据库练习

Java - 在 .jar 所在的目录中创建一个文件(如果该文件尚不存在)

SQL函数来计算字符串在列中出现的次数?

java - PostgreSQL - quartz JDBC-JobStoreTX - getTriggersForJob - ArrayIndexOutOfBoundsException

java - JDBC SQL 异常 : query executes correctly on the MySQL prompt but gives error in java

java - 如何使用二进制搜索来查找具有特定权重的数组中的第一个元素(不同数组中的另一个元素)?

java - 如何在 Windows 中使用 File.separator