Search in sources :

Example 51 with BinaryDecoder

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(io.cdap.cdap.api.data.schema.Schema) IOException(java.io.IOException) Decoder(io.cdap.cdap.common.io.Decoder) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder)

Example 52 with BinaryDecoder

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);
}
Also used : MetricValues(io.cdap.cdap.api.metrics.MetricValues) IOException(java.io.IOException) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) TopicId(io.cdap.cdap.proto.id.TopicId) RawMessage(io.cdap.cdap.messaging.data.RawMessage) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)52 BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)42 Test (org.junit.Test)42 PipedInputStream (java.io.PipedInputStream)40 PipedOutputStream (java.io.PipedOutputStream)40 ReflectionDatumReader (io.cdap.cdap.internal.io.ReflectionDatumReader)34 TypeToken (com.google.common.reflect.TypeToken)32 Schema (io.cdap.cdap.api.data.schema.Schema)16 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ImmutableList (com.google.common.collect.ImmutableList)10 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)10 IOException (java.io.IOException)10 List (java.util.List)10 Decoder (io.cdap.cdap.common.io.Decoder)8 ImmutableMap (com.google.common.collect.ImmutableMap)6 Map (java.util.Map)6 ReflectionDatumWriter (io.cdap.cdap.internal.io.ReflectionDatumWriter)4 Predicate (com.google.common.base.Predicate)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 DataSetException (io.cdap.cdap.api.dataset.DataSetException)2