Search in sources :

Example 11 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class RecordWithString method testCollection.

@Test
public void testCollection() throws UnsupportedTypeException, IOException {
    List<String> list = Lists.newArrayList("1", "2", "3");
    Schema sourceSchema = new ReflectionSchemaGenerator().generate(new TypeToken<List<String>>() {
    }.getType());
    Schema targetSchema = new ReflectionSchemaGenerator().generate(new TypeToken<Set<String>>() {
    }.getType());
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
    Set<String> set = new ReflectionDatumReader<>(targetSchema, new TypeToken<Set<String>>() {
    }).read(new BinaryDecoder(input), sourceSchema);
    Assert.assertEquals(Sets.newHashSet("1", "2", "3"), set);
    targetSchema = new ReflectionSchemaGenerator().generate(String[].class);
    new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
    String[] array = new ReflectionDatumReader<>(targetSchema, new TypeToken<String[]>() {
    }).read(new BinaryDecoder(input), sourceSchema);
    Assert.assertArrayEquals(new String[] { "1", "2", "3" }, array);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 12 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class UserIdentityCodec method encode.

@Override
public byte[] encode(UserIdentity identifier) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(UserIdentity.Schemas.getVersion());
    DatumWriter<UserIdentity> writer = writerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, UserIdentity.Schemas.getCurrentSchema());
    writer.encode(identifier, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) Encoder(io.cdap.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 13 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class AccessTokenCodec method encode.

@Override
public byte[] encode(AccessToken token) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(AccessToken.Schemas.getVersion());
    DatumWriter<AccessToken> writer = writerFactory.create(ACCESS_TOKEN_TYPE, AccessToken.Schemas.getCurrentSchema());
    writer.encode(token, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) Encoder(io.cdap.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 14 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class KeyIdentifierCodec method encode.

@Override
public byte[] encode(KeyIdentifier keyIdentifier) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(KeyIdentifier.Schemas.getVersion());
    DatumWriter<KeyIdentifier> writer = writerFactory.create(KEY_IDENTIFIER_TYPE, KeyIdentifier.Schemas.getCurrentSchema());
    writer.encode(keyIdentifier, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) Encoder(io.cdap.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 15 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.

the class UserIdentityCodec method encode.

@Override
public byte[] encode(UserIdentity identifier) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(UserIdentity.Schemas.getVersion());
    DatumWriter<UserIdentity> writer = writerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, UserIdentity.Schemas.getCurrentSchema());
    writer.encode(identifier, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) Encoder(io.cdap.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)54 Test (org.junit.Test)46 BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)42 PipedInputStream (java.io.PipedInputStream)40 PipedOutputStream (java.io.PipedOutputStream)40 TypeToken (com.google.common.reflect.TypeToken)34 ReflectionDatumReader (io.cdap.cdap.internal.io.ReflectionDatumReader)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Schema (io.cdap.cdap.api.data.schema.Schema)12 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)12 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 Encoder (io.cdap.cdap.common.io.Encoder)8 ReflectionDatumWriter (io.cdap.cdap.internal.io.ReflectionDatumWriter)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)4 Predicate (com.google.common.base.Predicate)2 DataSetException (io.cdap.cdap.api.dataset.DataSetException)2 Decoder (io.cdap.cdap.common.io.Decoder)2 TypeRepresentation (io.cdap.cdap.internal.io.TypeRepresentation)2