Search in sources :

Example 16 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class NlpSentimentOperator method open.

@Override
public void open() throws TexeraException {
    if (cursor != CLOSED) {
        return;
    }
    if (inputOperator == null) {
        throw new DataflowException(ErrorMessages.INPUT_OPERATOR_NOT_SPECIFIED);
    }
    inputOperator.open();
    Schema inputSchema = inputOperator.getOutputSchema();
    // check if input schema is present
    if (!inputSchema.containsAttribute(predicate.getInputAttributeName())) {
        throw new TexeraException(String.format("input attribute %s is not in the input schema %s", predicate.getInputAttributeName(), inputSchema.getAttributeNames()));
    }
    // check if attribute type is valid
    AttributeType inputAttributeType = inputSchema.getAttribute(predicate.getInputAttributeName()).getType();
    boolean isValidType = inputAttributeType.equals(AttributeType.STRING) || inputAttributeType.equals(AttributeType.TEXT);
    if (!isValidType) {
        throw new TexeraException(String.format("input attribute %s must have type String or Text, its actual type is %s", predicate.getInputAttributeName(), inputAttributeType));
    }
    // generate output schema by transforming the input schema
    outputSchema = transformSchema(inputOperator.getOutputSchema());
    cursor = OPENED;
    // setup NLP sentiment analysis pipeline
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
    sentimentPipeline = new StanfordCoreNLP(props);
}
Also used : AttributeType(edu.uci.ics.texera.api.schema.AttributeType) Schema(edu.uci.ics.texera.api.schema.Schema) DataflowException(edu.uci.ics.texera.api.exception.DataflowException) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) Properties(java.util.Properties) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP)

Example 17 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class ExcelSink method open.

@Override
public void open() throws TexeraException {
    if (cursor != CLOSED) {
        return;
    }
    inputOperator.open();
    inputSchema = inputOperator.getOutputSchema();
    outputSchema = new Schema(inputSchema.getAttributes().stream().filter(attr -> !attr.getName().equalsIgnoreCase(SchemaConstants._ID)).filter(attr -> !attr.getName().equalsIgnoreCase(SchemaConstants.PAYLOAD)).filter(attr -> !attr.getType().equals(AttributeType.LIST)).toArray(Attribute[]::new));
    wb = new XSSFWorkbook();
    DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
    fileName = df.format(new Date()) + ".xlsx";
    try {
        if (Files.notExists(excelIndexDirectory)) {
            Files.createDirectories(excelIndexDirectory);
        }
        fileOut = new FileOutputStream(excelIndexDirectory.resolve(fileName).toString());
    } catch (IOException e) {
        throw new DataflowException(e);
    }
    sheet = wb.createSheet("new sheet");
    Row row = sheet.createRow(0);
    List<String> attributeNames = outputSchema.getAttributeNames();
    for (int i = 0; i < attributeNames.size(); i++) {
        String attributeName = attributeNames.get(i);
        row.createCell(i).setCellValue(attributeName);
    }
    cursor = OPENED;
}
Also used : Date(java.util.Date) Tuple(edu.uci.ics.texera.api.tuple.Tuple) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) SimpleDateFormat(java.text.SimpleDateFormat) ArrayList(java.util.ArrayList) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) SchemaConstants(edu.uci.ics.texera.api.constants.SchemaConstants) IOperator(edu.uci.ics.texera.api.dataflow.IOperator) IField(edu.uci.ics.texera.api.field.IField) ISink(edu.uci.ics.texera.api.dataflow.ISink) Cell(org.apache.poi.ss.usermodel.Cell) DataflowException(edu.uci.ics.texera.api.exception.DataflowException) Schema(edu.uci.ics.texera.api.schema.Schema) Attribute(edu.uci.ics.texera.api.schema.Attribute) Path(java.nio.file.Path) DateFormat(java.text.DateFormat) IntegerField(edu.uci.ics.texera.api.field.IntegerField) Sheet(org.apache.poi.ss.usermodel.Sheet) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) DoubleField(edu.uci.ics.texera.api.field.DoubleField) DateField(edu.uci.ics.texera.api.field.DateField) List(java.util.List) Workbook(org.apache.poi.ss.usermodel.Workbook) Utils(edu.uci.ics.texera.api.utils.Utils) AttributeType(edu.uci.ics.texera.api.schema.AttributeType) Row(org.apache.poi.ss.usermodel.Row) Schema(edu.uci.ics.texera.api.schema.Schema) IOException(java.io.IOException) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) DataflowException(edu.uci.ics.texera.api.exception.DataflowException) Row(org.apache.poi.ss.usermodel.Row) SimpleDateFormat(java.text.SimpleDateFormat)

Example 18 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class MysqlSink method open.

/**
 * Filter the input tuples to removie _id and list fields Setup JDBC
 * connection. Drop previous testTable and create new testTable based on
 * output schema
 */
