@Test
  public void testAvroScheme() {

    byte[] byteArray = {
      12, 109, 121, 112, 105, 112, 101, 14, 116, 101, 115, 116, 105, 110, 103, -96, 1, 4, 4, 105,
      100, 18, 10, 115, 99, 111, 114, 101, -56, 1, 0, 8, 14, 115, 117, 98, 106, 101, 99, 116, 16,
      114, 101, 108, 105, 103, 105, 111, 110, 16, 108, 97, 115, 116, 110, 97, 109, 101, 10, 115,
      116, 97, 109, 109, 18, 102, 105, 114, 115, 116, 110, 97, 109, 101, 14, 114, 111, 115, 97, 110,
      110, 101, 8, 100, 97, 116, 101, 20, 50, 48, 49, 52, 45, 49, 48, 45, 50, 50, 0, 0
    };

    try {
      SpecificDatumReader<InsertMutation> reader =
          new SpecificDatumReader<>(InsertMutation.getClassSchema());
      Decoder decoder = DecoderFactory.get().binaryDecoder(byteArray, null);
      InsertMutation insertMutation = reader.read(null, decoder);

      LOG.info("ENTRY: " + insertMutation.toString());

      LOG.info("DATABASE NAME: " + insertMutation.getDatabase());
      assertEquals("mypipe", insertMutation.getDatabase().toString());

      LOG.info("TABLE NAME: " + insertMutation.getTable());
      assertEquals("testing", insertMutation.getTable().toString());

      LOG.info("ID: " + AvroSchemaUtils.getIntegerValueByKey(insertMutation.getIntegers(), "id"));
      assertEquals(
          9, (int) AvroSchemaUtils.getIntegerValueByKey(insertMutation.getIntegers(), "id"));

      LOG.info(
          "FIRST NAME: "
              + AvroSchemaUtils.getStringValueByKey(insertMutation.getStrings(), "firstname"));
      assertEquals(
          "rosanne", AvroSchemaUtils.getStringValueByKey(insertMutation.getStrings(), "firstname"));

      LOG.info(
          "LAST NAME: "
              + AvroSchemaUtils.getStringValueByKey(insertMutation.getStrings(), "lastname"));
      assertEquals(
          "stamm", AvroSchemaUtils.getStringValueByKey(insertMutation.getStrings(), "lastname"));

      LOG.info(
          "SUBJECT: "
              + AvroSchemaUtils.getStringValueByKey(insertMutation.getStrings(), "subject"));
      assertEquals(
          "religion", AvroSchemaUtils.getStringValueByKey(insertMutation.getStrings(), "subject"));

      LOG.info(
          "SCORE: " + AvroSchemaUtils.getIntegerValueByKey(insertMutation.getIntegers(), "score"));
      assertEquals(
          100, (int) AvroSchemaUtils.getIntegerValueByKey(insertMutation.getIntegers(), "score"));

      LOG.info("DATE: " + AvroSchemaUtils.getStringValueByKey(insertMutation.getStrings(), "date"));
      assertEquals(
          "2014-10-22", AvroSchemaUtils.getStringValueByKey(insertMutation.getStrings(), "date"));

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemple #2
0
 @SuppressWarnings({"unchecked", "deprecation"})
 public static Object fromBytes(Schema schema, byte[] val) throws IOException {
   Type type = schema.getType();
   switch (type) {
     case ENUM:
       return AvroUtils.getEnumValue(schema, val[0]);
     case STRING:
       return new Utf8(Bytes.toString(val));
     case BYTES:
       return ByteBuffer.wrap(val);
     case INT:
       return Bytes.toInt(val);
     case LONG:
       return Bytes.toLong(val);
     case FLOAT:
       return Bytes.toFloat(val);
     case DOUBLE:
       return Bytes.toDouble(val);
     case BOOLEAN:
       return val[0] != 0;
     case RECORD:
       // TODO: This is TOO SLOW... OPTIMIZE
       reader.setSchema(schema);
       reader.setExpected(schema);
       BinaryDecoder decoder = new BinaryDecoder(new ByteArrayInputStream(val));
       return reader.read(null, decoder);
     default:
       throw new RuntimeException("Unknown type: " + type);
   }
 }
 private void parseTopics() {
   if (state.getProperty(TOPIC_LIST) != null) {
     byte[] data = base64.decodeBase64(state.getProperty(TOPIC_LIST));
     BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(data, null);
     SpecificDatumReader<Topic> avroReader = new SpecificDatumReader<>(Topic.class);
     try { // NOSONAR
       Topic decodedTopic;
       while (!decoder.isEnd()) {
         decodedTopic = avroReader.read(null, decoder);
         LOG.debug("Loaded {}", decodedTopic);
         topicMap.put(decodedTopic.getId(), decodedTopic);
       }
     } catch (Exception e) {
       LOG.error("Unexpected exception occurred while reading information from decoder", e);
     }
   } else {
     LOG.info("No topic list found in state");
   }
 }
Exemple #4
0
  @Test
  public void testSchemolution() /* will not be televised */ throws AvroBaseException, IOException {
    testSaveJsonFormat();
    byte[] row = Bytes.toBytes("spullara");
    HTablePool pool = new HTablePool();
    HTableInterface userTable = pool.getTable(TABLE);
    try {
      Get get = new Get(row);
      Result userRow = userTable.get(get);
      byte[] schemaKey = userRow.getValue(COLUMN_FAMILY, Bytes.toBytes("s"));
      HTableInterface schemaTable = pool.getTable(SCHEMA_TABLE);
      Schema actual;
      try {
        Result schemaRow = schemaTable.get(new Get(schemaKey));
        actual =
            Schema.parse(
                Bytes.toString(schemaRow.getValue(Bytes.toBytes("avro"), Bytes.toBytes("s"))));
      } finally {
        pool.putTable(schemaTable);
      }
      JsonDecoder jd =
          new JsonDecoder(
              actual, Bytes.toString(userRow.getValue(COLUMN_FAMILY, Bytes.toBytes("d"))));

      // Read it as a slightly different schema lacking a field
      InputStream stream = getClass().getResourceAsStream("/User2.json");
      Schema expected = Schema.parse(stream);

      {
        SpecificDatumReader<User> sdr = new SpecificDatumReader<User>();
        sdr.setSchema(actual);
        sdr.setExpected(expected);
        User loaded = sdr.read(null, jd);
        assertEquals("Sam", loaded.firstName.toString());
        assertEquals(null, loaded.mobile);
      }
    } finally {
      pool.putTable(userTable);
    }
  }