Search in sources :

Example 1 with SchemaTypeAdapter

use of io.cdap.cdap.internal.io.SchemaTypeAdapter in project cdap by caskdata.

the class DefaultPreviewStore method add.

@Override
public void add(ApplicationId applicationId, AppRequest appRequest, @Nullable Principal principal) {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    long timeInSeconds = RunIds.getTime(applicationId.getApplication(), TimeUnit.SECONDS);
    MDSKey mdsKey = new MDSKey.Builder().add(WAITING).add(timeInSeconds).add(applicationId.getNamespace()).add(applicationId.getApplication()).build();
    try {
        previewQueueTable.putDefaultVersion(mdsKey.getKey(), APPID, Bytes.toBytes(gson.toJson(applicationId)));
        previewQueueTable.putDefaultVersion(mdsKey.getKey(), CONFIG, Bytes.toBytes(gson.toJson(appRequest)));
        previewQueueTable.putDefaultVersion(mdsKey.getKey(), PRINCIPAL, Bytes.toBytes(gson.toJson(principal)));
        long submitTimeInMillis = RunIds.getTime(applicationId.getApplication(), TimeUnit.MILLISECONDS);
        setPreviewStatus(applicationId, new PreviewStatus(PreviewStatus.Status.WAITING, submitTimeInMillis, null, null, null));
    } catch (IOException e) {
        String message = String.format("Error while adding preview request with application '%s' in preview store.", applicationId);
        throw new RuntimeException(message, e);
    }
}
Also used : SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Schema(io.cdap.cdap.api.data.schema.Schema) PreviewStatus(io.cdap.cdap.app.preview.PreviewStatus) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException)

Example 2 with SchemaTypeAdapter

use of io.cdap.cdap.internal.io.SchemaTypeAdapter in project cdap by caskdata.

the class DefaultPreviewStore method put.

@Override
public void put(ApplicationId applicationId, String tracerName, String propertyName, Object value) {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).registerTypeAdapter(StructuredRecord.class, new PreviewJsonSerializer()).create();
    MDSKey mdsKey = getPreviewRowKeyBuilder(DATA_ROW_KEY_PREFIX, applicationId).add(tracerName).add(counter.getAndIncrement()).build();
    try {
        previewTable.putDefaultVersion(mdsKey.getKey(), TRACER, Bytes.toBytes(tracerName));
        previewTable.putDefaultVersion(mdsKey.getKey(), PROPERTY, Bytes.toBytes(propertyName));
        previewTable.putDefaultVersion(mdsKey.getKey(), VALUE, Bytes.toBytes(gson.toJson(value)));
    } catch (IOException e) {
        String message = String.format("Error while putting property '%s' for application '%s' and tracer '%s' in" + " preview table.", propertyName, applicationId, tracerName);
        throw new RuntimeException(message, e);
    }
}
Also used : SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Example 3 with SchemaTypeAdapter

use of io.cdap.cdap.internal.io.SchemaTypeAdapter in project cdap by caskdata.

the class DefaultPreviewStore method get.

@Override
public Map<String, List<JsonElement>> get(ApplicationId applicationId, String tracerName) {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    byte[] startRowKey = getPreviewRowKeyBuilder(DATA_ROW_KEY_PREFIX, applicationId).add(tracerName).build().getKey();
    byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
    Map<String, List<JsonElement>> result = new HashMap<>();
    try (Scanner scanner = previewTable.scan(startRowKey, stopRowKey, null, null, null)) {
        Row indexRow;
        while ((indexRow = scanner.next()) != null) {
            Map<byte[], byte[]> columns = indexRow.getColumns();
            String propertyName = Bytes.toString(columns.get(PROPERTY));
            JsonElement value = gson.fromJson(Bytes.toString(columns.get(VALUE)), JsonElement.class);
            List<JsonElement> values = result.computeIfAbsent(propertyName, k -> new ArrayList<>());
            values.add(value);
        }
    } catch (IOException e) {
        String message = String.format("Error while reading preview data for application '%s' and tracer '%s'.", applicationId, tracerName);
        throw new RuntimeException(message, e);
    }
    return result;
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) GsonBuilder(com.google.gson.GsonBuilder) HashMap(java.util.HashMap) Schema(io.cdap.cdap.api.data.schema.Schema) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) List(java.util.List) Row(io.cdap.cdap.api.dataset.table.Row)

