pig/src/main/java/com/twitter/elephantbird/pig/util/PigToProtobuf.java
public static Descriptor schemaToProtoDescriptor(ResourceSchema schema, List<Pair<String, Type>> extraFields)
      throws DescriptorValidationException {

    // init protobufs
    DescriptorProto.Builder desBuilder = DescriptorProto.newBuilder();

    int count = 0;
    for (ResourceFieldSchema fieldSchema : schema.getFields()) {
      // Pig types
      int position = ++count;
      String fieldName = fieldSchema.getName();
      byte dataTypeId = fieldSchema.getType();

      // determine and add protobuf types
      Type protoType = pigTypeToProtoType(dataTypeId);
      LOG.info("Mapping Pig field " + fieldName + " of type " + dataTypeId + " to protobuf type: " + protoType);

      addField(desBuilder, fieldName, position, protoType);
    }

    if (count == 0) {
      throw new IllegalArgumentException("ResourceSchema does not have any fields");
    }

    // If extra fields are needed, let's add them
    if (extraFields != null) {
      for (Pair<String, Type> extraField : extraFields) {
        addField(desBuilder, extraField.first, ++count, extraField.second);
      }
    }

    desBuilder.setName("PigToProtobufDynamicBuilder");

    return Protobufs.makeMessageDescriptor(desBuilder.build());
  }
public static Descriptor schemaToProtoDescriptor(ResourceSchema schema, List<Pair<String, Type>> extraFields)
      throws DescriptorValidationException {

    // init protobufs
    DescriptorProto.Builder desBuilder = DescriptorProto.newBuilder();

    int count = 0;
    for (ResourceFieldSchema fieldSchema : schema.getFields()) {
      // Pig types
      int position = ++count;
      String fieldName = fieldSchema.getName();
      byte dataTypeId = fieldSchema.getType();

      // determine and add protobuf types
      Type protoType = pigTypeToProtoType(dataTypeId);
      LOG.info("Mapping Pig field " + fieldName + " of type " + dataTypeId + " to protobuf type: " + protoType);

      addField(desBuilder, fieldName, position, protoType);
    }

    if (count == 0) {
      throw new IllegalArgumentException("ResourceSchema does not have any fields");
    }

    // If extra fields are needed, let's add them
    if (extraFields != null) {
      for (Pair<String, Type> extraField : extraFields) {
        addField(desBuilder, extraField.first, ++count, extraField.second);
      }
    }

    desBuilder.setName("PigToProtobufDynamicBuilder");

    return Protobufs.makeMessageDescriptor(desBuilder.build());
  }
For a given <code>ResourceSchema</code>, generate a protobufs <code>Descriptor</code> with analogous field names
and types.

@param schema Pig schema.
@param extraFields optionally pass a List of extra fields (Pairs of name:type) to be included.
@return Protobufs Descriptor
@throws Descriptors.DescriptorValidationException