Search in sources :

Example 1 with CSVImportInfo

use of com.teradata.jaqy.utils.CSVImportInfo in project jaqy by Teradata.

the class CSVImporter method getObject.

@Override
public Object getObject(int index, ParameterInfo paramInfo, JaqyInterpreter interpreter) throws Exception {
    try {
        String value = m_record.get(index);
        if (m_naFilter) {
            for (String f : m_naValues) if (value.equals(f))
                return null;
        }
        CSVImportInfo importInfo = m_importInfoMap.get(index);
        if (importInfo != null) {
            if (value.length() == 0)
                return null;
            Path file = m_file.getRelativePath(value);
            if (!file.isFile())
                throw new FileNotFoundException("External file " + file.getPath() + " is not found.");
            if (importInfo.charset == null)
                return new FileBlob(file);
            else
                return new FileClob(file, importInfo.charset);
        }
        if (TypesUtils.isBinary(paramInfo.type))
            return StringUtils.getBytesFromHexString(value);
        return value;
    } catch (ArrayIndexOutOfBoundsException ex) {
        throw new IOException("Column " + (index + 1) + " is not found.");
    }
}
Also used : Path(com.teradata.jaqy.interfaces.Path) FileBlob(com.teradata.jaqy.resultset.FileBlob) FileClob(com.teradata.jaqy.resultset.FileClob) CSVImportInfo(com.teradata.jaqy.utils.CSVImportInfo)

Example 2 with CSVImportInfo

use of com.teradata.jaqy.utils.CSVImportInfo in project jaqy by Teradata.

the class CSVImporterFactory method getHandler.

@Override
public CSVImporter getHandler(CommandLine cmdLine, JaqyInterpreter interpreter) throws Exception {
    Charset charset = DEFAULT_CHARSET;
    CSVFormat format = CSVFormat.DEFAULT;
    HashMap<Integer, CSVImportInfo> importInfoMap = new HashMap<Integer, CSVImportInfo>();
    boolean naFilter = false;
    String[] naValues = null;
    boolean precise = false;
    // -1 indicates internal algorithm
    long scanThreshold = -1;
    Charset encoding = DEFAULT_CHARSET;
    for (Option option : cmdLine.getOptions()) {
        switch(option.getOpt().charAt(0)) {
            case 'c':
                {
                    charset = Charset.forName(option.getValue());
                    break;
                }
            case 'd':
                {
                    char delimiter = CSVUtils.getChar(option.getValue());
                    if (delimiter == 0)
                        throw new IllegalArgumentException("invalid delimiter: " + option.getValue());
                    format = format.withDelimiter(delimiter);
                    break;
                }
            case 'h':
                {
                    format = format.withHeader();
                    break;
                }
            case 't':
                {
                    format = CSVUtils.getFormat(option.getValue());
                    break;
                }
            case 'f':
                {
                    naFilter = true;
                    break;
                }
            case 'v':
                {
                    naValues = option.getValue().split(",");
                    naFilter = true;
                    break;
                }
            case 'p':
                {
                    precise = true;
                    break;
                }
            case 'r':
                {
                    scanThreshold = Long.parseLong(option.getValue());
                    if (scanThreshold <= 0)
                        scanThreshold = 0;
                    break;
                }
            case 'e':
                {
                    encoding = Charset.forName(option.getValue());
                    break;
                }
            case 'j':
                {
                    int column = Integer.parseInt(option.getValue());
                    if (column < 1) {
                        interpreter.error("Column index cannot be smaller than 1.");
                    }
                    CSVImportInfo info = new CSVImportInfo(encoding);
                    importInfoMap.put(column - 1, info);
                    break;
                }
            case 'k':
                {
                    int column = Integer.parseInt(option.getValue());
                    if (column < 1) {
                        interpreter.error("Column index cannot be smaller than 1.");
                    }
                    CSVImportInfo info = new CSVImportInfo(null);
                    importInfoMap.put(column - 1, info);
                    break;
                }
        }
    }
    String[] args = cmdLine.getArgs();
    if (args.length == 0)
        throw new IllegalArgumentException("missing file name.");
    CSVImporter importer = new CSVImporter(interpreter.getPath(args[0]), charset, format, importInfoMap, precise, scanThreshold);
    if (naFilter == true) {
        importer.setNaFilter(true);
        importer.setNaValues(naValues);
    }
    return importer;
}
Also used : HashMap(java.util.HashMap) CSVImportInfo(com.teradata.jaqy.utils.CSVImportInfo) Charset(java.nio.charset.Charset) CSVFormat(org.apache.commons.csv.CSVFormat) Option(org.apache.commons.cli.Option)

Aggregations

CSVImportInfo (com.teradata.jaqy.utils.CSVImportInfo)2 Path (com.teradata.jaqy.interfaces.Path)1 FileBlob (com.teradata.jaqy.resultset.FileBlob)1 FileClob (com.teradata.jaqy.resultset.FileClob)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 Option (org.apache.commons.cli.Option)1 CSVFormat (org.apache.commons.csv.CSVFormat)1