/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.sqoop.mapreduce;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.util.ReflectionUtils;
import com.cloudera.sqoop.lib.RecordParser;
import com.cloudera.sqoop.lib.SqoopRecord;
import com.cloudera.sqoop.mapreduce.AutoProgressMapper;

/**
 * Converts an input record from a string representation to a parsed Sqoop
 * record and emits that DBWritable to the OutputFormat for writeback to the
 * database.
 */
public class TextExportMapper
    extends AutoProgressMapper<LongWritable, Text, SqoopRecord, NullWritable> {

  private SqoopRecord recordImpl;

  public TextExportMapper() {
  }

  protected void setup(Context context)
      throws IOException, InterruptedException {
    super.setup(context);

    Configuration conf = context.getConfiguration();

    // Instantiate a copy of the user's class to hold and parse the record.
    String recordClassName = conf.get(
        ExportJobBase.SQOOP_EXPORT_TABLE_CLASS_KEY);
    if (null == recordClassName) {
      throw new IOException("Export table class name ("
          + ExportJobBase.SQOOP_EXPORT_TABLE_CLASS_KEY
          + ") is not set!");
    }

    try {
      Class cls = Class.forName(recordClassName, true,
          Thread.currentThread().getContextClassLoader());
      recordImpl = (SqoopRecord) ReflectionUtils.newInstance(cls, conf);
    } catch (ClassNotFoundException cnfe) {
      throw new IOException(cnfe);
    }

    if (null == recordImpl) {
      throw new IOException("Could not instantiate object of type "
          + recordClassName);
    }
  }


  public void map(LongWritable key, Text val, Context context)
      throws IOException, InterruptedException {
    try {
      recordImpl.parse(val);
      context.write(recordImpl, NullWritable.get());
    } catch (RecordParser.ParseError pe) {
      throw new IOException("Could not parse record: " + val, pe);
    }
  }
}

@@ -1058,72 +1058,131 @@
 oop.
-util.ReflectionUtils;%0Aimport com.cloudera.sqoop.lib.RecordParser
+mapreduce.InputSplit;%0Aimport org.apache.hadoop.mapreduce.lib.input.FileSplit;%0Aimport org.apache.hadoop.util.ReflectionUtils
 ;%0Aim
@@ -1277,16 +1277,101 @@
 sMapper;
+%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;
 %0A%0A/**%0A *
@@ -1648,16 +1648,106 @@
 ble%3E %7B%0A%0A
+  public static final Log LOG =%0A    LogFactory.getLog(TextExportMapper.class.getName());%0A%0A
   privat
@@ -2973,101 +2973,940 @@
 ch (
-RecordParser.ParseError pe) %7B%0A      throw new IOException(%22Could not parse record: %22 + val, p
+Exception e) %7B%0A      // Something bad has happened%0A      LOG.error(%22%22);%0A      LOG.error(%22Exception raised during data export%22);%0A      LOG.error(%22%22);%0A%0A      LOG.error(%22Exception: %22, e);%0A      LOG.error(%22On input: %22 + val);%0A%0A      InputSplit is = context.getInputSplit();%0A      if (is instanceof FileSplit) %7B%0A        LOG.error(%22On input file: %22 + ((FileSplit)is).getPath());%0A      %7D else if (is instanceof CombineFileSplit) %7B%0A        LOG.error(%22On input file: %22%0A          + context.getConfiguration().get(%22map.input.file%22));%0A      %7D%0A      LOG.error(%22At position %22 + key);%0A%0A      LOG.error(%22%22);%0A      LOG.error(%22Currently processing split:%22);%0A      LOG.error(is);%0A%0A      LOG.error(%22%22);%0A      LOG.error(%22This issue might not necessarily be caused by current input%22);%0A      LOG.error(%22due to the batching nature of export.%22);%0A      LOG.error(%22%22);%0A%0A      throw new IOException(%22Can't export data, please check task tracker logs%22,%0A        
 e);%0A

Improve error handling when exporting malformed text data