Search in sources :

Example 41 with KsqlException

use of io.confluent.ksql.util.KsqlException in project ksql by confluentinc.

the class StandaloneExecutor method readQueriesFile.

private static String readQueriesFile(final String queryFilePath) {
    final StringBuilder sb = new StringBuilder();
    try (final BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(queryFilePath), StandardCharsets.UTF_8))) {
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
    } catch (IOException e) {
        throw new KsqlException("Could not read the query file. Details: " + e.getMessage(), e);
    }
    return sb.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) KsqlException(io.confluent.ksql.util.KsqlException) FileInputStream(java.io.FileInputStream)

Example 42 with KsqlException

use of io.confluent.ksql.util.KsqlException in project ksql by confluentinc.

the class KsqlDelimitedDeserializer method deserialize.

@Override
public GenericRow deserialize(final String topic, final byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    String recordCsvString = new String(bytes, StandardCharsets.UTF_8);
    try {
        List<CSVRecord> csvRecords = CSVParser.parse(recordCsvString, CSVFormat.DEFAULT).getRecords();
        if (csvRecords == null || csvRecords.isEmpty()) {
            throw new KsqlException("Deserialization error in the delimited line: " + recordCsvString);
        }
        CSVRecord csvRecord = csvRecords.get(0);
        if (csvRecord == null || csvRecord.size() == 0) {
            throw new KsqlException("Deserialization error in the delimited line: " + recordCsvString);
        }
        List<Object> columns = new ArrayList<>();
        if (csvRecord.size() != schema.fields().size()) {
            throw new KsqlException(String.format("Unexpected field count, csvFields:%d schemaFields:%d line: %s", csvRecord.size(), schema.fields().size(), recordCsvString));
        }
        for (int i = 0; i < csvRecord.size(); i++) {
            if (csvRecord.get(i) == null) {
                columns.add(null);
            } else {
                columns.add(enforceFieldType(schema.fields().get(i).schema(), csvRecord.get(i)));
            }
        }
        return new GenericRow(columns);
    } catch (Exception e) {
        throw new SerializationException("Exception in deserializing the delimited row: " + recordCsvString, e);
    }
}
Also used : GenericRow(io.confluent.ksql.GenericRow) SerializationException(org.apache.kafka.common.errors.SerializationException) ArrayList(java.util.ArrayList) CSVRecord(org.apache.commons.csv.CSVRecord) KsqlException(io.confluent.ksql.util.KsqlException) SerializationException(org.apache.kafka.common.errors.SerializationException) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

KsqlException (io.confluent.ksql.util.KsqlException)42 HashMap (java.util.HashMap)9 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)6 Pair (io.confluent.ksql.util.Pair)6 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)5 Expression (io.confluent.ksql.parser.tree.Expression)5 Statement (io.confluent.ksql.parser.tree.Statement)5 ArrayList (java.util.ArrayList)5 CreateTable (io.confluent.ksql.parser.tree.CreateTable)4 StringLiteral (io.confluent.ksql.parser.tree.StringLiteral)4 SchemaKStream (io.confluent.ksql.structured.SchemaKStream)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 CreateStreamCommand (io.confluent.ksql.ddl.commands.CreateStreamCommand)3 RegisterTopicCommand (io.confluent.ksql.ddl.commands.RegisterTopicCommand)3 AbstractStreamCreateStatement (io.confluent.ksql.parser.tree.AbstractStreamCreateStatement)3 CreateStream (io.confluent.ksql.parser.tree.CreateStream)3 Query (io.confluent.ksql.parser.tree.Query)3 TableElement (io.confluent.ksql.parser.tree.TableElement)3 KsqlStructuredDataOutputNode (io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode)3