Java Code Examples for com.streamsets.pipeline.api.Field#getAttribute()

The following examples show how to use com.streamsets.pipeline.api.Field#getAttribute() . 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: RecordEL.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@ElFunction(
    prefix = RECORD_EL_PREFIX,
    name = "fieldAttribute",
    description = "Returns the value of the attribute named 'attributeName' of the field specified by 'fieldPath'")
@SuppressWarnings("unchecked")
public static String getFieldAttributeValue(
    @ElParam("fieldPath") String fieldPath, @ElParam("attributeName") String attributeName) {
  Record record = getRecordInContext();
  if (record != null) {
    Field field = record.get(fieldPath);
    if (field != null) {
      return field.getAttribute(attributeName);
    }
  }
  return null;
}
 
Example 2
Source File: TestXmlCharDataParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private static void assertBookRecord(
    String bookXpath,
    String bookTitle,
    String bookLang,
    String bookPrice,
    Field title,
    Field price,
    String titleXpathPrefix,
    String langXpathPrefix,
    String priceXpathPrefix
) {
  Assert.assertNotNull(title);
  Assert.assertNotNull(price);

  Map<String, Field> titleMap = ApiUtils.firstItemAsMap(title);

  Field titleValueField = titleMap.get(StreamingXmlParser.VALUE_KEY);
  Assert.assertEquals(bookTitle, titleValueField.getValueAsString());
  Assert.assertNotNull(titleValueField.getAttributes());
  Assert.assertEquals(1, titleValueField.getAttributes().size());
  String titleXpath = titleValueField.getAttribute(StreamingXmlParser.XPATH_KEY);
  Assert.assertEquals(bookXpath + "/" + titleXpathPrefix + "title", titleXpath);

  Field titleField = title.getValueAsList().get(0);

  String langField = titleField.getAttribute(StreamingXmlParser.XMLATTR_ATTRIBUTE_PREFIX+"lang");
  Assert.assertEquals(bookLang, langField);

  Map<String, Field> priceMap = ApiUtils.firstItemAsMap(price);
  Field priceField = priceMap.get(StreamingXmlParser.VALUE_KEY);
  Assert.assertEquals(bookPrice, priceField.getValueAsString());
  String priceXpath = priceField.getAttribute(StreamingXmlParser.XPATH_KEY);
  Assert.assertEquals(bookXpath + "/" + priceXpathPrefix + "price", priceXpath);

}
 
Example 3
Source File: TestXmlCharDataParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private static void assertBookRecordOldStyle(
    String bookXpath,
    String bookTitle,
    String bookLang,
    String bookPrice,
    Field title,
    Field price,
    String titleXpathPrefix,
    String langXpathPrefix,
    String priceXpathPrefix
) {
  Assert.assertNotNull(title);
  Assert.assertNotNull(price);

  Map<String, Field> titleMap = ApiUtils.firstItemAsMap(title);

  Field titleField = titleMap.get(StreamingXmlParser.VALUE_KEY);
  Assert.assertEquals(bookTitle, titleField.getValueAsString());
  Assert.assertNotNull(titleField.getAttributes());
  Assert.assertEquals(1, titleField.getAttributes().size());
  String titleXpath = titleField.getAttribute(StreamingXmlParser.XPATH_KEY);
  Assert.assertEquals(bookXpath + "/" + titleXpathPrefix + "title", titleXpath);

  Field langField = titleMap.get(StreamingXmlParser.ATTR_PREFIX_KEY+"lang");
  Assert.assertEquals(bookLang, langField.getValueAsString());
  Assert.assertNotNull(langField.getAttributes());
  Assert.assertEquals(1, langField.getAttributes().size());
  String langXpath = langField.getAttribute(StreamingXmlParser.XPATH_KEY);
  Assert.assertEquals(bookXpath + "/"+ titleXpathPrefix + "title/@" + langXpathPrefix + "lang", langXpath);

  Map<String, Field> priceMap = ApiUtils.firstItemAsMap(price);
  Field priceField = priceMap.get(StreamingXmlParser.VALUE_KEY);
  Assert.assertEquals(bookPrice, priceField.getValueAsString());
  String priceXpath = priceField.getAttribute(StreamingXmlParser.XPATH_KEY);
  Assert.assertEquals(bookXpath + "/" + priceXpathPrefix + "price", priceXpath);

}
 
Example 4
Source File: OffsetQueryUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static Map<String, String> getOffsetsFromColumns(TableRuntimeContext tableContext, Map<String, Field> fields) throws StageException {
  final Map<String, String> offsets = new HashMap<>();
  for (String offsetColumn : tableContext.getSourceTableContext().getOffsetColumns()) {
    Field field = fields.get(offsetColumn);
    String value;

    if(field.getValue() == null) {
      throw new StageException(JdbcErrors.JDBC_408, offsetColumn);
    }

    if (field.getType().isOneOf(Field.Type.DATE, Field.Type.TIME)) {
      //For DATE/TIME fields store the long in string format and convert back to date when using offset
      //in query
      value = String.valueOf(field.getValueAsDatetime().getTime());
    } else if (field.getType() == Field.Type.DATETIME) {
      //DATETIME is similar to above, but there may also be a nanosecond portion (stored in field attr)
      String nanosAttr = field.getAttribute(JdbcUtil.FIELD_ATTRIBUTE_NANOSECONDS);
      int nanos;
      if (StringUtils.isNotBlank(nanosAttr) && StringUtils.isNumeric(nanosAttr)) {
        nanos = Integer.parseInt(nanosAttr);
      } else {
        nanos = 0;
      }
      value = TableContextUtil.getOffsetValueForTimestampParts(field.getValueAsDatetime().getTime(), nanos);
    } else if(field.getType() == Field.Type.ZONED_DATETIME) {
      value = field.getValueAsZonedDateTime().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    } else {
      value = field.getValueAsString();
    }
    offsets.put(offsetColumn, value);
  }
  return offsets;
}
 
Example 5
Source File: JdbcMetastoreUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
/**
 *  Evaluate precision or scale in context of record and given field path.
 */
private static int resolveScaleOrPrecisionExpression(
    String type,
    Field field,
    String attributeName,
    String fieldPath
) throws JdbcStageCheckedException {
  String stringValue = field.getAttribute(attributeName);

  try {
    return Integer.parseInt(stringValue);
  } catch (NumberFormatException e) {
    throw new JdbcStageCheckedException(JdbcErrors.JDBC_304, type, fieldPath, attributeName, stringValue, e);
  }
}
 
Example 6
Source File: AvroSchemaGenerator.java    From datacollector with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve parameters of decimal type.
 */
private int getDecimalScaleOrPrecision(
    Record record,
    Field field,
    String attributeName,
    int defaultValue,
    int minAllowed
) throws OnRecordErrorException {
  int finalValue = -1; // Invalid value

  // Firstly try the field attribute
  String stringValue = field.getAttribute(attributeName);
  if(!StringUtils.isEmpty(stringValue)) {
    finalValue = Integer.valueOf(stringValue);
  }

  // If it's invalid, then use the default value
  if(finalValue < minAllowed) {
    finalValue = defaultValue;
  }

  // If even the default value is invalid, then send the record to error
  if(finalValue < minAllowed) {
    throw new OnRecordErrorException(record, Errors.SCHEMA_GEN_0004, finalValue, field);
  }

  return finalValue;
}