Package com.linkedin.data.schema

Examples of com.linkedin.data.schema.DataSchema


        }
        Map.Entry<String, Object> entry = unionMap.entrySet().iterator().next();
        key = entry.getKey();
        memberValue = entry.getValue();
      }
      DataSchema memberDataSchema = unionDataSchema.getType(key);
      if (memberDataSchema == null)
      {
        throw new IllegalArgumentException(message(path, "union value %1$s has invalid member key %2$s", value, key));
      }
      if (memberDataSchema != unionDataSchema.getTypes().get(0))
View Full Code Here


    }

    @Override
    protected Object translateField(List<Object> path, Object fieldValue, RecordDataSchema.Field field)
    {
      DataSchema fieldDataSchema = field.getType();
      boolean isOptional = field.getOptional();
      if (isOptional)
      {
        if (fieldDataSchema.getDereferencedType() != DataSchema.Type.UNION)
        {
          if (fieldValue == null)
          {
            if (_options.getOptionalDefaultMode() != OptionalDefaultMode.TRANSLATE_TO_NULL &&
                field.getDefault() != null)
View Full Code Here

      finally
      {
        is.close();
      }
      String fileSchemaText = new String(bytes);
      DataSchema fileSchema = DataTemplateUtil.parseSchema(fileSchemaText);
      assertTrue(fileSchema instanceof NamedDataSchema);

      // run the generator again
      // verify that output file has not changed
      // test up-to-date
View Full Code Here

      "    { \"name\" : \"e\", \"type\" : \"foo\" },\n" +
      "    { \"name\" : \"f\", \"type\" : { \"type\" : \"map\", \"values\" : \"string\" } },\n" +
      "    { \"name\" : \"u\", \"type\" : [ \"int\", \"double\", \"string\" ] }\n" +
      "  ]\n" +
      "}\n";
    DataSchema schema = DataTemplateUtil.parseSchema(schemaText);

    Object[][] tests =
      {
        {
          DataSchema.Type.INT,
View Full Code Here

      "    { \"name\" : \"e\", \"type\" : \"foo\" },\n" +
      "    { \"name\" : \"f\", \"type\" : { \"name\" : \"xx.F\", \"type\" : \"fixed\", \"size\" : 4 } },\n" +
      "    { \"name\" : \"g\", \"type\" : { \"name\" : \"yy.G\", \"type\" : \"enum\", \"symbols\" : [ \"APPLE\", \"BANANA\", \"ORANGE\" ] } }\n" +
      "  ]\n" +
      "}\n";
    DataSchema schema = DataTemplateUtil.parseSchema(schemaText);

    Object[][] tests =
      {
        {
          "xx.F",
View Full Code Here

    for (Object[] row : inputs)
    {
      String dataSchemaJson = (String) row[0];
      String avroSchemaJson = (String) row[1];

      DataSchema schema = dataSchemaFromString(dataSchemaJson);
      String avroJsonOutput = SchemaTranslator.dataToAvroSchemaJson(schema);
      assertEquals(TestUtil.dataMapFromString(avroJsonOutput), TestUtil.dataMapFromString(avroSchemaJson));
      Schema avroSchema = Schema.parse(avroJsonOutput);
      Schema avroSchema2 = SchemaTranslator.dataToAvroSchema(schema);
      assertEquals(avroSchema, avroSchema2);
View Full Code Here

    }
  }

  /*package*/ static String buildDataSchemaType(final Class<?> type)
  {
    final DataSchema schema = DataTemplateUtil.getSchema(type);
    return buildDataSchemaType(schema);
  }
View Full Code Here

    }
  }

  private static String buildDataSchemaType(final Class<?> type, final DataSchema dataSchema)
  {
    final DataSchema schemaToEncode;
    if (dataSchema instanceof TyperefDataSchema)
    {
      return ((TyperefDataSchema)dataSchema).getFullName();
    }
    else if (dataSchema instanceof PrimitiveDataSchema || dataSchema instanceof NamedDataSchema)
View Full Code Here

      if (avroOverride != null)
      {
        return avroOverride.getCustomDataTranslator().avroGenericToData(this, value, avroSchema, dataSchema);
      }

      DataSchema dereferencedDataSchema = dataSchema.getDereferencedDataSchema();
      DataSchema.Type type = dereferencedDataSchema.getType();
      Object result;
      switch (type)
      {
        case NULL:
          if (value != null)
          {
            appendMessage("value must be null for null schema");
            result = BAD_RESULT;
            break;
          }
          result = Data.NULL;
          break;
        case BOOLEAN:
          result = ((Boolean) value).booleanValue();
          break;
        case INT:
          result = ((Number) value).intValue();
          break;
        case LONG:
          result = ((Number) value).longValue();
          break;
        case FLOAT:
          result = ((Number) value).floatValue();
          break;
        case DOUBLE:
          result = ((Number) value).doubleValue();
          break;
        case STRING:
          result = value.toString();
          break;
        case BYTES:
          ByteBuffer byteBuffer = (ByteBuffer) value;
          ByteString byteString = ByteString.copy(byteBuffer);
          byteBuffer.rewind();
          result = byteString;
          break;
        case ENUM:
          String enumValue = value.toString();
          EnumDataSchema enumDataSchema = (EnumDataSchema) dereferencedDataSchema;
          if (enumDataSchema.getSymbols().contains(enumValue) == false)
          {
            appendMessage("enum value %1$s not one of %2$s", enumValue, enumDataSchema.getSymbols());
            result = BAD_RESULT;
            break;
          }
          result = enumValue;
          break;
        case FIXED:
          GenericFixed fixed = (GenericFixed) value;
          byte[] fixedBytes = fixed.bytes();
          FixedDataSchema fixedDataSchema = (FixedDataSchema) dereferencedDataSchema;
          if (fixedDataSchema.getSize() != fixedBytes.length)
          {
            appendMessage("GenericFixed size %1$d != FixedDataSchema size %2$d",
                          fixedBytes.length,
                          fixedDataSchema.getSize());
            result = BAD_RESULT;
            break;
          }
          byteString = ByteString.copy(fixedBytes);
          result = byteString;
          break;
        case MAP:
          @SuppressWarnings("unchecked")
          Map<?, Object> map = (Map<?, Object>) value;
          DataSchema valueDataSchema = ((MapDataSchema) dereferencedDataSchema).getValues();
          Schema valueAvroSchema = avroSchema.getValueType();
          DataMap dataMap = new DataMap(map.size());
          for (Map.Entry<?, Object> entry : map.entrySet())
          {
            String key = entry.getKey().toString();
            _path.addLast(key);
            Object entryValue = translate(entry.getValue(), valueDataSchema, valueAvroSchema);
            _path.removeLast();
            dataMap.put(key, entryValue);
          }
          result = dataMap;
          break;
        case ARRAY:
          GenericArray<?> list = (GenericArray<?>) value;
          DataSchema elementDataSchema = ((ArrayDataSchema) dereferencedDataSchema).getItems();
          Schema elementAvroSchema = avroSchema.getElementType();
          DataList dataList = new DataList(list.size());
          for (int i = 0; i < list.size(); i++)
          {
            _path.addLast(i);
            Object entryValue = translate(list.get(i), elementDataSchema, elementAvroSchema);
            _path.removeLast();
            dataList.add(entryValue);
          }
          result = dataList;
          break;
        case RECORD:
          GenericRecord record = (GenericRecord) value;
          RecordDataSchema recordDataSchema = (RecordDataSchema) dereferencedDataSchema;
          dataMap = new DataMap(avroSchema.getFields().size());
          for (RecordDataSchema.Field field : recordDataSchema.getFields())
          {
            String fieldName = field.getName();
            Object fieldValue = record.get(fieldName);
            boolean isOptional = field.getOptional();
            if (isOptional && fieldValue == null)
            {
              continue;
            }
            DataSchema fieldDataSchema = field.getType();
            Schema fieldAvroSchema = avroSchema.getField(fieldName).schema();
            if (isOptional && (fieldDataSchema.getDereferencedType() != DataSchema.Type.UNION))
            {
              // Avro schema should be union with 2 types: null and the field's type.
              Map.Entry<String, Schema> fieldAvroEntry = findUnionMember(fieldDataSchema, fieldAvroSchema);
              if (fieldAvroEntry == null)
              {
                continue;
              }
              fieldAvroSchema = fieldAvroEntry.getValue();
            }
            _path.addLast(fieldName);
            dataMap.put(fieldName, translate(fieldValue, fieldDataSchema, fieldAvroSchema));
            _path.removeLast();
          }
          result = dataMap;
          break;
        case UNION:
          UnionDataSchema unionDataSchema = (UnionDataSchema) dereferencedDataSchema;
          Map.Entry<DataSchema, Schema> memberSchemas = findUnionMemberSchema(value, unionDataSchema, avroSchema);
          if (memberSchemas == null)
          {
            result = BAD_RESULT;
            break;
          }
          if (value == null)
          {
            // schema must be "null" schema
            result = Data.NULL;
          }
          else
          {
            DataSchema memberDataSchema = memberSchemas.getKey();
            Schema memberAvroSchema = memberSchemas.getValue();
            String key = memberDataSchema.getUnionMemberKey();
            dataMap = new DataMap(1);
            _path.addLast(key);
            dataMap.put(key, translate(value, memberDataSchema, memberAvroSchema));
            _path.removeLast();
            result = dataMap;
View Full Code Here

          key = memberAvroSchema.getFullName();
          break;
        default:
          key = memberAvroSchema.getType().toString().toLowerCase();
      }
      DataSchema memberDataSchema = unionDataSchema.getType(key);
      if (memberDataSchema == null)
      {
        for (DataSchema dataSchema : unionDataSchema.getTypes())
        {
          AvroOverride avroOverride = getAvroOverride(dataSchema);
View Full Code Here

TOP

Related Classes of com.linkedin.data.schema.DataSchema

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.