List of usage examples for org.apache.cassandra.serializers MapSerializer serializeValues
public List<ByteBuffer> serializeValues(Map<K, V> map)
From source file:org.elassandra.cluster.InternalCassandraClusterService.java
License:Apache License
/** * Serialize a cassandra typed object.//from w ww .j a va 2 s .c o m * List of list converted to List, see https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html * @param ksName * @param cfName * @param type * @param name * @param value * @param mapper * @return * @throws SyntaxException * @throws ConfigurationException * @throws IOException * @throws JsonMappingException * @throws JsonGenerationException */ public static ByteBuffer serializeType(final String ksName, final String cfName, final AbstractType type, final String name, final Object value, final Mapper mapper) throws SyntaxException, ConfigurationException, JsonGenerationException, JsonMappingException, IOException { if (value == null) { return null; } if (type instanceof UserType) { UserType udt = (UserType) type; ByteBuffer[] components = new ByteBuffer[udt.size()]; int i = 0; if (GEO_POINT_TYPE.equals(ByteBufferUtil.string(udt.name))) { GeoPoint geoPoint = new GeoPoint(); if (value instanceof String) { // parse from string lat,lon (ex: "41.12,-71.34") or geohash (ex:"drm3btev3e86") geoPoint.resetFromString((String) value); } else { // parse from lat, lon fields as map Map<String, Object> mapValue = (Map<String, Object>) value; geoPoint.reset((Double) mapValue.get(GeoPointFieldMapper.Names.LAT), (Double) mapValue.get(GeoPointFieldMapper.Names.LON)); } components[i++] = serializeType(ksName, cfName, udt.fieldType(0), GeoPointFieldMapper.Names.LAT, geoPoint.lat(), null); components[i++] = serializeType(ksName, cfName, udt.fieldType(1), GeoPointFieldMapper.Names.LON, geoPoint.lon(), null); } else if (COMPLETION_TYPE.equals(ByteBufferUtil.string(udt.name))) { // input list<text>, output text, weight int, payload text Map<String, Object> mapValue = (Map<String, Object>) value; components[i++] = serializeType(ksName, cfName, udt.fieldType(0), Fields.CONTENT_FIELD_NAME_INPUT, mapValue.get(Fields.CONTENT_FIELD_NAME_INPUT), null); components[i++] = serializeType(ksName, cfName, udt.fieldType(1), Fields.CONTENT_FIELD_NAME_OUTPUT, mapValue.get(Fields.CONTENT_FIELD_NAME_OUTPUT), null); components[i++] = serializeType(ksName, cfName, udt.fieldType(2), Fields.CONTENT_FIELD_NAME_WEIGHT, new Long((Integer) mapValue.get(Fields.CONTENT_FIELD_NAME_WEIGHT)), null); components[i++] = serializeType(ksName, cfName, udt.fieldType(3), Fields.CONTENT_FIELD_NAME_PAYLOAD, stringify(mapValue.get(Fields.CONTENT_FIELD_NAME_PAYLOAD)), null); } else { Map<String, Object> mapValue = (Map<String, Object>) value; for (int j = 0; j < udt.size(); j++) { String subName = UTF8Type.instance.compose(udt.fieldName(j)); AbstractType<?> subType = udt.fieldType(j); Object subValue = mapValue.get(subName); Mapper subMapper = (mapper instanceof ObjectMapper) ? ((ObjectMapper) mapper).getMapper(subName) : null; components[i++] = serializeType(ksName, cfName, subType, subName, subValue, subMapper); } } return TupleType.buildValue(components); } else if (type instanceof MapType) { MapType mapType = InternalCassandraClusterService.getMapType(ksName, cfName, name); MapSerializer serializer = mapType.getSerializer(); Map map = (Map) value; List<ByteBuffer> buffers = serializer.serializeValues((Map) value); return CollectionSerializer.pack(buffers, map.size(), Server.VERSION_3); } else if (type instanceof CollectionType) { AbstractType elementType = (type instanceof ListType) ? ((ListType) type).getElementsType() : ((SetType) type).getElementsType(); if (elementType instanceof UserType && InternalCassandraClusterService.GEO_POINT_TYPE .equals(ByteBufferUtil.string(((UserType) elementType).name)) && value instanceof List && ((List) value).get(0) instanceof Double) { // geo_point as array of double lon,lat like [1.2, 1.3] UserType udt = (UserType) elementType; List<Double> values = (List<Double>) value; ByteBuffer[] elements = new ByteBuffer[] { serializeType(ksName, cfName, udt.fieldType(0), GeoPointFieldMapper.Names.LAT, values.get(1), null), serializeType(ksName, cfName, udt.fieldType(1), GeoPointFieldMapper.Names.LON, values.get(0), null) }; ByteBuffer geo_point = TupleType.buildValue(elements); return CollectionSerializer.pack(ImmutableList.of(geo_point), 1, Server.VERSION_3); } if (value instanceof Collection) { // list of elementType List<ByteBuffer> elements = new ArrayList<ByteBuffer>(); for (Object v : flattenCollection((Collection) value)) { ByteBuffer bb = serializeType(ksName, cfName, elementType, name, v, mapper); elements.add(bb); } return CollectionSerializer.pack(elements, elements.size(), Server.VERSION_3); } else { // singleton list ByteBuffer bb = serializeType(ksName, cfName, elementType, name, value, mapper); return CollectionSerializer.pack(ImmutableList.of(bb), 1, Server.VERSION_3); } } else { // Native cassandra type, encoded with mapper if available. if (mapper != null && mapper instanceof FieldMapper) { return type.decompose(value((FieldMapper) mapper, value)); } return type.decompose(value); } }
From source file:org.elasticsearch.cassandra.cluster.InternalCassandraClusterService.java
License:Apache License
public ByteBuffer serializeMapAsMap(final String ksName, final String cfName, final String fieldName, final Map<String, Object> source, final ObjectMapper mapper) throws SyntaxException, ConfigurationException { MapType mapType = getMapType(ksName, cfName, fieldName); MapSerializer serializer = mapType.getSerializer(); List<ByteBuffer> buffers = serializer.serializeValues(source); return TupleType.buildValue(buffers.toArray(new ByteBuffer[buffers.size()])); }