@Override
public void open() throws TexeraException {
    if (cursor == OPENED) {
        return;
    }
    inputOperator.open();
    Schema inputSchema = inputOperator.getOutputSchema();
    outputSchema = new Schema(inputSchema.getAttributes().stream().filter(attr -> !attr.getName().equalsIgnoreCase(SchemaConstants._ID)).filter(attr -> !attr.getName().equalsIgnoreCase(SchemaConstants.PAYLOAD)).filter(attr -> !attr.getType().equals(AttributeType.LIST)).toArray(Attribute[]::new));
    // JDBC connection
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String url = "jdbc:mysql://" + predicate.getHost() + ":" + predicate.getPort() + "/" + predicate.getDatabase() + "?autoReconnect=true&useSSL=true";
        this.connection = DriverManager.getConnection(url, predicate.getUsername(), predicate.getPassword());
        statement = connection.createStatement();
        mysqlDropTable();
        mysqlCreateTable();
        cursor = OPENED;
    } catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new DataflowException("MysqlSink failed to connect to mysql database." + e.getMessage());
    }
}
Also used : Connection(java.sql.Connection) Tuple(edu.uci.ics.texera.api.tuple.Tuple) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) PreparedStatement(java.sql.PreparedStatement) Collectors(java.util.stream.Collectors) DoubleField(edu.uci.ics.texera.api.field.DoubleField) ArrayList(java.util.ArrayList) DateField(edu.uci.ics.texera.api.field.DateField) SQLException(java.sql.SQLException) List(java.util.List) Stream(java.util.stream.Stream) SchemaConstants(edu.uci.ics.texera.api.constants.SchemaConstants) IOperator(edu.uci.ics.texera.api.dataflow.IOperator) IField(edu.uci.ics.texera.api.field.IField) ISink(edu.uci.ics.texera.api.dataflow.ISink) Statement(java.sql.Statement) DataflowException(edu.uci.ics.texera.api.exception.DataflowException) AttributeType(edu.uci.ics.texera.api.schema.AttributeType) Schema(edu.uci.ics.texera.api.schema.Schema) Attribute(edu.uci.ics.texera.api.schema.Attribute) DriverManager(java.sql.DriverManager) IntegerField(edu.uci.ics.texera.api.field.IntegerField) SQLException(java.sql.SQLException) Schema(edu.uci.ics.texera.api.schema.Schema) DataflowException(edu.uci.ics.texera.api.exception.DataflowException)

Example 19 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class PlanStoreTest method assertPlanEquivalence.

/**
 * This function checks whether the two given logical plan JSON strings are equivalent
 * @param plan1 - The first Logical Plan JSON
 * @param plan2 - The second Logical Plan JSON
 */
public static void assertPlanEquivalence(String plan1, String plan2) {
    Assert.assertNotNull(plan1);
    Assert.assertNotNull(plan2);
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode1 = objectMapper.readValue(plan1, JsonNode.class);
        JsonNode jsonNode2 = objectMapper.readValue(plan2, JsonNode.class);
        Assert.assertEquals(jsonNode1, jsonNode2);
    } catch (IOException e) {
        throw new TexeraException(e);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 20 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class PlanStoreResource method addQueryPlan.

@POST
public TexeraWebResponse addQueryPlan(String queryPlanBeanJson) {
    try {
        QueryPlanBean queryPlanBean = new ObjectMapper().readValue(queryPlanBeanJson, QueryPlanBean.class);
        // Adding the query plan to the PlanStore
        planStore.addPlan(queryPlanBean.getName(), queryPlanBean.getDescription(), mapper.writeValueAsString(queryPlanBean.getQueryPlan()));
    } catch (TexeraException e) {
        throw new TexeraWebException(e.getMessage());
    } catch (IOException e) {
        throw new TexeraWebException(e.getMessage());
    }
    return new TexeraWebResponse(0, "Success");
}
Also used : TexeraWebResponse(edu.uci.ics.texera.web.response.TexeraWebResponse) IOException(java.io.IOException) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) TexeraWebException(edu.uci.ics.texera.web.TexeraWebException) QueryPlanBean(edu.uci.ics.texera.web.response.planstore.QueryPlanBean) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

TexeraException (edu.uci.ics.texera.api.exception.TexeraException)25 DataflowException (edu.uci.ics.texera.api.exception.DataflowException)17 IOException (java.io.IOException)12 Tuple (edu.uci.ics.texera.api.tuple.Tuple)11 Schema (edu.uci.ics.texera.api.schema.Schema)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 AttributeType (edu.uci.ics.texera.api.schema.AttributeType)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Attribute (edu.uci.ics.texera.api.schema.Attribute)5 TexeraWebException (edu.uci.ics.texera.web.TexeraWebException)5 IField (edu.uci.ics.texera.api.field.IField)4 QueryPlanBean (edu.uci.ics.texera.web.response.planstore.QueryPlanBean)4 SchemaConstants (edu.uci.ics.texera.api.constants.SchemaConstants)3 IOperator (edu.uci.ics.texera.api.dataflow.IOperator)3 ISink (edu.uci.ics.texera.api.dataflow.ISink)3 StorageException (edu.uci.ics.texera.api.exception.StorageException)3 IntegerField (edu.uci.ics.texera.api.field.IntegerField)3 Collectors (java.util.stream.Collectors)3