Java Code Examples for cascading.tuple.Fields#size()

The following examples show how to use cascading.tuple.Fields#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TupleTypeInfo.java    From cascading-flink with Apache License 2.0 5 votes vote down vote up
public TupleTypeInfo(Fields schema) {
	super(Tuple.class);

	this.schema = schema;
	this.fieldIndexes = new HashMap<String, Integer>();

	if(schema.isDefined()) {
		this.length = schema.size();
		this.fieldTypes = new LinkedHashMap<String, FieldTypeInfo>(this.length);

		Comparator[] comps = schema.getComparators();
		Class[] typeClasses = schema.getTypesClasses();

		for(int i=0; i<length; i++) {
			String fieldName = getFieldName(i);
			FieldTypeInfo fieldType = getFieldTypeInfo(i, typeClasses, comps);

			this.fieldTypes.put(fieldName, fieldType);
			this.fieldIndexes.put(fieldName, i);
		}
	}
	else {
		if(schema.isUnknown()) {
			this.length = -1;
			this.fieldTypes = new LinkedHashMap<String, FieldTypeInfo>(16);

			this.fieldTypes.put("0", new FieldTypeInfo());
			this.fieldIndexes.put("0", 0);
		}
		else {
			throw new IllegalArgumentException("Unsupported Fields: "+schema);
		}
	}
}
 
Example 2
Source File: DefinedTupleSerializer.java    From cascading-flink with Apache License 2.0 5 votes vote down vote up
public DefinedTupleSerializer(Fields fields, TypeSerializer[] fieldSers) {
	if(!fields.isDefined()) {
		throw new RuntimeException("DefinedTupleSerializer requires defined Fields schema");
	}
	if(fieldSers == null || fieldSers.length != fields.size()) {
		throw new RuntimeException("Exactly one field serializer required for each tuple field.");
	}

	this.fields = fields;
	this.fieldSers = fieldSers;
	this.length = fields.size();
	this.nullFields = new boolean[this.length];
}
 
Example 3
Source File: TupleComparator.java    From plunger with Apache License 2.0 5 votes vote down vote up
TupleComparator(Fields declaredFields, Fields sortFields) {
  if (Fields.merge(declaredFields, sortFields).size() != declaredFields.size()) {
    throw new IllegalArgumentException("Declared fields must contain sort fields: sortFields=" + sortFields
        + ", declaredFields=" + declaredFields);
  }
  this.declaredFields = declaredFields;
  this.sortFields = sortFields;
}
 
Example 4
Source File: PlungerAssert.java    From plunger with Apache License 2.0 5 votes vote down vote up
public static Matcher<TupleEntry> tupleEntry(Fields fields, Tuple tuple) {
  if (fields.size() != tuple.size()) {
    throw new IllegalArgumentException("Fields size (" + fields.size() + ") does not match tuple size ("
        + tuple.size() + ")");
  }
  return tupleEntry(new TupleEntry(fields, tuple));
}
 
Example 5
Source File: FieldTypeValidator.java    From plunger with Apache License 2.0 5 votes vote down vote up
/**
 * Validates that the passed values can be converted to the types in the passed {@link Fields Fields} but only in
 * cases where the creation of a {@link Tuple Tuple} wouldn't throw an Exception (i.e. we are stricter than Cascading
 * in order to reduce hard to debug type-related errors in tests). For example Cascading will coerce a null or a "4"
 * to the boolean false which Plunger won't allow.
 * 
 * @param fields Fields containing the expected types.
 * @param values Values to be validated against the expected types.
 * @return values or an Object[] with length of fields.size() when values is empty or null.
 * @throws IllegalStateException if the validation fails in any way.
 */
static Object[] validateValues(Fields fields, Object... values) {
  if (values == null || values.length == 0) {
    values = new Object[fields.size()];
  } else if (values.length != fields.size()) {
    throw new IllegalStateException("Value array length not suitable for fields: " + fields);
  }

  for (int i = 0; i < values.length; i++) {
    Class<?> typeClass = fields.getTypeClass(i);
    if (typeClass != null) {
      Object value = values[i];

      if (typeClass.isPrimitive() && value == null) {
        throw new IllegalStateException("null cannot be converted to " + typeClass);
      }

      if (typeClass.isPrimitive() && value != null) {
        Class<?> nonPrimitiveTypeClass = Coercions.asNonPrimitive(typeClass);
        Class<?> nonPrimitiveValueClass = Coercions.asNonPrimitive(value.getClass());
        if (!nonPrimitiveTypeClass.isAssignableFrom(nonPrimitiveValueClass)) {
          throw new IllegalStateException(value.getClass() + " cannot be converted to " + typeClass);
        }
      }
    }
  }
  return values;
}
 
