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
Related Q & A:
- How to completely backup all mysql databases?Best solution by Server Fault
- How to do a local mysql database replication on an online server?Best solution by howtoforge.com
- How do you create temporary MySQL table in SQLAlchemy?Best solution by Stack Overflow
- How to migrate data from mySQL to PostgreSQL?Best solution by Stack Overflow
- How do I connect to SQL Server using C#?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.