Search in sources :

Example 1 with KafkaConnectInspectionRequest

use of io.atlasmap.kafkaconnect.v2.KafkaConnectInspectionRequest in project atlasmap by atlasmap.

the class KafkaConnectService method inspect.

/**
 * Inspects a Kafka Connect schema and return a Document object.
 * @param request request
 * @return {@link KafkaConnectInspectionResponse}
 */
public Response inspect(KafkaConnectInspectionRequest request) {
    long startTime = System.currentTimeMillis();
    KafkaConnectInspectionResponse response = new KafkaConnectInspectionResponse();
    KafkaConnectDocument d = null;
    try {
        ClassLoader loader = resourceContext != null ? resourceContext.getResource(AtlasService.class).getLibraryLoader() : KafkaConnectService.class.getClassLoader();
        KafkaConnectInspectionService s = new KafkaConnectInspectionService(loader);
        String schemaTypeStr = request.getOptions().get(KafkaConnectConstants.OPTIONS_SCHEMA_TYPE);
        KafkaConnectSchemaType schemaType = KafkaConnectSchemaType.valueOf(schemaTypeStr);
        HashMap<String, Object> options = KafkaConnectUtil.repackParserOptions(request.getOptions());
        switch(schemaType) {
            case JSON:
                d = s.inspectJson(request.getSchemaData(), options);
                break;
            case AVRO:
                d = s.inspectAvro(request.getSchemaData(), options);
                break;
            default:
                response.setErrorMessage("Unsupported inspection type: " + schemaType);
                break;
        }
    } catch (Exception e) {
        LOG.error("Error inspecting Kafka Connect schema: " + e.getMessage(), e);
        response.setErrorMessage(e.getMessage());
    } finally {
        response.setExecutionTime(System.currentTimeMillis() - startTime);
    }
    response.setKafkaConnectDocument(d);
    return Response.ok().entity(toJson(response)).build();
}
Also used : KafkaConnectSchemaType(io.atlasmap.kafkaconnect.v2.KafkaConnectSchemaType) KafkaConnectInspectionResponse(io.atlasmap.kafkaconnect.v2.KafkaConnectInspectionResponse) KafkaConnectInspectionService(io.atlasmap.kafkaconnect.inspect.KafkaConnectInspectionService) KafkaConnectDocument(io.atlasmap.kafkaconnect.v2.KafkaConnectDocument) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 2 with KafkaConnectInspectionRequest

use of io.atlasmap.kafkaconnect.v2.KafkaConnectInspectionRequest in project atlasmap by atlasmap.

the class KafkaConnectServiceTest method testAvroSchema.

@Test
public void testAvroSchema() throws Exception {
    KafkaConnectInspectionRequest request = new KafkaConnectInspectionRequest();
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("avro-complex.json");
    request.setSchemaData(new String(is.readAllBytes()));
    request.getOptions().put(KafkaConnectConstants.OPTIONS_SCHEMA_TYPE, "AVRO");
    request.getOptions().put(KafkaConnectConstants.OPTIONS_IS_KEY, "true");
    Response res = kafkaConnectService.inspect(request);
    Object entity = res.getEntity();
    assertEquals(byte[].class, entity.getClass());
    KafkaConnectInspectionResponse inspectionResponse = Json.mapper().readValue((byte[]) entity, KafkaConnectInspectionResponse.class);
    assertNull(inspectionResponse.getErrorMessage());
    KafkaConnectDocument doc = inspectionResponse.getKafkaConnectDocument();
    assertNotNull(doc);
    assertEquals(org.apache.kafka.connect.data.Schema.Type.STRUCT, doc.getRootSchemaType());
}
Also used : Response(javax.ws.rs.core.Response) KafkaConnectInspectionResponse(io.atlasmap.kafkaconnect.v2.KafkaConnectInspectionResponse) InputStream(java.io.InputStream) KafkaConnectInspectionResponse(io.atlasmap.kafkaconnect.v2.KafkaConnectInspectionResponse) KafkaConnectInspectionRequest(io.atlasmap.kafkaconnect.v2.KafkaConnectInspectionRequest) KafkaConnectDocument(io.atlasmap.kafkaconnect.v2.KafkaConnectDocument) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaConnectDocument (io.atlasmap.kafkaconnect.v2.KafkaConnectDocument)2 KafkaConnectInspectionResponse (io.atlasmap.kafkaconnect.v2.KafkaConnectInspectionResponse)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 KafkaConnectInspectionService (io.atlasmap.kafkaconnect.inspect.KafkaConnectInspectionService)1 KafkaConnectInspectionRequest (io.atlasmap.kafkaconnect.v2.KafkaConnectInspectionRequest)1 KafkaConnectSchemaType (io.atlasmap.kafkaconnect.v2.KafkaConnectSchemaType)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.jupiter.api.Test)1