use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.
the class UserIdentityCodec method decode.
@Override
public UserIdentity decode(byte[] data) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
Decoder decoder = new BinaryDecoder(bis);
DatumReader<UserIdentity> reader = readerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, UserIdentity.Schemas.getCurrentSchema());
int readVersion = decoder.readInt();
Schema readSchema = UserIdentity.Schemas.getSchemaVersion(readVersion);
if (readSchema == null) {
throw new IOException("Unknown schema version for AccessTokenIdentifier: " + readVersion);
}
return reader.read(decoder, readSchema);
}
use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.
the class MessagingMetricsCollectionServiceTest method assertMetricsFromMessaging.
private void assertMetricsFromMessaging(final Schema schema, ReflectionDatumReader recordReader, Table<String, String, Long> expected) throws TopicNotFoundException {
// Consume from kafka
final Map<String, MetricValues> metrics = Maps.newHashMap();
for (int i = 0; i < cConf.getInt(Constants.Metrics.MESSAGING_TOPIC_NUM); i++) {
TopicId topicId = NamespaceId.SYSTEM.topic(TOPIC_PREFIX + i);
try (CloseableIterator<RawMessage> iterator = messagingService.prepareFetch(topicId).fetch()) {
while (iterator.hasNext()) {
RawMessage message = iterator.next();
MetricValues metricsRecord = (MetricValues) recordReader.read(new BinaryDecoder(new ByteArrayInputStream(message.getPayload())), schema);
StringBuilder flattenContext = new StringBuilder();
// for verifying expected results, sorting tags
Map<String, String> tags = Maps.newTreeMap();
tags.putAll(metricsRecord.getTags());
for (Map.Entry<String, String> tag : tags.entrySet()) {
flattenContext.append(tag.getKey()).append(".").append(tag.getValue()).append(".");
}
// removing trailing "."
if (flattenContext.length() > 0) {
flattenContext.deleteCharAt(flattenContext.length() - 1);
}
metrics.put(flattenContext.toString(), metricsRecord);
}
} catch (IOException e) {
LOG.info("Failed to decode message to MetricValue. Skipped. {}", e.getMessage());
}
}
Assert.assertEquals(expected.rowKeySet().size(), metrics.size());
checkReceivedMetrics(expected, metrics);
}
Aggregations