List of usage examples for org.apache.hadoop.mapred.lib.db DBConfiguration configureDB
public static void configureDB(JobConf job, String driverClass, String dbUrl)
From source file:co.cask.cdap.template.etl.batch.sink.DBSink.java
License:Apache License
@Override public void prepareRun(BatchSinkContext context) { LOG.debug("tableName = {}; pluginType = {}; pluginName = {}; connectionString = {}; columns = {}", dbSinkConfig.tableName, dbSinkConfig.jdbcPluginType, dbSinkConfig.jdbcPluginName, dbSinkConfig.connectionString, dbSinkConfig.columns); Job job = context.getHadoopJob();/*www.ja v a 2 s . c o m*/ Configuration hConf = job.getConfiguration(); // Load the plugin class to make sure it is available. Class<? extends Driver> driverClass = context.loadPluginClass(getJDBCPluginId()); if (dbSinkConfig.user == null && dbSinkConfig.password == null) { DBConfiguration.configureDB(hConf, driverClass.getName(), dbSinkConfig.connectionString); } else { DBConfiguration.configureDB(hConf, driverClass.getName(), dbSinkConfig.connectionString, dbSinkConfig.user, dbSinkConfig.password); } List<String> fields = Lists.newArrayList(Splitter.on(",").omitEmptyStrings().split(dbSinkConfig.columns)); try { ETLDBOutputFormat.setOutput(job, dbSinkConfig.tableName, fields.toArray(new String[fields.size()])); } catch (IOException e) { throw Throwables.propagate(e); } job.setOutputFormatClass(ETLDBOutputFormat.class); }
From source file:com.github.gaoyangthu.demo.mapred.DBCountPageView.java
License:Apache License
@Override //Usage DBCountPageView [driverClass dburl] public int run(String[] args) throws Exception { String driverClassName = DRIVER_CLASS; String url = DB_URL;/*from w ww .j ava 2s .c o m*/ 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); 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; }