Java Code Examples for com.amazonaws.services.dynamodbv2.model.AttributeValue#getS()

The following examples show how to use com.amazonaws.services.dynamodbv2.model.AttributeValue#getS() . 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: DynamoDBTableReplicator.java    From podyn with Apache License 2.0 6 votes vote down vote up
public static TableColumnType columnTypeFromDynamoValue(AttributeValue typedValue) {
	if(typedValue.getB() != null) {
		return TableColumnType.bytea;
	} else if (typedValue.getBOOL() != null) {
		return TableColumnType.bool;
	} else if (typedValue.getBS() != null) {
		return TableColumnType.jsonb;
	} else if (typedValue.getL() != null) {
		return TableColumnType.jsonb;
	} else if (typedValue.getM() != null) {
		return TableColumnType.jsonb;
	} else if (typedValue.getN() != null) {
		return TableColumnType.numeric;
	} else if (typedValue.getNS() != null) {
		return TableColumnType.jsonb;
	} else if (typedValue.getS() != null) {
		return TableColumnType.text;
	} else if (typedValue.getSS() != null) {
		return TableColumnType.jsonb;
	} else {
		return TableColumnType.text;
	}
}
 
Example 2
Source File: Util.java    From syndesis with Apache License 2.0 6 votes vote down vote up
public static Object getValue(AttributeValue value) {
    if(value.getB() != null) {
        return value.getB();
    } else if (value.getS() != null){
        return value.getS();
    } else if (value.getBOOL() != null){
        return value.getBOOL();
    } else if (value.getBS() != null){
        return value.getBS();
    } else if (value.getL() != null){
        return value.getL();
    } else if (value.getM() != null){
        return value.getM();
    } else if (value.getN() != null){
        return value.getN();
    } else if (value.getNS() != null){
        return value.getNS();
    } else if (value.getSS() != null){
        return value.getSS();
    }
    return null;
}
 
Example 3
Source File: ImmutableAttributeValue.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
public ImmutableAttributeValue(AttributeValue av) {
    s = av.getS();
    n = av.getN();
    b = av.getB() != null ? av.getB().array().clone() : null;
    ns = av.getNS() != null ? new ArrayList<String>(av.getNS()) : null;
    ss = av.getSS() != null ? new ArrayList<String>(av.getSS()) : null;
    bs = av.getBS() != null ? new ArrayList<byte[]>(av.getBS().size()) : null;
    
    if(av.getBS() != null) {
        for(ByteBuffer buf : av.getBS()) {
            if(buf != null) {
                bs.add(buf.array().clone());
            } else {
                bs.add(null);
            }
        }
    }
}
 
