Search in sources :

Example 1 with SasColumnType

use of org.eobjects.sassy.SasColumnType in project pentaho-kettle by pentaho.

the class SasInput method processRow.

@Override
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (SasInputMeta) smi;
    data = (SasInputData) sdi;
    final Object[] fileRowData = getRow();
    if (fileRowData == null) {
        // No more work to do...
        // 
        setOutputDone();
        return false;
    }
    // 
    if (first) {
        // The output row meta data, what does it look like?
        // 
        data.outputRowMeta = new RowMeta();
        // See if the input row contains the filename field...
        // 
        int idx = getInputRowMeta().indexOfValue(meta.getAcceptingField());
        if (idx < 0) {
            throw new KettleException(BaseMessages.getString(PKG, "SASInput.Log.Error.UnableToFindFilenameField", meta.getAcceptingField()));
        }
        // Determine the output row layout
        // 
        data.outputRowMeta = getInputRowMeta().clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
    }
    String rawFilename = getInputRowMeta().getString(fileRowData, meta.getAcceptingField(), null);
    final String filename = KettleVFS.getFilename(KettleVFS.getFileObject(rawFilename));
    data.helper = new SasInputHelper(filename);
    logBasic(BaseMessages.getString(PKG, "SASInput.Log.OpenedSASFile") + " : [" + data.helper + "]");
    // 
    if (data.fileLayout == null) {
        data.fileLayout = data.helper.getRowMeta();
    } else {
        // 
        if (data.fileLayout.size() != data.helper.getRowMeta().size()) {
            throw new KettleException("All input files need to have the same number of fields. File '" + filename + "' has " + data.helper.getRowMeta().size() + " fields while the first file only had " + data.fileLayout.size());
        }
        for (int i = 0; i < data.fileLayout.size(); i++) {
            ValueMetaInterface first = data.fileLayout.getValueMeta(i);
            ValueMetaInterface second = data.helper.getRowMeta().getValueMeta(i);
            if (!first.getName().equalsIgnoreCase(second.getName())) {
                throw new KettleException("Field nr " + i + " in file '" + filename + "' is called '" + second.getName() + "' while it was called '" + first.getName() + "' in the first file");
            }
            if (first.getType() != second.getType()) {
                throw new KettleException("Field nr " + i + " in file '" + filename + "' is of data type '" + second.getTypeDesc() + "' while it was '" + first.getTypeDesc() + "' in the first file");
            }
        }
    }
    // 
    if (first) {
        first = false;
        data.fieldIndexes = new ArrayList<Integer>();
        for (SasInputField field : meta.getOutputFields()) {
            int fieldIndex = data.fileLayout.indexOfValue(field.getName());
            if (fieldIndex < 0) {
                throw new KettleException("Selected field '" + field.getName() + "' couldn't be found in file '" + filename + "'");
            }
            data.fieldIndexes.add(fieldIndex);
        }
    }
    // Add this to the result file names...
    // 
    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(filename), getTransMeta().getName(), getStepname());
    resultFile.setComment(BaseMessages.getString(PKG, "SASInput.ResultFile.Comment"));
    addResultFile(resultFile);
    SasReader sasReader = new SasReader(new File(filename));
    sasReader.read(new SasReaderCallback() {

        private boolean firstRead = true;

        @Override
        public void column(int index, String name, String label, SasColumnType type, int length) {
        }

        @Override
        public boolean readData() {
            return true;
        }

        @Override
        public boolean row(int rowNumber, Object[] rowData) {
            try {
                // 
                if (firstRead) {
                    firstRead = false;
                } else {
                    if (rowNumber == 1) {
                        return false;
                    }
                }
                Object[] row = RowDataUtil.createResizedCopy(fileRowData, data.outputRowMeta.size());
                // Only pick those fields that we're interested in.
                // 
                int outputIndex = getInputRowMeta().size();
                for (int i = 0; i < data.fieldIndexes.size(); i++) {
                    int fieldIndex = data.fieldIndexes.get(i);
                    int type = data.fileLayout.getValueMeta(fieldIndex).getType();
                    switch(type) {
                        case ValueMetaInterface.TYPE_STRING:
                            row[outputIndex++] = rowData[fieldIndex];
                            break;
                        case ValueMetaInterface.TYPE_NUMBER:
                            Double value = (Double) rowData[fieldIndex];
                            if (value.equals(Double.NaN)) {
                                value = null;
                            }
                            row[outputIndex++] = value;
                            break;
                        default:
                            throw new RuntimeException("Unhandled data type '" + ValueMetaFactory.getValueMetaName(type));
                    }
                }
                // Convert the data type of the new data to the requested data types
                // 
                convertData(data.fileLayout, row, data.outputRowMeta);
                // Pass along the row to further steps...
                // 
                putRow(data.outputRowMeta, row);
                return !isStopped();
            } catch (Exception e) {
                throw new RuntimeException("There was an error reading from SAS7BAT file '" + filename + "'", e);
            }
        }
    });
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) RowMeta(org.pentaho.di.core.row.RowMeta) ResultFile(org.pentaho.di.core.ResultFile) KettleException(org.pentaho.di.core.exception.KettleException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SasColumnType(org.eobjects.sassy.SasColumnType) SasReader(org.eobjects.sassy.SasReader) File(java.io.File) ResultFile(org.pentaho.di.core.ResultFile) SasReaderCallback(org.eobjects.sassy.SasReaderCallback)

Aggregations

File (java.io.File)1 SasColumnType (org.eobjects.sassy.SasColumnType)1 SasReader (org.eobjects.sassy.SasReader)1 SasReaderCallback (org.eobjects.sassy.SasReaderCallback)1 ResultFile (org.pentaho.di.core.ResultFile)1 KettleException (org.pentaho.di.core.exception.KettleException)1 RowMeta (org.pentaho.di.core.row.RowMeta)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1