日常积累

日常积累

1.日期相关操作。

1.1字符串转Date类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Demo {
public static void main(String[] args) {
String time = "1994-11-24";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(time);//结果为:Thu Nov 24 00:00:00 CST 1994
System.out.println(sdf.format(new Date()));//结果为字符串:1994-11-24
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

1.2常用注解

  • 1
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

2.java8相关操作

2.1过滤后进行排序操作

  • 注:一个stream进行filter操作后,可以继续调用.collect(Collectors.groupingBy()),但是groupingBy里无法传入lambda表达式?

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    public JSONObject obtainOnlineJson(List deviceDetailList){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Stream dataStream = deviceDetailList.stream()
    .filter(data -> sdf.format(new Date()).equals(data.getGmtOnline().substring(0, 10)));
    Map> onlineMap = dataStream.collect(Collectors.groupingBy(data -> data.getGmtOnline().substring(0, 10)));
    JSONObject online = new JSONObject();
    online.put(sdf.format(new Date()),onlineMap.size());
    return online;
    }

3.Map相关操作

3.1遍历map相关操作

  • 四种方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;

    public class TestMap {
    public static void main(String[] args) {
    Map map = new HashMap();
    map.put(1, "a");
    map.put(2, "b");
    map.put(3, "ab");
    map.put(4, "ab");
    map.put(4, "ab");// 和上面相同 , 会自己筛选
    System.out.println(map.size());
    // 第一种:
    /*
    * Set set = map.keySet(); //得到所有key的集合
    *
    * for (Integer in : set) { String str = map.get(in);
    * System.out.println(in + " " + str); }
    */
    System.out.println("第一种:通过Map.keySet遍历key和value:");
    for (Integer in : map.keySet()) {
    //map.keySet()返回的是所有key的值
    String str = map.get(in);//得到每个key多对用value的值
    System.out.println(in + " " + str);
    }
    // 第二种:
    System.out.println("第二种:通过Map.entrySet使用iterator遍历key和value:");
    Iterator> it = map.entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry entry = it.next();
    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    }
    // 第三种:推荐,尤其是容量大时
    System.out.println("第三种:通过Map.entrySet遍历key和value");
    for (Map.Entry entry : map.entrySet()) {
    //Map.entry 映射项(键-值对) 有几个方法:用上面的名字entry
    //entry.getKey() ;entry.getValue(); entry.setValue();
    //map.entrySet() 返回此映射中包含的映射关系的 Set视图。
    System.out.println("key= " + entry.getKey() + " and value= "
    + entry.getValue());
    }
    // 第四种:
    System.out.println("第四种:通过Map.values()遍历所有的value,但不能遍历key");
    for (String v : map.values()) {
    System.out.println("value= " + v);
    }
    }
    }

4.nginx使用记录

4.1安装与测试

  1. 添加资源库。

    1
    vim /etc/yum.repos.d/nginx.repo(可以直接使用该命令,文件不存在会自动生成的。)
  2. 向第一步打开的nginx.repo文件写入以下内容。

    1
    2
    3
    4
    5
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=0
    enabled=1
  3. yum命令安装nginx。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    yum install nginx
    可以看到以下内容:
    Loaded plugins: fastestmirror, langpacks
    nginx | 2.9 kB 00:00
    nginx/7/x86_64/primary_db | 14 kB 00:01
    Loading mirror speeds from cached hostfile
    * base: mirrors.sina.cn
    * extras: mirrors.btte.net
    * updates: mirrors.sina.cn
    Resolving Dependencies
    --> Running transaction check
    ---> Package nginx.x86_64 1:1.10.1-1.el7.ngx will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    ================================================================================
    Package Arch Version Repository Size
    ================================================================================
    Installing:
    nginx x86_64 1:1.10.1-1.el7.ngx nginx 640 k

    Transaction Summary
    ================================================================================
    Install 1 Package

    Total download size: 640 k
    Installed size: 2.1 M
    Is this ok [y/d/N]: y
    Downloading packages:
    nginx-1.10.1-1.el7.ngx.x86_64.rpm | 640 kB 00:27
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
    Installing : 1:nginx-1.10.1-1.el7.ngx.x86_64 1/1
    ----------------------------------------------------------------------

    Thanks for using nginx!

    Please find the official documentation for nginx here:
    * http://nginx.org/en/docs/

    Commercial subscriptions for nginx are available on:
    * http://nginx.com/products/

    ----------------------------------------------------------------------
    Verifying : 1:nginx-1.10.1-1.el7.ngx.x86_64 1/1

    Installed:
    nginx.x86_64 1:1.10.1-1.el7.ngx

    Complete!
  4. 测试nginx配置文件并查看安装的路径。

    1
    2
    3
    4
    nginx -t
    可以看到:
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
  5. 启动nginx。

    1
    systemctl start nginx.service(无返回内容)
  6. 关闭防火墙。

    1
    systemctl stop firewalld.service
  7. 在浏览器输入nginx安装在宿主机的ip地址,返回welcome nginx则大功告成。

5.redis使用记录

5.1redis多数据源配置

  1. 配置application.yml文件的redis

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    spring:
    datasource:
    url: jdbc:mysql://localhost/service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: ******
    password: ******
    redis:
    host: ******
    password: ******
    redis-c40:
    host: ******
    password: ******
    port: 6379
    database: 0
    redis-c51:
    host: ******
    password: ******
    port: 6379
    database: 1
    redis-c53:
    host: ******
    password: ******
    port: 6379
    database: 2
    pool:
    max-active: 8
    max-idle: 8
    min-idle: 0
    max-wait: 10000
  2. 在config包下新建MultiRedisConfig.java。内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    package com.uaes.iot.hub.config;

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import redis.clients.jedis.JedisPoolConfig;

    @Configuration
    public class MultiRedisConfig {
    @Value("${spring.pool.max-idle}")
    private int maxIdle;
    @Value("${spring.pool.max-active}")
    private int maxActive;
    @Value("${spring.pool.max-wait}")
    private Long maxWait;
    @Value("${spring.pool.min-idle}")
    private int minIdle;

    @Bean(name = "redisTemplateForC40")
    public RedisTemplate redisTemplateForC40(@Value("${spring.redis-c40.host}")String hostName,
    @Value("${spring.redis-c40.port}")int port,
    @Value("${spring.redis-c40.password}")String password,
    @Value("${spring.redis-c40.database}")int database) {
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = jackson2JsonRedisSerializer();
    return redisTemplate(hostName,port,password,database,jackson2JsonRedisSerializer);
    }
    @Bean(name = "redisTemplateForC51")
    public RedisTemplate redisTemplateForC51(@Value("${spring.redis-c51.host}")String hostName,
    @Value("${spring.redis-c51.port}")int port,
    @Value("${spring.redis-c51.password}")String password,
    @Value("${spring.redis-c51.database}")int database) {
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = jackson2JsonRedisSerializer();
    return redisTemplate(hostName,port,password,database,jackson2JsonRedisSerializer);
    }

    @Bean(name = "redisTemplateForC53")
    public RedisTemplate redisTemplateForC53(@Value("${spring.redis-c53.host}")String hostName,
    @Value("${spring.redis-c53.port}")int port,
    @Value("${spring.redis-c53.password}")String password,
    @Value("${spring.redis-c53.database}")int database) {
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = jackson2JsonRedisSerializer();
    return redisTemplate(hostName,port,password,database,jackson2JsonRedisSerializer);
    }

    public Jackson2JsonRedisSerializer jackson2JsonRedisSerializer(){
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    return jackson2JsonRedisSerializer;
    }

    public RedisConnectionFactory connectionFactory(
    String hostName, int port, String password, int maxIdle,int minIdle, int maxTotal, int index, long maxWaitMillis) {
    JedisConnectionFactory jedis = new JedisConnectionFactory();
    jedis.setHostName(hostName);
    jedis.setPort(port);
    jedis.setPassword(password);
    jedis.setDatabase(index);
    jedis.setPoolConfig(this.poolConfig(maxIdle,minIdle, maxTotal, maxWaitMillis));
    jedis.afterPropertiesSet();
    RedisConnectionFactory factory = jedis;
    return factory;
    }

    public RedisTemplate redisTemplate(String hostName, int port, String password, int database,Jackson2JsonRedisSerializer jackson2JsonRedisSerializer){
    RedisTemplate template = new RedisTemplate<>();
    template.setConnectionFactory(this.connectionFactory(
    hostName, port, password, maxIdle, minIdle,maxActive,database,maxWait));
    template.setKeySerializer(jackson2JsonRedisSerializer);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashKeySerializer(jackson2JsonRedisSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
    }

    public JedisPoolConfig poolConfig(int maxIdle, int minIdle,int maxActive, long maxWaitMillis) {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setMaxTotal(maxActive);
    poolConfig.setMaxWaitMillis(maxWaitMillis);
    poolConfig.setTestOnBorrow(false);
    return poolConfig;
    }
    }
  3. 新建测试类进行测试。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    package com.uaes.iot.hub.service.device;

    import com.uaes.iot.hub.HubApplication;
    import com.uaes.iot.hub.domain.CarCurrent;
    import com.uaes.iot.hub.service.car.CarCurrentService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.test.context.junit4.SpringRunner;

    /**
    * Created by xk on 2017/12/14.
    */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = HubApplication.class)
    public class CarCurrentServiceTest {
    @Autowired
    private CarCurrentService carCurrentService;

    @Autowired
    private RedisTemplate redisTemplateForC40;
    @Autowired
    private RedisTemplate redisTemplateForC51;
    @Autowired
    private RedisTemplate redisTemplateForC53;
    @Test
    public void testMultiRedis(){
    Object test1 = redisTemplateForC40.opsForHash().get(CarCurrent.OBJECT_KEY, "LNBSCCAK4JT100137");
    Object test2 = redisTemplateForC51.opsForHash().get(CarCurrent.OBJECT_KEY, "LNBSCCAK4JT100137");
    Object test3 = redisTemplateForC53.opsForHash().get(CarCurrent.OBJECT_KEY, "LNBSCCAK4JT100137");
    System.out.println(test1);
    System.out.println(test2);
    System.out.println(test3);
    }
    }
  4. 总结:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    1. 可以在MultiRedisConfig类的每个@Bean方法打个断点,会发现每个redisTemplate的连接信息不同。

    2. 测试类里可以通过断点发现的确是从每个redis的不同DB取值。

    3. 注意redis序列化问题,在MultiRedisConfig的redisTemplate()方法里设置不同的序列化。也可以这样设置序列化(默认的redisTemplate使用的就是这个序列化):
    public RedisTemplate redisTemplate(String hostName, int port, String password, int database,Jackson2JsonRedisSerializer jackson2JsonRedisSerializer){
    RedisTemplate temple = new RedisTemplate();
    temple.setConnectionFactory(this.connectionFactory(
    hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis));
    RedisSerializer stringSerializer = new StringRedisSerializer();
    temple.setKeySerializer(stringSerializer);//key序列化
    temple.setValueSerializer(jackson2JsonRedisSerializer);//value序列化
    temple.setHashKeySerializer(stringSerializer);//Hash key序列化
    temple.setHashValueSerializer(jackson2JsonRedisSerializer);//Hash value序列化
    return temple;
    }

5.2redis其他用法

6.Java中使用Kafka记录

6.1消费者使用

  1. 在pom.xml文件引入如下依赖:

    1
    2
    3
    4
    5

    org.springframework.kafka
    spring-kafka
    1.1.4.RELEASE

  2. 在application.yml文件里配置连接Kafka的信息:

    1
    2
    3
    kafka:
    topics: BAIC-C40D-M05-uaes-result-topic,BAIC-C51E-F05-uaes-result-topic,BAIC-C53F-uaes-result-topic
    broker_list: ******,******,******
  3. 在config包下新建KafkaConsumerConfig类,内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    package com.uaes.iot.emr.kafka.config;

    import org.apache.kafka.clients.consumer.ConsumerConfig;
    import org.apache.kafka.common.serialization.StringDeserializer;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.kafka.annotation.EnableKafka;
    import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
    import org.springframework.kafka.config.KafkaListenerContainerFactory;
    import org.springframework.kafka.core.ConsumerFactory;
    import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
    import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;

    import java.util.HashMap;
    import java.util.Map;

    /**
    * Created by xk on 2018/05/22
    */

    @Configuration
    @EnableKafka
    public class KafkaConsumerConfig {

    @Value("${kafka.broker_list}")
    private String services;

    public Map consumerConfigs() {
    Map props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, services);
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
    props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100");
    props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "multi-tenancy");
    return props;
    }

    public ConsumerFactory consumerFactory() {
    return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    @Bean
    public KafkaListenerContainerFactory> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.setConcurrency(3);
    factory.getContainerProperties().setPollTimeout(3000);
    return factory;
    }
    }
  4. 在process.in包下新建KafkaConsumerService类,内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    package com.uaes.iot.emr.kafka.process.in;

    import com.iot.util.ReturnData;
    import com.uaes.iot.emr.kafka.client.*;
    import com.uaes.iot.emr.kafka.common.PushParaPost;
    import com.uaes.iot.emr.kafka.dto.*;
    import net.sf.json.JSONObject;
    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.ResponseEntity;
    import org.springframework.kafka.annotation.KafkaListener;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringUtils;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Map;

    /**
    * Created by xk on 2018/05/22
    */
    @Component
    public class KafkaConsumerService {
    @KafkaListener(topics = {"#{'${kafka.topics}'.split(',')}"})
    public void consumerEmrKafkaMessage(ConsumerRecord record){
    JSONObject message = JSONObject.fromObject(record.value());
    String topic = record.topic();
    if (message == null || StringUtils.isEmpty(message)) {
    return;
    }
    Logger logger = LoggerFactory.getLogger(KafkaConsumerService.class);
    logger.info("获取到数据了:"+message.get("data"));
    }
    }
  5. 总结:

    1. @KafkaListener(topics = {“#{‘${kafka.topics}’.split(‘,’)}”})这种写法以后如果需要在监听其他topic时,只需要在application.yml文件里添加即可。

6.2生产者使用

你可能感兴趣的:(日常积累)