1 SparkConf conf = newSparkConf();2 conf.setAppName("Simple Application").setMaster("local");3 JavaSparkContext jsc = newJavaSparkContext(conf);4
5
6 //1.直接使用jdbcRDD的构造函数
7 class DbConnection extends AbstractFunction0implements8 Serializable {9 private static final long serialVersionUID = 1L;10 privateString driverClassName;11 privateString connectionUrl;12 privateString userName;13 privateString password;14
15 publicDbConnection(String driverClassName, String connectionUrl,16 String userName, String password) {17 this.driverClassName =driverClassName;18 this.connectionUrl =connectionUrl;19 this.userName =userName;20 this.password =password;21 }22
23 @Override24 publicConnection apply() {25 try{26 Class.forName(driverClassName);27 } catch(ClassNotFoundException e) {28 }29 Properties properties = newProperties();30 properties.setProperty("user", use