Spring Boot and RESTful API(6)Redis Advance and Cassandra Advance and Bean Mappe
SpringBootandRESTfulAPI(6)RedisAdvanceandCassandraAdvanceandBeanMapper
RedisConfigurationUpgrade
Lasttime,IsetuptheRedisTemplateinclassandconfigurationinYAML.Buttheyarenotworkingtogether.
Sohereissomechangestomakethatworking.
RedisConfig.java
packagecom.sillycat.jobsmonitorapi.config;
importorg.springframework.boot.context.properties.ConfigurationProperties;
importorg.springframework.boot.context.properties.EnableConfigurationProperties;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.data.redis.connection.RedisConnectionFactory;
importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;
importorg.springframework.data.redis.core.RedisTemplate;
importorg.springframework.data.redis.serializer.StringRedisSerializer;
importcom.sillycat.jobsmonitorapi.domain.User;
importcom.sillycat.jobsmonitorapi.repository.RedisObjectSerializer;
importredis.clients.jedis.JedisPoolConfig;
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix="spring.redis")
publicclassRedisConfig{
privateStringdatabase;
privateStringhost;
privateIntegerport;
@Bean
JedisConnectionFactoryjedisConnectionFactory(){
JedisPoolConfigpoolConfig=newJedisPoolConfig();
poolConfig.setMaxTotal(5);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
JedisConnectionFactoryob=newJedisConnectionFactory(poolConfig);
ob.setUsePool(true);
ob.setHostName(this.getHost());
ob.setPort(this.getPort());
returnob;
}
@Bean
publicRedisTemplate<String,User>redisTemplate(RedisConnectionFactoryfactory){
RedisTemplate<String,User>template=newRedisTemplate<String,User>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(newStringRedisSerializer());
template.setValueSerializer(newRedisObjectSerializer());
returntemplate;
}
publicStringgetDatabase(){
returndatabase;
}
publicvoidsetDatabase(Stringdatabase){
this.database=database;
}
publicStringgetHost(){
returnhost;
}
publicvoidsetHost(Stringhost){
this.host=host;
}
publicIntegergetPort(){
returnport;
}
publicvoidsetPort(Integerport){
this.port=port;
}
}
Configurationinapplication.yamlorapplication-prod.yaml/application-dev.yaml/application-stage.yaml
spring:
profiles:
active:dev
redis:
database:0
host:localhost
port:6379
CassandraandHector
https://github.com/hector-client/hector
Itseemsthatlibraryisnolongeractive.ButIwanttosetupthattocheckthecolumnsusageinmyoldprojects.
Ihaveanoldprojectcalledeasycassandraserver.TrytosetuptheENVtorunit.
Exceptions
error:errorwhileloadingCharSequence,classfile'/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar(java/lang/CharSequence.class)'isbroken
(badconstantpooltag18atbyte10)
Solution:
RollbacktheJDKto7
Limit1andDescontheTimeforRecords
cassandracql
>createtablebooks(brandCodetext,deviceIDtext,unixtimebigint,notestext,primarykey((brandCode,deviceID),unixtime))withcompactstorageandcompression={'sstable_compression':''}andcompaction={'class':'LeveledCompactionStrategy','sstable_size_in_mb':10}andclusteringorderby(unixtimedesc);
>select*frombooks;
brandcode|deviceid|unixtime|notes
-----------+----------+----------+-------
>insertintobooks(brandCode,deviceID,unixtime,notes)values('sillycat','iphone5',1,'thereisabookthere');
>insertintobooks(brandCode,deviceID,unixtime,notes)values('sillycat','iphone5',2,'updatetoos6.0’);
>insertintobooks(brandCode,deviceID,unixtime,notes)values('sillycat','iphone5',3,'updatetoos7.0');
>insertintobooks(brandCode,deviceID,unixtime,notes)values('sillycat','android',1,'updatetoos2.1');
>insertintobooks(brandCode,deviceID,unixtime,notes)values('sillycat','android',2,'updatetoos2.2');
>select*frombookswherebrandcode='sillycat'anddeviceid='iphone5';
brandcode|deviceid|unixtime|notes
-----------+----------+----------+-----------------------
sillycat|iphone5|3|updatetoos7.0
sillycat|iphone5|2|updatetoos6.0
sillycat|iphone5|1|thereisabookthere
>select*frombookswherebrandcode='sillycat'anddeviceid='iphone5'limit1;
brandcode|deviceid|unixtime|notes
-----------+----------+----------+------------------
sillycat|iphone5|3|updatetoos7.0
ObjectMapping-orika
http://orika-mapper.github.io/orika-docs/intro.html
Learnfromorg.springside.modules.utils.mapper.BeanMapper
IjustcopytheclassfromBeanMapperinspringside,Idonotwanttoincludethewholelibrary.
packagecom.j2c.jobsmonitorapi.dto;
importjava.util.List;
importma.glasnost.orika.MapperFacade;
importma.glasnost.orika.MapperFactory;
importma.glasnost.orika.impl.DefaultMapperFactory;
importma.glasnost.orika.metadata.Type;
importma.glasnost.orika.metadata.TypeFactory;
publicclassBeanMapper{
privatestaticMapperFacademapper;
static{
MapperFactorymapperFactory=newDefaultMapperFactory.Builder().build();
mapper=mapperFactory.getMapperFacade();
}
/**
*简单的复制出新类型对象.
*
*通过source.getClass()获得源Class
*/
publicstatic<S,D>Dmap(Ssource,Class<D>destinationClass){
returnmapper.map(source,destinationClass);
}
/**
*极致性能的复制出新类型对象.
*
*预先通过BeanMapper.getType()静态获取并缓存Type类型,在此处传入
*/
publicstatic<S,D>Dmap(Ssource,Type<S>sourceType,Type<D>destinationType){
returnmapper.map(source,sourceType,destinationType);
}
/**
*简单的复制出新对象列表到ArrayList
*
*不建议使用mapper.mapAsList(Iterable<S>,Class<D>)接口,sourceClass需要反射,实在有点慢
*/
publicstatic<S,D>List<D>mapList(Iterable<S>sourceList,Class<S>sourceClass,Class<D>destinationClass){
returnmapper.mapAsList(sourceList,TypeFactory.valueOf(sourceClass),TypeFactory.valueOf(destinationClass));
}
/**
*极致性能的复制出新类型对象到ArrayList.
*
*预先通过BeanMapper.getType()静态获取并缓存Type类型,在此处传入
*/
publicstatic<S,D>List<D>mapList(Iterable<S>sourceList,Type<S>sourceType,Type<D>destinationType){
returnmapper.mapAsList(sourceList,sourceType,destinationType);
}
/**
*简单复制出新对象列表到数组
*
*通过source.getComponentType()获得源Class
*/
publicstatic<S,D>D[]mapArray(finalD[]destination,finalS[]source,finalClass<D>destinationClass){
returnmapper.mapAsArray(destination,source,destinationClass);
}
/**
*极致性能的复制出新类型对象到数组
*
*预先通过BeanMapper.getType()静态获取并缓存Type类型,在此处传入
*/
publicstatic<S,D>D[]mapArray(D[]destination,S[]source,Type<S>sourceType,Type<D>destinationType){
returnmapper.mapAsArray(destination,source,sourceType,destinationType);
}
/**
*预先获取orika转换所需要的Type,避免每次转换.
*/
publicstatic<E>Type<E>getType(finalClass<E>rawType){
returnTypeFactory.valueOf(rawType);
}
}
Includethethirdpartyjarpom.xml
<orika.version>1.5.1</orika.version>
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>${orika.version}</version>
</dependency>
IntheControllerLayer
JobDtoreturnDto=BeanMapper.map(job,JobDto.class);
returnMono.just(returnDto);
list.add(job1);
list.add(job2);
returnList=BeanMapper.mapList(list,Job.class,JobDto.class);
returnFlux.fromArray(returnList.toArray(newJobDto[returnList.size()]));
References: