Search in sources :

Example 1 with FileMetadataTransformResponseModifier

use of com.thinkbiganalytics.spark.rest.filemetadata.FileMetadataTransformResponseModifier in project kylo by Teradata.

the class FileMetadataTaskService method findFileMetadataSchemas.

/**
 * Group the files by their respective mime type
 * For each mime type that spark can process create a task to determine the header information
 */
public void findFileMetadataSchemas(ModifiedTransformResponse<FileMetadataResponse> modifiedTransformResponse, FileMetadataTransformResponseModifier resultModifier) {
    FileMetadataCompletionTask result = new FileMetadataCompletionTask(modifiedTransformResponse, resultModifier);
    metadataResultCache.put(result.getTableId(), result);
    Map<String, List<com.thinkbiganalytics.spark.rest.model.FileMetadataResponse.ParsedFileMetadata>> mimeTypeGroup = resultModifier.groupByMimeType();
    List<String> mimeTypes = Lists.newArrayList(mimeTypeGroup.keySet());
    mimeTypes.removeIf(type -> !PARSABLE_MIME_TYPES.contains(type));
    List<SparkShellScriptRunner> tasks = new ArrayList<>();
    for (String mimeType : mimeTypes) {
        List<com.thinkbiganalytics.spark.rest.model.FileMetadataResponse.ParsedFileMetadata> data = mimeTypeGroup.get(mimeType);
        if (mimeType == "application/xml") {
            // need to group by rowtag
            Map<String, List<com.thinkbiganalytics.spark.rest.model.FileMetadataResponse.ParsedFileMetadata>> rowTags = data.stream().collect(Collectors.groupingBy(row -> row.getRowTag()));
            for (Map.Entry<String, List<com.thinkbiganalytics.spark.rest.model.FileMetadataResponse.ParsedFileMetadata>> rows : rowTags.entrySet()) {
                List<String> files = rows.getValue().stream().map(r -> r.getFilePath()).collect(Collectors.toList());
                SparkShellScriptRunner shellScriptRunner = new SparkShellScriptRunner(sparkShellUserProcessService, restClient, getUsername(), FileMetadataSchemaScriptBuilder.getSparkScript(mimeType, rows.getKey(), files), mimeType);
                tasks.add(shellScriptRunner);
                result.addTask(shellScriptRunner, rows.getValue());
            }
        } else {
            List<String> files = data.stream().map(r -> r.getFilePath()).collect(Collectors.toList());
            SparkShellScriptRunner shellScriptRunner = new SparkShellScriptRunner(sparkShellUserProcessService, restClient, getUsername(), FileMetadataSchemaScriptBuilder.getSparkScript(mimeType, null, files), mimeType);
            tasks.add(shellScriptRunner);
            result.addTask(shellScriptRunner, data);
        }
    }
    submitTasks(result, tasks);
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) SparkShellUserProcessService(com.thinkbiganalytics.spark.rest.controller.SparkShellUserProcessService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LoggerFactory(org.slf4j.LoggerFactory) User(org.springframework.security.core.userdetails.User) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) FileMetadataSchemaScriptBuilder(com.thinkbiganalytics.spark.rest.filemetadata.FileMetadataSchemaScriptBuilder) Map(java.util.Map) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Nonnull(javax.annotation.Nonnull) ExecutorService(java.util.concurrent.ExecutorService) SparkShellRestClient(com.thinkbiganalytics.spark.shell.SparkShellRestClient) CyclicBarrier(java.util.concurrent.CyclicBarrier) Logger(org.slf4j.Logger) FileMetadataTransformResponseModifier(com.thinkbiganalytics.spark.rest.filemetadata.FileMetadataTransformResponseModifier) ModifiedTransformResponse(com.thinkbiganalytics.spark.rest.model.ModifiedTransformResponse) BlockingQueue(java.util.concurrent.BlockingQueue) UUID(java.util.UUID) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) FileMetadataResponse(com.thinkbiganalytics.spark.rest.model.FileMetadataResponse) TimeUnit(java.util.concurrent.TimeUnit) SparkShellProxyController(com.thinkbiganalytics.spark.rest.controller.SparkShellProxyController) Component(org.springframework.stereotype.Component) List(java.util.List) SparkShellScriptRunner(com.thinkbiganalytics.spark.rest.controller.SparkShellScriptRunner) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Authentication(org.springframework.security.core.Authentication) ArrayList(java.util.ArrayList) SparkShellScriptRunner(com.thinkbiganalytics.spark.rest.controller.SparkShellScriptRunner) FileMetadataResponse(com.thinkbiganalytics.spark.rest.model.FileMetadataResponse) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 2 with FileMetadataTransformResponseModifier

use of com.thinkbiganalytics.spark.rest.filemetadata.FileMetadataTransformResponseModifier in project kylo by Teradata.

the class FileMetadataTest method testChainedResult.