Example 4
Source File: DirectKmsMaterialProvider.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts relevant information from {@code context} and uses it to populate fields in
 * {@code kmsEc}. Subclass can override the default implementation to provide an alternative
 * encryption context in calls to KMS. Currently, the default implementation includes these fields:
 * <dl>
 * <dt>{@code HashKeyName}</dt>
 * <dd>{@code HashKeyValue}</dd>
 * <dt>{@code RangeKeyName}</dt>
 * <dd>{@code RangeKeyValue}</dd>
 * <dt>{@link #TABLE_NAME_EC_KEY}</dt>
 * <dd>{@code TableName}</dd>
 */
protected void populateKmsEcFromEc(EncryptionContext context, Map<String, String> kmsEc) {
    final String hashKeyName = context.getHashKeyName();
    if (hashKeyName != null) {
        final AttributeValue hashKey = context.getAttributeValues().get(hashKeyName);
        if (hashKey.getN() != null) {
            kmsEc.put(hashKeyName, hashKey.getN());
        } else if (hashKey.getS() != null) {
            kmsEc.put(hashKeyName, hashKey.getS());
        } else if (hashKey.getB() != null) {
            kmsEc.put(hashKeyName, Base64.encodeToString(toArray(hashKey.getB())));
        } else {
            throw new UnsupportedOperationException("DirectKmsMaterialProvider only supports String, Number, and Binary HashKeys");
        }
    }
    final String rangeKeyName = context.getRangeKeyName();
    if (rangeKeyName != null) {
        final AttributeValue rangeKey = context.getAttributeValues().get(rangeKeyName);
        if (rangeKey.getN() != null) {
            kmsEc.put(rangeKeyName, rangeKey.getN());
        } else if (rangeKey.getS() != null) {
            kmsEc.put(rangeKeyName, rangeKey.getS());
        } else if (rangeKey.getB() != null) {
            kmsEc.put(rangeKeyName, Base64.encodeToString(toArray(rangeKey.getB())));
        } else {
            throw new UnsupportedOperationException("DirectKmsMaterialProvider only supports String, Number, and Binary RangeKeys");
        }
    }

    final String tableName = context.getTableName();
    if (tableName != null) {
        kmsEc.put(TABLE_NAME_EC_KEY, tableName);
    }
}
 
Example 5
Source File: Transaction.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
/**
 * Determines the current lock holder for the given item
 * 
 * @param item must not be null
 * @return the owning transaction id, or null if the item isn't locked
 */
protected static String getOwner(Map<String, AttributeValue> item) {
    if(item == null) {
        throw new IllegalArgumentException();
    }
    AttributeValue itemTxId = item.get(AttributeName.TXID.toString()); 
    if(itemTxId != null && itemTxId.getS() != null) {
        return itemTxId.getS();
    }
    return null;
}
 
Example 6
Source File: AbstractDynamoDBProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
protected Object getAttributeValue(ProcessContext context, PropertyDescriptor propertyType, AttributeValue value) {
    if ( context.getProperty(propertyType).getValue().equals(ALLOWABLE_VALUE_STRING.getValue())) {
        if ( value == null ) return null;
        else return value.getS();
    } else {
        if ( value == null ) return null;
        else return new BigDecimal(value.getN());
    }
}
 
Example 7
Source File: DynamoDBService.java    From Doradus with Apache License 2.0 5 votes vote down vote up
private List<DColumn> loadAttributes(Map<String, AttributeValue> attributeMap,
                                     Predicate<String> colNamePredicate) {
    List<DColumn> columns = new ArrayList<>();
    if (attributeMap != null) {
        for (Map.Entry<String, AttributeValue> mapEntry : attributeMap.entrySet()) {
            String colName = mapEntry.getKey();
            if (!colName.equals(DynamoDBService.ROW_KEY_ATTR_NAME) && // Don't add row key attribute as a column
                            colNamePredicate.test(colName)) {
                AttributeValue attrValue = mapEntry.getValue();
                if (attrValue.getB() != null) {
                    columns.add(new DColumn(colName, Utils.getBytes(attrValue.getB())));
                } else if (attrValue.getS() != null) {
                    String value = attrValue.getS();
                    if (value.equals(DynamoDBService.NULL_COLUMN_MARKER)) {
                        value = "";
                    }
                    columns.add(new DColumn(colName, value));
                } else {
                    throw new RuntimeException("Unknown AttributeValue type: " + attrValue);
                }
            }
        }
    }
    
    // Sort or reverse sort column names.
    Collections.sort(columns, new Comparator<DColumn>() {
        @Override public int compare(DColumn col1, DColumn col2) {
            return col1.getName().compareTo(col2.getName());
        }
    });
    return columns;
}
 
Example 8
Source File: JacksonConverterImpl.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a DynamoDB attribute to a JSON representation.
 *
 * @param av
 *            DynamoDB attribute
 * @param depth
 *            Current JSON depth
 * @return JSON representation of the DynamoDB attribute
 * @throws JacksonConverterException
 *             Unknown DynamoDB type or JSON is too deep
 */
private JsonNode getJsonNode(final AttributeValue av, final int depth) throws JacksonConverterException {
    assertDepth(depth);
    if (av.getS() != null) {
        return JsonNodeFactory.instance.textNode(av.getS());
    } else if (av.getN() != null) {
        try {
            return JsonNodeFactory.instance.numberNode(Integer.parseInt(av.getN()));
        } catch (final NumberFormatException e) {
            // Not an integer
            try {
                return JsonNodeFactory.instance.numberNode(Float.parseFloat(av.getN()));
            } catch (final NumberFormatException e2) {
                // Not a number
                throw new JacksonConverterException(e.getMessage());
            }
        }
    } else if (av.getBOOL() != null) {
        return JsonNodeFactory.instance.booleanNode(av.getBOOL());
    } else if (av.getNULL() != null) {
        return JsonNodeFactory.instance.nullNode();
    } else if (av.getL() != null) {
        return listToJsonArray(av.getL(), depth);
    } else if (av.getM() != null) {
        return mapToJsonObject(av.getM(), depth);
    } else {
        throw new JacksonConverterException("Unknown type value " + av);
    }
}
 
Example 9
Source File: TransactionDynamoDBFacade.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
private static boolean expectedValueMatches(AttributeValue expected, AttributeValue actual) {
    if (expected.getN() != null) {
        return actual.getN() != null && new BigDecimal(expected.getN()).compareTo(new BigDecimal(actual.getN())) == 0;
    } else if (expected.getS() != null || expected.getB() != null) {
        return expected.equals(actual);
    } else {
        throw new IllegalArgumentException("Expect condition using unsupported value type: " + expected);
    }
}
 
Example 10
Source File: DynamoDBUnmarshallerUtil.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
public static SUnmarshaller getStringSUnmarshaller() {
    return new SUnmarshaller() {

        @Override
        public Object unmarshall(final AttributeValue value) {
            return value.getS();
        }
    };

}
 
Example 11
Source File: DynamoDBUnmarshallerUtil.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
public static SUnmarshaller getS3LinkSUnmarshaller(final S3ClientCache s3cc) {
    return new SUnmarshaller() {
        @Override
        public Object unmarshall(final AttributeValue value) {
            if (s3cc == null) {
                throw new IllegalStateException("Mapper must be constructed with S3 AWS Credentials to load S3Link");
            }
            // value should never be null
            final String json = value.getS();
            return S3Link.fromJson(s3cc, json);
        }
    };
}
 
Example 12
Source File: DynamoDSETranslatorJSONBlob.java    From dynamo-cassandra-proxy with Apache License 2.0 5 votes vote down vote up
private Object getAttributeObject(ScalarAttributeType type, AttributeValue value) {
    //Note: only string, number, and binary are allowed for primary keys in dynamodb
    switch (type) {
        case N:
            return Double.parseDouble(value.getN());
        case S:
            return value.getS();
        case B:
            return value.getB();
        default:
            throw new IllegalStateException("Unknown dynamo scalar type: " + type);
    }
}
 
Example 13
Source File: AbstractBuilder.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 5 votes vote down vote up
protected StaticBuffer decodeKey(final Map<String, AttributeValue> key, final String name) {
    if (null == key || !key.containsKey(name)) {
        return null;
    }
    final AttributeValue attributeValue = key.get(name);
    final String value = attributeValue.getS();
    return decodeKey(value);
}
 
Example 14
Source File: AttributeValueCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(AttributeValue value, OutputStream outStream) throws IOException {

  if (value.getS() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.s.toString(), outStream);
    StringUtf8Coder.of().encode(value.getS(), outStream);
  } else if (value.getN() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.n.toString(), outStream);
    StringUtf8Coder.of().encode(value.getN(), outStream);
  } else if (value.getBOOL() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.bOOL.toString(), outStream);
    BooleanCoder.of().encode(value.getBOOL(), outStream);
  } else if (value.getB() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.b.toString(), outStream);
    ByteArrayCoder.of().encode(convertToByteArray(value.getB()), outStream);
  } else if (value.getSS() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.sS.toString(), outStream);
    LIST_STRING_CODER.encode(value.getSS(), outStream);
  } else if (value.getNS() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.nS.toString(), outStream);
    LIST_STRING_CODER.encode(value.getNS(), outStream);
  } else if (value.getBS() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.bS.toString(), outStream);
    LIST_BYTE_CODER.encode(convertToListByteArray(value.getBS()), outStream);
  } else if (value.getL() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.l.toString(), outStream);
    LIST_ATTRIBUTE_CODER.encode(value.getL(), outStream);
  } else if (value.getM() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.m.toString(), outStream);
    MAP_ATTRIBUTE_CODER.encode(value.getM(), outStream);
  } else if (value.getNULL() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.nULLValue.toString(), outStream);
    BooleanCoder.of().encode(value.getNULL(), outStream);
  } else {
    throw new CoderException("Unknown Type");
  }
}
 
