Search in sources :

Example 6 with SchemaParserDescriptor

use of com.thinkbiganalytics.discovery.model.SchemaParserDescriptor in project kylo by Teradata.

the class FileMetadataResultModifier method modifySuccessfulResults.

@Override
public void modifySuccessfulResults(ModifiedTransformResponse<FileMetadataResponse> modifiedTransformResponse) {
    FileMetadataResponse fileMetadataResponse = new FileMetadataResponse();
    Map<String, List<FileMetadataResponse.ParsedFileMetadata>> groupedMetadata = response.getResults().getRows().stream().map(objects -> {
        FileMetadata fileMetadata = new FileMetadata();
        String mimeType = (String) getRowValue("mimeType", objects);
        String delimiter = (String) getRowValue("delimiter", objects);
        Integer headerCount = getRowValue("headerCount", objects, Integer.class, 0);
        String filePath = (String) getRowValue("resource", objects);
        String encoding = (String) getRowValue("encoding", objects);
        FileMetadataResponse.ParsedFileMetadata metadata = new FileMetadataResponse.ParsedFileMetadata();
        metadata.setMimeType(mimeType);
        metadata.setDelimiter(delimiter);
        metadata.setEncoding(encoding);
        metadata.setFilePath(filePath);
        return metadata;
    }).collect(Collectors.groupingBy(FileMetadataResponse.ParsedFileMetadata::getKey));
    // create user friendly names from the groupings
    // the key here is going to be
    Map<String, FileMetadataResponse.FileDataSet> fileDatasets = groupedMetadata.keySet().stream().collect(Collectors.toMap(key -> groupedMetadata.get(key).get(0).getFilePath(), key -> {
        FileMetadataResponse.FileDataSet dataSet = new FileMetadataResponse.FileDataSet();
        List<FileMetadataResponse.ParsedFileMetadata> files = groupedMetadata.get(key);
        dataSet.setFiles(files);
        FileMetadataResponse.ParsedFileMetadata firstFile = files.get(0);
        dataSet.setMimeType(firstFile.getMimeType());
        findSchemaParserForMimeType(firstFile.getMimeType()).ifPresent(schemaParserDescriptor -> {
            dataSet.setSchemaParser(schemaParserDescriptor);
            // if the parser has a delimiter property set it
            schemaParserDescriptor.getProperties().stream().filter(property -> property.getObjectProperty().equals("separatorChar")).findFirst().ifPresent(property -> {
                property.setValue(firstFile.getDelimiter());
            });
            Optional<FileSchemaParser> fileSchemaParser = fileSchemaParser(schemaParserDescriptor);
            if (fileSchemaParser.isPresent() && fileSchemaParser.get() instanceof SparkFileSchemaParser) {
                List<String> paths = files.stream().map(parsedFileMetadata -> parsedFileMetadata.getFilePath()).collect(Collectors.toList());
                SampleFileSparkScript sparkScript = ((SparkFileSchemaParser) fileSchemaParser.get()).getSparkScript(paths);
                dataSet.setSparkScript(sparkScript);
            }
        });
        return dataSet;
    }));
    fileMetadataResponse.setDatasets(fileDatasets);
    modifiedTransformResponse.setResults(fileMetadataResponse);
}
Also used : SampleFileSparkScript(com.thinkbiganalytics.discovery.parser.SampleFileSparkScript) Arrays(java.util.Arrays) SchemaParserDescriptor(com.thinkbiganalytics.discovery.model.SchemaParserDescriptor) AbstractTransformResponseModifier(com.thinkbiganalytics.spark.rest.controller.AbstractTransformResponseModifier) ModifiedTransformResponse(com.thinkbiganalytics.spark.rest.model.ModifiedTransformResponse) FileParserFactory(com.thinkbiganalytics.discovery.FileParserFactory) FileMetadata(com.thinkbiganalytics.kylo.metadata.file.FileMetadata) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SparkFileSchemaParser(com.thinkbiganalytics.discovery.parser.SparkFileSchemaParser) FileMetadataResponse(com.thinkbiganalytics.spark.rest.model.FileMetadataResponse) FileSchemaParser(com.thinkbiganalytics.discovery.parser.FileSchemaParser) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) SchemaParserAnnotationTransformer(com.thinkbiganalytics.discovery.rest.controller.SchemaParserAnnotationTransformer) SparkFileSchemaParser(com.thinkbiganalytics.discovery.parser.SparkFileSchemaParser) Optional(java.util.Optional) FileMetadata(com.thinkbiganalytics.kylo.metadata.file.FileMetadata) SampleFileSparkScript(com.thinkbiganalytics.discovery.parser.SampleFileSparkScript) FileMetadataResponse(com.thinkbiganalytics.spark.rest.model.FileMetadataResponse) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with SchemaParserDescriptor

use of com.thinkbiganalytics.discovery.model.SchemaParserDescriptor in project kylo by Teradata.

the class SchemaDiscoveryRestController method sparkScriptForFilesList.

/**
 * Generate the spark script that can parse the passed in file using the passed in "parserDescriptor"
 *
 * @return an object including the name of the file on disk and the generated spark script
 */
