Search in sources :

Example 6 with SchemaTypeAdapter

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

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)

Example 7 with SchemaTypeAdapter

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

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 8 with SchemaTypeAdapter

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

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 9 with SchemaTypeAdapter

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

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 10 with SchemaTypeAdapter

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

the class SchemaTest method testGenerateSchema.

@Test
public void testGenerateSchema() throws UnsupportedTypeException {
    Schema schema = (new ReflectionSchemaGenerator()).generate((new TypeToken<Child<Node>>() {
    }).getType());
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    Assert.assertEquals(schema, gson.fromJson(gson.toJson(schema), Schema.class));
}
Also used : SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Schema(io.cdap.cdap.api.data.schema.Schema) Gson(com.google.gson.Gson) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) Test(org.junit.Test)

Aggregations

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