use of org.apache.commons.csv.CSVFormat in project symja_android_library by axkr.
the class Import method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (Config.isFileSystemEnabled(engine)) {
if (!(ast.arg1() instanceof IStringX)) {
return F.NIL;
}
IStringX arg1 = (IStringX) ast.arg1();
Extension format = Extension.importFilename(arg1.toString());
String fileName = arg1.toString();
if (ast.size() > 2) {
if (!(ast.arg2() instanceof IStringX)) {
return F.NIL;
}
format = Extension.importExtension(((IStringX) ast.arg2()).toString());
}
FileReader reader = null;
try {
File file = new File(fileName);
switch(format) {
case DOT:
case GRAPHML:
// graph Format
reader = new FileReader(fileName);
return graphImport(reader, format, engine);
case EXPRESSIONJSON:
return expressionJSONImport(fileName);
case JSON:
return jsonImport(fileName);
case M:
if (ast.isAST1()) {
return S.Get.of(engine, ast.arg1());
}
break;
case TABLE:
reader = new FileReader(fileName);
AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(' ');
Iterable<CSVRecord> records = csvFormat.parse(reader);
IASTAppendable rowList = F.ListAlloc(256);
for (CSVRecord record : records) {
IASTAppendable columnList = F.ListAlloc(record.size());
for (String string : record) {
final ASTNode node = parser.parse(string);
IExpr temp = ast2Expr.convert(node);
columnList.append(temp);
}
rowList.append(columnList);
}
return rowList;
case STRING:
return ofString(file, engine);
case TXT:
return ofText(file, engine);
case WXF:
byte[] byteArray = com.google.common.io.Files.toByteArray(file);
return WL.deserialize(byteArray);
default:
}
} catch (IOException ioe) {
LOGGER.log(engine.getLogLevel(), "Import: file {} not found!", fileName, ioe);
} catch (SyntaxError se) {
LOGGER.log(engine.getLogLevel(), "Import: file {} syntax error!", fileName, se);
} catch (Exception ex) {
LOGGER.log(engine.getLogLevel(), "Import: file {} ", fileName, ex);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
return F.NIL;
}
use of org.apache.commons.csv.CSVFormat in project beam by apache.
the class TextTableProvider method buildBeamSqlTable.
@Override
public BeamSqlTable buildBeamSqlTable(Table table) {
Schema schema = table.getSchema();
String filePattern = table.getLocation();
JSONObject properties = table.getProperties();
String format = MoreObjects.firstNonNull(properties.getString("format"), "csv");
String deadLetterFile = properties.getString("deadLetterFile");
// Backwards compatibility: previously "type": "text" meant CSV and "format" was where the
// CSV format went. So assume that any other format is the CSV format.
@Nullable String legacyCsvFormat = null;
if (!ImmutableSet.of("csv", "lines", "json").contains(format)) {
legacyCsvFormat = format;
format = "csv";
}
switch(format) {
case "csv":
String specifiedCsvFormat = properties.getString("csvformat");
CSVFormat csvFormat = specifiedCsvFormat != null ? CSVFormat.valueOf(specifiedCsvFormat) : (legacyCsvFormat != null ? CSVFormat.valueOf(legacyCsvFormat) : CSVFormat.DEFAULT);
return new TextTable(schema, filePattern, new CsvToRow(schema, csvFormat), new RowToCsv(csvFormat));
case "json":
return new TextJsonTable(schema, filePattern, JsonToRow.create(schema, deadLetterFile), RowToJson.create());
case "lines":
if (!(schema.getFieldCount() == 1 && schema.getField(0).getType().getTypeName().equals(TypeName.STRING))) {
throw new InvalidTableException("Table with type 'text' and format 'lines' " + "must have exactly one STRING/VARCHAR/CHAR column ");
}
return new TextTable(schema, filePattern, new LinesReadConverter(), new LinesWriteConverter());
default:
throw new InvalidTableException("Table with type 'text' must have format 'csv' or 'lines' or 'json'");
}
}
Aggregations