List of usage examples for org.apache.hadoop.mapreduce.lib.db DBConfiguration getOutputFieldCount
public int getOutputFieldCount()
From source file:co.cask.cdap.template.etl.common.ETLDBOutputFormat.java
License:Apache License
@Override public RecordWriter<K, V> getRecordWriter(TaskAttemptContext context) throws IOException { Configuration conf = context.getConfiguration(); DBConfiguration dbConf = new DBConfiguration(conf); String tableName = dbConf.getOutputTableName(); String[] fieldNames = dbConf.getOutputFieldNames(); if (fieldNames == null) { fieldNames = new String[dbConf.getOutputFieldCount()]; }//from ww w . jav a2 s. com try { Connection connection = getConnection(conf); PreparedStatement statement = connection.prepareStatement(constructQuery(tableName, fieldNames)); return new DBRecordWriter(connection, statement) { @Override public void close(TaskAttemptContext context) throws IOException { super.close(context); try { DriverManager.deregisterDriver(driverShim); } catch (SQLException e) { throw new IOException(e); } } }; } catch (Exception ex) { throw new IOException(ex.getMessage()); } }
From source file:co.cask.hydrator.plugin.db.batch.sink.ETLDBOutputFormat.java
License:Apache License
@Override public RecordWriter<K, V> getRecordWriter(TaskAttemptContext context) throws IOException { Configuration conf = context.getConfiguration(); DBConfiguration dbConf = new DBConfiguration(conf); String tableName = dbConf.getOutputTableName(); String[] fieldNames = dbConf.getOutputFieldNames(); if (fieldNames == null) { fieldNames = new String[dbConf.getOutputFieldCount()]; }// w w w .j ava 2s .co m try { Connection connection = getConnection(conf); PreparedStatement statement = connection.prepareStatement(constructQuery(tableName, fieldNames)); return new DBRecordWriter(connection, statement) { private boolean emptyData = true; //Implementation of the close method below is the exact implementation in DBOutputFormat except that //we check if there is any data to be written and if not, we skip executeBatch call. //There might be reducers that don't receive any data and thus this check is necessary to prevent //empty data to be committed (since some Databases doesn't support that). @Override public void close(TaskAttemptContext context) throws IOException { try { if (!emptyData) { getStatement().executeBatch(); getConnection().commit(); } } catch (SQLException e) { try { getConnection().rollback(); } catch (SQLException ex) { LOG.warn(StringUtils.stringifyException(ex)); } throw new IOException(e.getMessage()); } finally { try { getStatement().close(); getConnection().close(); } catch (SQLException ex) { throw new IOException(ex.getMessage()); } } try { DriverManager.deregisterDriver(driverShim); } catch (SQLException e) { throw new IOException(e); } } @Override public void write(K key, V value) throws IOException { super.write(key, value); emptyData = false; } }; } catch (Exception ex) { throw new IOException(ex.getMessage()); } }
From source file:com.cloudera.sqoop.mapreduce.db.DBOutputFormat.java
License:Apache License
@Override /** {@inheritDoc} */ public RecordWriter<K, V> getRecordWriter(TaskAttemptContext context) throws IOException { DBConfiguration dbConf = new DBConfiguration(context.getConfiguration()); String tableName = dbConf.getOutputTableName(); String[] fieldNames = dbConf.getOutputFieldNames(); if (fieldNames == null) { fieldNames = new String[dbConf.getOutputFieldCount()]; }//from w ww .j a va 2 s . c o m try { Connection connection = dbConf.getConnection(); PreparedStatement statement = null; statement = connection.prepareStatement(constructQuery(tableName, fieldNames)); return new DBRecordWriter(connection, statement); } catch (Exception ex) { throw new IOException(ex.getMessage()); } }
From source file:hadoop.MysqlDBOutputFormat.java
License:Apache License
/** {@inheritDoc} */ public RecordWriter<K, V> getRecordWriter(TaskAttemptContext context) throws IOException { DBConfiguration dbConf = new DBConfiguration(context.getConfiguration()); String tableName = dbConf.getOutputTableName(); String[] fieldNames = dbConf.getOutputFieldNames(); if (fieldNames == null) { fieldNames = new String[dbConf.getOutputFieldCount()]; }/* w w w . jav a 2s.c o m*/ try { Connection connection = dbConf.getConnection(); PreparedStatement statement = null; statement = connection.prepareStatement(constructQuery(tableName, fieldNames)); return new DBRecordWriter(connection, statement); } catch (Exception ex) { throw new IOException(ex.getMessage()); } }