Example 6
Source File: DataBuilder.java    From plunger with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new tuple source builder that will provide {@link Tuple Tuples} that contain values consistent with
 * the declared {@link Fields}. Adding tuples will also coerce types.
 */
public DataBuilder(Fields fields, Class<?>[] types) {
  this.fields = fields;
  fieldMask = fields;
  this.types = types;
  list = new ArrayList<Tuple>();
  if (types != null && types.length != fields.size()) {
    throw new IllegalArgumentException("There must be the same number of types as fields");
  }
}
 
Example 7
Source File: JDBCScheme.java    From SpyGlass with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor JDBCScheme creates a new JDBCScheme instance.
 *
 * @param inputFormatClass  of type Class<? extends DBInputFormat>
 * @param outputFormatClass of type Class<? extends DBOutputFormat>
 * @param columnFields      of type Fields
 * @param columns           of type String[]
 * @param orderBy           of type String[]
 * @param conditions        of type String
 * @param limit             of type long
 * @param updateByFields    of type Fields
 * @param updateBy          of type String[]
 */
public JDBCScheme( Class<? extends DBInputFormat> inputFormatClass, Class<? extends DBOutputFormat> outputFormatClass, Fields columnFields, String[] columns, String[] orderBy, String conditions, long limit, Fields updateByFields, String[] updateBy )
{
    this.columnFields = columnFields;

    verifyColumns( columnFields, columns );

    setSinkFields( columnFields );
    setSourceFields( columnFields );

    if( updateBy != null && updateBy.length != 0 )
    {
        this.updateBy = updateBy;
        this.updateByFields = updateByFields;

        if( updateByFields.size() != updateBy.length )
            throw new IllegalArgumentException( "updateByFields and updateBy must be the same size" );

        if( !this.columnFields.contains( this.updateByFields ) )
            throw new IllegalArgumentException( "columnFields must contain updateByFields column names" );

        this.updateValueFields = columnFields.subtract( updateByFields ).append( updateByFields );
        this.updateIfTuple = Tuple.size( updateByFields.size() ); // all nulls
    }

    this.columns = columns;
    this.orderBy = orderBy;
    this.conditions = conditions;
    this.limit = limit;

    this.inputFormatClass = inputFormatClass;
    this.outputFormatClass = outputFormatClass;
}
 
Example 8
Source File: HBaseScheme.java    From SpyGlass with Apache License 2.0 5 votes vote down vote up
@Override
public boolean source(FlowProcess<JobConf> flowProcess,
    SourceCall<Object[], RecordReader> sourceCall) throws IOException {
  Tuple result = new Tuple();

  Object key = sourceCall.getContext()[0];
  Object value = sourceCall.getContext()[1];
  boolean hasNext = sourceCall.getInput().next(key, value);
  if (!hasNext) { return false; }

  // Skip nulls
  if (key == null || value == null) { return true; }

  ImmutableBytesWritable keyWritable = (ImmutableBytesWritable) key;
  Result row = (Result) value;
  result.add(keyWritable);

  for (int i = 0; i < this.familyNames.length; i++) {
    String familyName = this.familyNames[i];
    byte[] familyNameBytes = Bytes.toBytes(familyName);
    Fields fields = this.valueFields[i];
    for (int k = 0; k < fields.size(); k++) {
      String fieldName = (String) fields.get(k);
      byte[] fieldNameBytes = Bytes.toBytes(fieldName);
      byte[] cellValue = row.getValue(familyNameBytes, fieldNameBytes);
      result.add(cellValue != null ? new ImmutableBytesWritable(cellValue) : null);
    }
  }

  sourceCall.getIncomingEntry().setTuple(result);

  return true;
}
 
Example 9
Source File: JDBCScheme.java    From SpyGlass with Apache License 2.0 4 votes vote down vote up
private void verifyColumns( Fields columnFields, String[] columns )
{
    if( columnFields.size() != columns.length )
        throw new IllegalArgumentException( "columnFields and columns must be the same size" );
}