@Test
public void testChainedResult() {
    setup();
    TransformResponse initialResponse = new TransformResponse();
    initialResponse.setStatus(TransformResponse.Status.SUCCESS);
    TransformQueryResult result = new TransformQueryResult();
    List<QueryResultColumn> columnList = new ArrayList<>();
    columnList.add(newColumn("mimeType"));
    columnList.add(newColumn("delimiter"));
    columnList.add(newColumn("headerCount"));
    columnList.add(newColumn("resource"));
    columnList.add(newColumn("encoding"));
    result.setColumns(columnList);
    List<List<Object>> rows = new ArrayList<>();
    rows.add(newtRow("application/parquet", "file://my/parquet001.parquet"));
    rows.add(newtRow("application/parquet", "file://my/parquet002.parquet"));
    rows.add(newtRow("application/parquet", "file://my/parquet003.parquet"));
    rows.add(newtRow("application/avro", "file://my/avro001.avro"));
    rows.add(newtRow("application/avro", "file://my/avro002.avro"));
    rows.add(newtRow("text/csv", "file://my/test001.csv"));
    rows.add(newtRow("text/csv", "file://my/test002.csv"));
    result.setRows(rows);
    initialResponse.setResults(result);
    initialResponse.setTable(UUID.randomUUID().toString());
    FileMetadataTransformResponseModifier fileMetadataResult = new FileMetadataTransformResponseModifier(trackerService);
    ModifiedTransformResponse<FileMetadataResponse> m = fileMetadataResult.modify(initialResponse);
    FileMetadataResponse response = m.getResults();
    int retryCount = 0;
    long start = System.currentTimeMillis();
    boolean process = response == null;
    while (process) {
        Uninterruptibles.sleepUninterruptibly(1000, TimeUnit.MILLISECONDS);
        response = m.getResults();
        if (response != null) {
            process = false;
        }
        retryCount += 1;
        if (retryCount > 40) {
            process = false;
        }
    }
    long stop = System.currentTimeMillis();
    log.info("Time to get chained response {} ms, Retry Attempts: {}", (stop - start), retryCount);
    Assert.assertEquals(3, response.getDatasets().size());
    Assert.assertEquals(2, response.getDatasets().get("file://my/test001.csv").getFiles().size());
    Assert.assertEquals(3, response.getDatasets().get("file://my/parquet001.parquet").getFiles().size());
    Assert.assertEquals(2, response.getDatasets().get("file://my/avro001.avro").getFiles().size());
}
Also used : ArrayList(java.util.ArrayList) FileMetadataTransformResponseModifier(com.thinkbiganalytics.spark.rest.filemetadata.FileMetadataTransformResponseModifier) TransformQueryResult(com.thinkbiganalytics.spark.rest.model.TransformQueryResult) FileMetadataResponse(com.thinkbiganalytics.spark.rest.model.FileMetadataResponse) TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse) ModifiedTransformResponse(com.thinkbiganalytics.spark.rest.model.ModifiedTransformResponse) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) DefaultQueryResultColumn(com.thinkbiganalytics.discovery.model.DefaultQueryResultColumn) QueryResultColumn(com.thinkbiganalytics.discovery.schema.QueryResultColumn) Test(org.junit.Test)

Example 3 with FileMetadataTransformResponseModifier

use of com.thinkbiganalytics.spark.rest.filemetadata.FileMetadataTransformResponseModifier in project kylo by Teradata.

the class SparkShellProxyController method fileMetadata.

@POST
@Path(FILE_METADATA)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("returns filemetadata based upon the list of file paths in the dataset.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the status of the file-metadata job.", response = TransformResponse.class), @ApiResponse(code = 400, message = "The requested data source does not exist.", response = RestResponseStatus.class), @ApiResponse(code = 500, message = "There was a problem processing the data.", response = RestResponseStatus.class) })
public Response fileMetadata(com.thinkbiganalytics.kylo.catalog.rest.model.DataSet dataSet) {
    TransformRequest request = new TransformRequest();
    DataSet decrypted = catalogModelTransform.decryptOptions(dataSet);
    request.setScript(FileMetadataScalaScriptGenerator.getScript(DataSetUtil.getPaths(decrypted).orElseGet(Collections::emptyList), DataSetUtil.mergeTemplates(decrypted).getOptions()));
    final SparkShellProcess process = getSparkShellProcess();
    return getModifiedTransformResponse(() -> Optional.of(restClient.transform(process, request)), new FileMetadataTransformResponseModifier(fileMetadataTrackerService));
}
Also used : SparkShellProcess(com.thinkbiganalytics.spark.shell.SparkShellProcess) DataSet(com.thinkbiganalytics.kylo.catalog.rest.model.DataSet) Collections(java.util.Collections) TransformRequest(com.thinkbiganalytics.spark.rest.model.TransformRequest) FileMetadataTransformResponseModifier(com.thinkbiganalytics.spark.rest.filemetadata.FileMetadataTransformResponseModifier) 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

FileMetadataTransformResponseModifier (com.thinkbiganalytics.spark.rest.filemetadata.FileMetadataTransformResponseModifier)3 FileMetadataResponse (com.thinkbiganalytics.spark.rest.model.FileMetadataResponse)2 ModifiedTransformResponse (com.thinkbiganalytics.spark.rest.model.ModifiedTransformResponse)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 Lists (com.google.common.collect.Lists)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 DefaultQueryResultColumn (com.thinkbiganalytics.discovery.model.DefaultQueryResultColumn)1 QueryResultColumn (com.thinkbiganalytics.discovery.schema.QueryResultColumn)1 DataSet (com.thinkbiganalytics.kylo.catalog.rest.model.DataSet)1 SparkShellProxyController (com.thinkbiganalytics.spark.rest.controller.SparkShellProxyController)1 SparkShellScriptRunner (com.thinkbiganalytics.spark.rest.controller.SparkShellScriptRunner)1 SparkShellUserProcessService (com.thinkbiganalytics.spark.rest.controller.SparkShellUserProcessService)1 FileMetadataSchemaScriptBuilder (com.thinkbiganalytics.spark.rest.filemetadata.FileMetadataSchemaScriptBuilder)1 TransformQueryResult (com.thinkbiganalytics.spark.rest.model.TransformQueryResult)1 TransformRequest (com.thinkbiganalytics.spark.rest.model.TransformRequest)1 TransformResponse (com.thinkbiganalytics.spark.rest.model.TransformResponse)1 SparkShellProcess (com.thinkbiganalytics.spark.shell.SparkShellProcess)1