Example 4 with SchemaTypeAdapter

use of io.cdap.cdap.internal.io.SchemaTypeAdapter in project cdap by caskdata.

the class DefaultPreviewStore method getAllInWaitingState.

@Override
public List<PreviewRequest> getAllInWaitingState() {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    byte[] startRowKey = new MDSKey.Builder().add(WAITING).build().getKey();
    byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
    List<PreviewRequest> result = new ArrayList<>();
    try (Scanner scanner = previewQueueTable.scan(startRowKey, stopRowKey, null, null, null)) {
        Row indexRow;
        while ((indexRow = scanner.next()) != null) {
            Map<byte[], byte[]> columns = indexRow.getColumns();
            AppRequest request = gson.fromJson(Bytes.toString(columns.get(CONFIG)), AppRequest.class);
            ApplicationId applicationId = gson.fromJson(Bytes.toString(columns.get(APPID)), ApplicationId.class);
            Principal principal = gson.fromJson(Bytes.toString(columns.get(PRINCIPAL)), Principal.class);
            result.add(new PreviewRequest(applicationId, request, principal));
        }
    } catch (IOException e) {
        throw new RuntimeException("Error while listing the waiting preview requests.", e);
    }
    return result;
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) GsonBuilder(com.google.gson.GsonBuilder) Schema(io.cdap.cdap.api.data.schema.Schema) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) Row(io.cdap.cdap.api.dataset.table.Row) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Principal(io.cdap.cdap.proto.security.Principal)

Example 5 with SchemaTypeAdapter

use of io.cdap.cdap.internal.io.SchemaTypeAdapter in project cdap by caskdata.

the class ApplicationClassCodecTest method testCodec.

@Test
public void testCodec() throws IOException {
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).registerTypeAdapter(ApplicationClass.class, new ApplicationClassCodec()).create();
    String testApp1 = getData("ApplicationClass_6_2.json");
    ApplicationClass applicationClass62 = gson.fromJson(testApp1, ApplicationClass.class);
    Assert.assertEquals(Requirements.EMPTY, applicationClass62.getRequirements());
    Assert.assertEquals("io.cdap.cdap.internal.app.runtime.artifact.app.inspection.InspectionApp", applicationClass62.getClassName());
    Assert.assertEquals("", applicationClass62.getDescription());
    Assert.assertNotNull(applicationClass62.getConfigSchema());
    Assert.assertEquals(applicationClass62, gson.fromJson(gson.toJson(applicationClass62), ApplicationClass.class));
    String testApp2 = getData("ApplicationClass_6_3.json");
    ApplicationClass applicationClass63 = gson.fromJson(testApp2, ApplicationClass.class);
    Requirements requirements = applicationClass63.getRequirements();
    Assert.assertEquals(Collections.singleton("cdc"), requirements.getCapabilities());
    Assert.assertEquals(applicationClass63, gson.fromJson(gson.toJson(applicationClass63), ApplicationClass.class));
}
Also used : SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) Requirements(io.cdap.cdap.api.plugin.Requirements) Test(org.junit.Test)

Aggregations

Gson (com.google.gson.Gson)7 GsonBuilder (com.google.gson.GsonBuilder)7 SchemaTypeAdapter (io.cdap.cdap.internal.io.SchemaTypeAdapter)7 Schema (io.cdap.cdap.api.data.schema.Schema)5 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)5 IOException (java.io.IOException)5 Row (io.cdap.cdap.api.dataset.table.Row)2 Scanner (io.cdap.cdap.api.dataset.table.Scanner)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 JsonElement (com.google.gson.JsonElement)1 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)1 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)1 Requirements (io.cdap.cdap.api.plugin.Requirements)1 PreviewRequest (io.cdap.cdap.app.preview.PreviewRequest)1 PreviewStatus (io.cdap.cdap.app.preview.PreviewStatus)1 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)1 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)1 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)1 Principal (io.cdap.cdap.proto.security.Principal)1