Example 15
Source File: GenericDynamoDB.java    From strongbox with Apache License 2.0 5 votes vote down vote up
private String getValueAsString(AttributeValue attributeValue) {
    if (attributeValue.getS() != null) {
        return attributeValue.getS();
    } else if (attributeValue.getN() != null) {
        return attributeValue.getN();
    } else {
        throw new ParseException("Attribute value could not be extracted.");
    }
}
 
Example 16
Source File: TransactionItem.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the state of the transaction item.  Keep in mind that the current state is never truly known until you try to perform an action,
 * so be careful with how you use this information.
 * 
 * @return
 */
public State getState() {
    AttributeValue stateVal = txItem.get(AttributeName.STATE.toString());
    String txState = (stateVal != null) ? stateVal.getS() : null;
    
    if(STATE_COMMITTED.equals(txState)) {
        return State.COMMITTED;
    } else if(STATE_ROLLED_BACK.equals(txState)) {
        return State.ROLLED_BACK;
    } else if(STATE_PENDING.equals(txState)) {
        return State.PENDING;
    } else {
        throw new TransactionAssertionException(txId, "Unrecognized transaction state: " + txState);
    }
}
 
Example 17
Source File: AttributeValueMarshaller.java    From aws-dynamodb-encryption-java with Apache License 2.0 4 votes vote down vote up
private static void marshall(final AttributeValue attributeValue, final DataOutputStream out)
        throws IOException {
    
    if (attributeValue.getB() != null) {
        out.writeChar('b');
        writeBytes(attributeValue.getB(), out);
    } else if (attributeValue.getBS() != null) {
        out.writeChar('B');
        writeBytesList(attributeValue.getBS(), out);
    } else if (attributeValue.getN() != null) {
        out.writeChar('n');
        writeString(trimZeros(attributeValue.getN()), out);
    } else if (attributeValue.getNS() != null) {
        out.writeChar('N');

        final List<String> ns = new ArrayList<String>(attributeValue.getNS().size());
        for (final String n : attributeValue.getNS()) {
            ns.add(trimZeros(n));
        }
        writeStringList(ns, out);
    } else if (attributeValue.getS() != null) {
        out.writeChar('s');
        writeString(attributeValue.getS(), out);
    } else if (attributeValue.getSS() != null) {
        out.writeChar('S');
        writeStringList(attributeValue.getSS(), out);
    } else if (attributeValue.getBOOL() != null) {
        out.writeChar('?');
        out.writeByte((attributeValue.getBOOL() ? TRUE_FLAG : FALSE_FLAG));
    } else if (Boolean.TRUE.equals(attributeValue.getNULL())) {
        out.writeChar('\0');
    } else if (attributeValue.getL() != null) {
        final List<AttributeValue> l = attributeValue.getL();
        out.writeChar('L');
        out.writeInt(l.size());
        for (final AttributeValue attr : l) {
            if (attr == null) {
                throw new NullPointerException(
                    "Encountered null list entry value while marshalling attribute value "
                    + attributeValue);
            }
            marshall(attr, out);
        }
    } else if (attributeValue.getM() != null) {
        final Map<String, AttributeValue> m = attributeValue.getM();
        final List<String> mKeys = new ArrayList<String>(m.keySet());
        Collections.sort(mKeys);
        out.writeChar('M');
        out.writeInt(m.size());
        for (final String mKey : mKeys) {
            marshall(new AttributeValue().withS(mKey), out);
            
            final AttributeValue mValue = m.get(mKey);
            
            if (mValue == null) {
                throw new NullPointerException(
                    "Encountered null map value for key "
                    + mKey
                    + " while marshalling attribute value "
                    + attributeValue);
            }
            marshall(mValue, out);
        }
    } else {
        throw new IllegalArgumentException("A seemingly empty AttributeValue is indicative of invalid input or potential errors");
    }
}
 
