Search in sources :

Example 1 with FileSchemaParser

use of com.thinkbiganalytics.discovery.parser.FileSchemaParser in project kylo by Teradata.

the class FileParserFactory method listSchemaParsers.

/**
 * Returns a list of available schema parsers. Parsers are guaranteed to be annotated with @SchemaParser and implement FileSchemaParser interface
 *
 * @return a list of file schema parsers
 */
public List<FileSchemaParser> listSchemaParsers() {
    List<FileSchemaParser> supportedParsers = new ArrayList<>();
    List<Class<SchemaParser>> supportedParsersClazzes = listSchemaParsersClasses();
    for (Class<SchemaParser> clazz : supportedParsersClazzes) {
        try {
            FileSchemaParser newInstance = (FileSchemaParser) clazz.newInstance();
            newInstance = (FileSchemaParser) SpringApplicationContext.autowire(newInstance);
            supportedParsers.add(newInstance);
        } catch (InstantiationException | IllegalAccessException e) {
            log.warn("Failed to instantiate registered schema parser [?]. Missing default constructor?", clazz.getAnnotation(SchemaParser.class).name(), e);
        }
    }
    return supportedParsers;
}
Also used : ArrayList(java.util.ArrayList) FileSchemaParser(com.thinkbiganalytics.discovery.parser.FileSchemaParser) FileSchemaParser(com.thinkbiganalytics.discovery.parser.FileSchemaParser) SchemaParser(com.thinkbiganalytics.discovery.parser.SchemaParser)

Example 2 with FileSchemaParser

use of com.thinkbiganalytics.discovery.parser.FileSchemaParser in project kylo by Teradata.

the class SchemaDiscoveryRestController method getFileParsers.

@GET
@Path("/file-parsers")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Gets the available file parsers.")
@ApiResponses(@ApiResponse(code = 200, message = "Returns the file parsers.", response = SchemaParserDescriptor.class, responseContainer = "List"))
public Response getFileParsers() {
    List<FileSchemaParser> parsers = FileParserFactory.instance().listSchemaParsers();
    List<SchemaParserDescriptor> descriptors = new ArrayList<>();
    SchemaParserAnnotationTransformer transformer = new SchemaParserAnnotationTransformer();
    for (FileSchemaParser parser : parsers) {
        SchemaParserDescriptor descriptor = transformer.toUIModel(parser);
        descriptors.add(descriptor);
    }
    return Response.ok(descriptors).build();
}
Also used : ArrayList(java.util.ArrayList) FileSchemaParser(com.thinkbiganalytics.discovery.parser.FileSchemaParser) SchemaParserDescriptor(com.thinkbiganalytics.discovery.model.SchemaParserDescriptor) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with FileSchemaParser

use of com.thinkbiganalytics.discovery.parser.FileSchemaParser in project kylo by Teradata.

the class SchemaDiscoveryRestController method uploadFile.

@POST
@Path("/hive/sample-file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Determines the schema of the provided file.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the schema.", response = Schema.class), @ApiResponse(code = 500, message = "The schema could not be determined.", response = RestResponseStatus.class) })
public Response uploadFile(@FormDataParam("parser") String parserDescriptor, @FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception {
    Schema schema;
    SchemaParserAnnotationTransformer transformer = new SchemaParserAnnotationTransformer();
    try {
        SchemaParserDescriptor descriptor = ObjectMapperSerializer.deserialize(parserDescriptor, SchemaParserDescriptor.class);
        FileSchemaParser p = transformer.fromUiModel(descriptor);
        // TODO: Detect charset
        schema = p.parse(fileInputStream, Charset.defaultCharset(), TableSchemaType.HIVE);
    } catch (IOException e) {
        throw new WebApplicationException(e.getMessage());
    } catch (PolicyTransformException e) {
        log.warn("Failed to convert parser", e);
        throw new InternalServerErrorException(STRINGS.getString("discovery.transformError"), e);
    }
    return Response.ok(schema).build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Schema(com.thinkbiganalytics.discovery.schema.Schema) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) IOException(java.io.IOException) PolicyTransformException(com.thinkbiganalytics.policy.PolicyTransformException) FileSchemaParser(com.thinkbiganalytics.discovery.parser.FileSchemaParser) SchemaParserDescriptor(com.thinkbiganalytics.discovery.model.SchemaParserDescriptor) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

FileSchemaParser (com.thinkbiganalytics.discovery.parser.FileSchemaParser)3 SchemaParserDescriptor (com.thinkbiganalytics.discovery.model.SchemaParserDescriptor)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ArrayList (java.util.ArrayList)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 SchemaParser (com.thinkbiganalytics.discovery.parser.SchemaParser)1 Schema (com.thinkbiganalytics.discovery.schema.Schema)1 PolicyTransformException (com.thinkbiganalytics.policy.PolicyTransformException)1 IOException (java.io.IOException)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 POST (javax.ws.rs.POST)1 WebApplicationException (javax.ws.rs.WebApplicationException)1