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