Search in sources :

Example 56 with CSVFormat

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;
}
Also used : IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ImportException(org.jgrapht.nio.ImportException) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser) Extension(org.matheclipse.core.io.Extension) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) SyntaxError(org.matheclipse.parser.client.SyntaxError) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FileReader(java.io.FileReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr) File(java.io.File)

Example 57 with CSVFormat

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'");
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) InvalidTableException(org.apache.beam.sdk.extensions.sql.meta.provider.InvalidTableException) Schema(org.apache.beam.sdk.schemas.Schema) CSVFormat(org.apache.commons.csv.CSVFormat) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

CSVFormat (org.apache.commons.csv.CSVFormat)57 CSVRecord (org.apache.commons.csv.CSVRecord)22 IOException (java.io.IOException)21 CSVParser (org.apache.commons.csv.CSVParser)19 ArrayList (java.util.ArrayList)15 StringReader (java.io.StringReader)13 InputStream (java.io.InputStream)9 InputStreamReader (java.io.InputStreamReader)8 HashMap (java.util.HashMap)8 CSVPrinter (org.apache.commons.csv.CSVPrinter)8 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)8 RecordField (org.apache.nifi.serialization.record.RecordField)8 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)8 Test (org.junit.Test)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Reader (java.io.Reader)7 LinkedHashMap (java.util.LinkedHashMap)7 SchemaNameAsAttribute (org.apache.nifi.schema.access.SchemaNameAsAttribute)7 MapRecord (org.apache.nifi.serialization.record.MapRecord)7 Record (org.apache.nifi.serialization.record.Record)7