Example 18
Source File: AttributeValueMarshaller.java    From aws-dynamodb-encryption-java with Apache License 2.0 4 votes vote down vote up
private static AttributeValue unmarshall(final DataInputStream in) throws IOException {
    char type = in.readChar();
    AttributeValue result = new AttributeValue();
    switch (type) {
    case '\0':
        result.setNULL(Boolean.TRUE);
        break;
    case 'b':
        result.setB(readBytes(in));
        break;
    case 'B':
        result.setBS(readBytesList(in));
        break;
    case 'n':
        result.setN(readString(in));
        break;
    case 'N':
        result.setNS(readStringList(in));
        break;
    case 's':
        result.setS(readString(in));
        break;
    case 'S':
        result.setSS(readStringList(in));
        break;
    case '?':
        final byte boolValue = in.readByte();

        if (boolValue == TRUE_FLAG) {
            result.setBOOL(Boolean.TRUE);
        } else if (boolValue == FALSE_FLAG) {
            result.setBOOL(Boolean.FALSE);
        } else {
            throw new IllegalArgumentException("Improperly formatted data");
        }
        break;
    case 'L':
        final int lCount = in.readInt();
        final List<AttributeValue> l = new ArrayList<AttributeValue>(lCount);
        for (int lIdx = 0; lIdx < lCount; lIdx++) {
            l.add(unmarshall(in));
        }
        result.setL(l);
        break;
    case 'M':
        final int mCount = in.readInt();
        final Map<String, AttributeValue> m = new HashMap<String, AttributeValue>();
        for (int mIdx = 0; mIdx < mCount; mIdx++) {
            final AttributeValue key = unmarshall(in);
            if (key.getS() == null) {
                throw new IllegalArgumentException("Improperly formatted data");
            }
            AttributeValue value = unmarshall(in);
            m.put(key.getS(), value);
        }
        result.setM(m);
        break;
    default:
        throw new IllegalArgumentException("Unsupported data encoding");
    }

    return result;
}
 
