How to connect to mysql in MapReduce temporarily?

How to temporarily connect to MySQL in MapReduce

  • I want to connect to MySQL in MapReduce temporarily. In other words, I want to bring table in MySQL to Map function temporarily to change InputData(Text) but, the Result was empty. Below is my code: public class Map extends Mapper{ private Text outputKey=new Text(); private final static IntWritable outputValue=new IntWritable(1); public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException { int i=0; try{ Connection con=DriverManager.getConnection("jdbc:sqlserver://locallhost:3306/test_db", "user","user"); PreparedStatement ps=con.prepareStatement("select * from studentinfo"); ResultSet rs=ps.executeQuery(); while(rs.next()) i++; AirlinePerformanceParser parser=new AirlinePerformanceParser(value); outputKey.set(parser.getMonth_day()+","+i); context.write(outputKey, outputValue); }catch(SQLException e) { e.printStackTrace(); } } } Map and reduce work well (there are no exceptions), but Result was empty. I don't want to use DBInputFormat. MySQL table must not be used by DBInputFormat. What is the problem?

  • Answer:

    If your query is static, i.e.it does not depend on map input (as is your current query), you should move your sql code into the setup function, to query your DB only once. // set global the data buffer from your sql results (here a counter) int nbResults; @Override protected void setup(Context context) throws IOException, InterruptedException { // query your DB Connection con [...] } public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException { AirlinePerformanceParser parser=new AirlinePerformanceParser(value); outputKey.set(parser.getMonth_day()+","+nbResults); context.write(outputKey, outputValue); } As pointed by @Ramzy, check the connexion status. In addition, do not perform a SELECT * if what you need is a COUNT(*).

kimkwangmin at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.