Example usage for org.apache.hadoop.mapreduce.lib.db DBConfiguration getOutputFieldNames

List of usage examples for org.apache.hadoop.mapreduce.lib.db DBConfiguration getOutputFieldNames

Introduction

In this page you can find the example usage for org.apache.hadoop.mapreduce.lib.db DBConfiguration getOutputFieldNames.

Prototype

public String[] getOutputFieldNames() 

Source Link

Usage

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   w  w w .  j a v  a  2s.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()];
    }/*from   w  w w  .j  a  va  2 s  . com*/

    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 w  w .j  a v a  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()];
    }/*from   ww  w . ja v  a  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());
    }
}