Example 19
Source File: DynamoDbDelegate.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method that can clone an Attribute Value
 *
 * @param val the AttributeValue to copy
 * @param sourceDestinationMap used to avoid loops by keeping track of references
 * @return a copy of val
 */
public static AttributeValue clone(final AttributeValue val, final IdentityHashMap<AttributeValue, AttributeValue> sourceDestinationMap) {
    if (val == null) {
        return null;
    }

    if (sourceDestinationMap.containsKey(val)) {
        return sourceDestinationMap.get(val);
    }

    final AttributeValue clonedVal = new AttributeValue();
    sourceDestinationMap.put(val, clonedVal);
    if (val.getN() != null) {
        clonedVal.setN(val.getN());
    } else if (val.getS() != null) {
        clonedVal.setS(val.getS());
    } else if (val.getB() != null) {
        clonedVal.setB(val.getB());
    } else if (val.getNS() != null) {
        clonedVal.setNS(val.getNS());
    } else if (val.getSS() != null) {
        clonedVal.setSS(val.getSS());
    } else if (val.getBS() != null) {
        clonedVal.setBS(val.getBS());
    } else if (val.getBOOL() != null) {
        clonedVal.setBOOL(val.getBOOL());
    } else if (val.getNULL() != null) {
        clonedVal.setNULL(val.getNULL());
    } else if (val.getL() != null) {
        final List<AttributeValue> list = new ArrayList<>(val.getL().size());
        for (AttributeValue listItemValue : val.getL()) {
            if (!sourceDestinationMap.containsKey(listItemValue)) {
                sourceDestinationMap.put(listItemValue, clone(listItemValue, sourceDestinationMap));
            }
            list.add(sourceDestinationMap.get(listItemValue));
        }
        clonedVal.setL(list);
    } else if (val.getM() != null) {
        final Map<String, AttributeValue> map = new HashMap<>(val.getM().size());
        for (Entry<String, AttributeValue> pair : val.getM().entrySet()) {
            if (!sourceDestinationMap.containsKey(pair.getValue())) {
                sourceDestinationMap.put(pair.getValue(), clone(pair.getValue(), sourceDestinationMap));
            }
            map.put(pair.getKey(), sourceDestinationMap.get(pair.getValue()));
        }
        clonedVal.setM(map);
    }
    return clonedVal;
}
 
Example 20
Source File: HiveDynamoDBStringType.java    From emr-dynamodb-connector with Apache License 2.0 4 votes vote down vote up
@Override
public Object getHiveData(AttributeValue data, ObjectInspector objectInspector) {
  return data.getS();
}