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);
}
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();
}
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));
}
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());
}
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;
}
Aggregations