@POST
@Path("/spark/files-list")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Determines the schema of the provided file.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the spark script that parses the sample file.", response = Schema.class), @ApiResponse(code = 500, message = "The schema could not be determined.", response = RestResponseStatus.class) })
public Response sparkScriptForFilesList(SparkFilesScript sparkFilesScript) throws Exception {
    SampleFileSparkScript sampleFileSparkScript = null;
    SchemaParserAnnotationTransformer transformer = new SchemaParserAnnotationTransformer();
    try {
        SchemaParserDescriptor descriptor = ObjectMapperSerializer.deserialize(sparkFilesScript.getParserDescriptor(), SchemaParserDescriptor.class);
        FileSchemaParser p = transformer.fromUiModel(descriptor);
        SparkFileSchemaParser sparkFileSchemaParser = (SparkFileSchemaParser) p;
        sparkFileSchemaParser.setDataFrameVariable(sparkFilesScript.getDataFrameVariable());
        sparkFileSchemaParser.setLimit(-1);
        sampleFileSparkScript = sparkFileSchemaParser.getSparkScript(sparkFilesScript.getFiles());
    } catch (PolicyTransformException e) {
        log.warn("Failed to convert parser", e);
        throw new InternalServerErrorException(STRINGS.getString("discovery.transformError"), e);
    }
    if (sampleFileSparkScript == null) {
        log.warn("Failed to convert parser");
        throw new InternalServerErrorException(STRINGS.getString("discovery.transformError"));
    }
    return Response.ok(sampleFileSparkScript).build();
}
Also used : SampleFileSparkScript(com.thinkbiganalytics.discovery.parser.SampleFileSparkScript) SparkFileSchemaParser(com.thinkbiganalytics.discovery.parser.SparkFileSchemaParser) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) PolicyTransformException(com.thinkbiganalytics.policy.PolicyTransformException) FileSchemaParser(com.thinkbiganalytics.discovery.parser.FileSchemaParser) SparkFileSchemaParser(com.thinkbiganalytics.discovery.parser.SparkFileSchemaParser) 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)

Example 8 with SchemaParserDescriptor

use of com.thinkbiganalytics.discovery.model.SchemaParserDescriptor in project kylo by Teradata.

the class SchemaDiscoveryRestControllerTest method testUploadFile.

@Test
public void testUploadFile() {
    SchemaParserDescriptor descriptor = createMockParserDescriptor();
    FormDataMultiPart form = new FormDataMultiPart();
    form.field("parser", descriptor, MediaType.APPLICATION_JSON_TYPE);
    form.field("fileName", "/Shared/marketing/scrap.txt");
    FormDataBodyPart fdp = new FormDataBodyPart("file", new ByteArrayInputStream("ABC".getBytes()), MediaType.APPLICATION_OCTET_STREAM_TYPE);
    form.bodyPart(fdp);
    Response response = target("/v1/schema-discovery/hive/sample-file").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(form, form.getMediaType()), Response.class);
    assertEquals(200, response.getStatus());
    assertNotNull("Expecting schema object", response.readEntity(DefaultHiveSchema.class));
}
Also used : Response(javax.ws.rs.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) DefaultHiveSchema(com.thinkbiganalytics.discovery.model.DefaultHiveSchema) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) SchemaParserDescriptor(com.thinkbiganalytics.discovery.model.SchemaParserDescriptor) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 9 with SchemaParserDescriptor

use of com.thinkbiganalytics.discovery.model.SchemaParserDescriptor in project kylo by Teradata.

the class SchemaDiscoveryRestControllerTest method testUploadFileDirect.

@Test
public void testUploadFileDirect() throws Exception {
    SchemaParserDescriptor descriptor = createMockParserDescriptor();
    SchemaDiscoveryRestController c = new SchemaDiscoveryRestController();
    Response response = c.uploadFile(ObjectMapperSerializer.serialize(descriptor), new ByteArrayInputStream("ABC".getBytes()), null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) SchemaParserDescriptor(com.thinkbiganalytics.discovery.model.SchemaParserDescriptor) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 10 with SchemaParserDescriptor

use of com.thinkbiganalytics.discovery.model.SchemaParserDescriptor in project kylo by Teradata.

the class FileParserFactory method getSparkSchemaParserDescriptors.

/**
 * Returns the list of Schema Parsers transformed for the UI that support Spark
 * @return
 */
public List<SchemaParserDescriptor> getSparkSchemaParserDescriptors() {
    List<FileSchemaParser> parsers = FileParserFactory.instance().listSchemaParsers();
    SchemaParserAnnotationTransformer transformer = new SchemaParserAnnotationTransformer();
    List<SchemaParserDescriptor> list = parsers.stream().map(parser -> transformer.toUIModel(parser)).sorted(SchemaParserDescriptorUtil.compareByNameThenSpark()).collect(Collectors.toList());
    list = SchemaParserDescriptorUtil.keepFirstByName(list);
    return list;
}
Also used : SchemaParserAnnotationTransformer(com.thinkbiganalytics.discovery.rest.controller.SchemaParserAnnotationTransformer) FileSchemaParser(com.thinkbiganalytics.discovery.parser.FileSchemaParser) SchemaParserDescriptor(com.thinkbiganalytics.discovery.model.SchemaParserDescriptor)

Aggregations

SchemaParserDescriptor (com.thinkbiganalytics.discovery.model.SchemaParserDescriptor)13 FileSchemaParser (com.thinkbiganalytics.discovery.parser.FileSchemaParser)7 SparkFileSchemaParser (com.thinkbiganalytics.discovery.parser.SparkFileSchemaParser)5 SampleFileSparkScript (com.thinkbiganalytics.discovery.parser.SampleFileSparkScript)4 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 SchemaParserAnnotationTransformer (com.thinkbiganalytics.discovery.rest.controller.SchemaParserAnnotationTransformer)3 PolicyTransformException (com.thinkbiganalytics.policy.PolicyTransformException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 Consumes (javax.ws.rs.Consumes)3 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)3 POST (javax.ws.rs.POST)3 FileParserFactory (com.thinkbiganalytics.discovery.FileParserFactory)2 AbstractTransformResponseModifier (com.thinkbiganalytics.spark.rest.controller.AbstractTransformResponseModifier)2 FileMetadataResponse (com.thinkbiganalytics.spark.rest.model.FileMetadataResponse)2