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.");
}
}
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;
}
Aggregations