• DBWritable的使用


    首先导入mysql连接驱动jar包

    或者maven模式下在pom.xml文件中追加:

    <dependency>

        <groupId>mysql</groupId>

        <artifactId>mysql-connector-java</artifactId>

        <version>5.1.38</version>

    </dependency>

    代码:

    package com.neworigin.db;

    import java.io.IOException;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.fs.FileSystem;

    import org.apache.hadoop.fs.Path;

    import org.apache.hadoop.io.LongWritable;

    import org.apache.hadoop.io.NullWritable;

    import org.apache.hadoop.io.Text;

    import org.apache.hadoop.mapreduce.Job;

    import org.apache.hadoop.mapreduce.Mapper;

    import org.apache.hadoop.mapreduce.Reducer;

    import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;

    import org.apache.hadoop.mapreduce.lib.db.DBInputFormat;

    import org.apache.hadoop.mapreduce.lib.db.DBOutputFormat;

    import org.apache.hadoop.mapreduce.lib.db.DBWritable;

    import org.apache.hadoop.mapreduce.lib.input.FileSplit;

    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

     

    public class DBJob {

    public static class TableUers implements DBWritable{//表的映射

    int id;

    String name;

    int age;

     

       public int getId() {

       return id;

    }

     

    public void setId(int id) {

       this.id = id;

    }

     

    public String getName() {

       return name;

    }

     

    public void setName(String name) {

       this.name = name;

    }

     

    public int getAge() {

       return age;

    }

     

    public void setAge(int age) {

       this.age = age;

    }

     

       public TableUers(int id, String name, int age) {

       this.id = id;

       this.name = name;

       this.age = age;

    }

     

       public TableUers() {

       }

    //将表的映射对象中的属性设置进(写入数据库)

       public void write(PreparedStatement statement) throws SQLException {

          statement.setInt(1, id);

          statement.setString(2, name);

          statement.setInt(3, age);

       }

    //从结果集通过与字段匹配的赋值给表的映射对象(读取数据库)

       public void readFields(ResultSet resultSet) throws SQLException {//获取表中的属性

          this.id=resultSet.getInt("id");

          this.name=resultSet.getString("name");

          this.age=resultSet.getInt("age");

         

       }

     

       @Override

       public String toString() {

          return  id + "  " + name + " " + age;

       }

      

    }

    public static class MyMapper extends Mapper<Object ,TableUers,TableUers,NullWritable>{

     

       protected void map(Object key, TableUers value, Mapper<Object, TableUers, TableUers, NullWritable>.Context context)

              throws IOException, InterruptedException {

          String s = value.toString();

          System.out.println(s);

         

         

          context.write(value,NullWritable.get());

         

       }

      

    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

       Path out=new Path("file:///F:/安装/java工程/MR/data/db/out");

       Job job = Job.getInstance();

       Configuration conf = job.getConfiguration();

    /* FileSystem fs =FileSystem.get(conf);

       if(fs.exists(out))

       {

          fs.delete(out);

       }*/

      

       job.setInputFormatClass(DBInputFormat.class);

       DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1/mydb","root","123456");

       DBInputFormat.setInput(job, TableUers.class, "select id,name,age from user", "select count(*) from user");

      

       job.setOutputFormatClass(DBOutputFormat.class);

       DBOutputFormat.setOutput(job, "aaa", "id","name","age");

      

      

       job.setMapperClass(MyMapper.class);

       job.setMapOutputKeyClass(TableUers.class);

       job.setMapOutputValueClass(NullWritable.class);

      

      

       job.setOutputKeyClass(TableUers.class);

       job.setOutputValueClass(NullWritable.class);

       job.setNumReduceTasks(0);

      

       //FileOutputFormat.setOutputPath(job, out);

      

       job.waitForCompletion(true);

    }

    }

  • 相关阅读:
    mysql索引类型normal,unique,full text的区别
    php中模糊查询并关联三个select框
    JQuery实现获取多个input输入框的值,并存放在一个数组中
    Chrome 浏览器跨域和安全访问问题 使用 chrome的命令行标记:disable-web-security 参数联调线上数据
    sqlserver 出现 因为文件组 'PRIMARY' 已满 的解决办法 有可能是磁盘剩余空间不足 导致的
    asp.net 分析器错误消息: 文件.aspx.cs”不存在错误
    SQLSERVER排查CPU占用高的情况
    SQL Server Cpu 100% 的常见原因及优化
    Getting Started with OWIN and Katana(Console 代替iis 制作 web服务的简单方案)
    Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)
  • 原文地址:https://www.cnblogs.com/chengdonghui/p/7825003.html
Copyright © 2020-2023  润新知