读取jedis配置

  • POM文件pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.laodixiao.connectToCodis</groupId>
  <artifactId>connectToCodis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>connectToCodis</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.7.2</version>
</dependency>

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.7</version>
</dependency>


  </dependencies>
</project>

 
 
  • 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
  • MapSort.java实现Map的排序
package com.laodixiao.connectToCodis.connectToCodis;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MapSort {

    /**
     * 
     * @param map
     * @return
     */
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            @Override
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                return -1*(o1.getValue()).compareTo(o2.getValue());
            }
        });

        Map<K, V> result = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

}

 
 
  • 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
  • 实现Codis的读写
package com.laodixiao.connectToCodis.connectToCodis;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class connectToCodis {

       private static JedisPool pool = null;  

        /** 
         * 构建redis连接池 
         *  
         * @param ip 
         * @param port 
         * @return JedisPool 
         */  
        public static JedisPool getPool() {  
            if (pool == null) {  
                JedisPoolConfig config = new JedisPoolConfig();  
                //最大连接数
                config.setMaxTotal(50);
                //最大空闲连接数
                config.setMaxIdle(10);
                //每次释放连接的最大数目
                config.setNumTestsPerEvictionRun(1024);
                //释放连接的扫描间隔(毫秒)
                config.setTimeBetweenEvictionRunsMillis(30000);
                //连接最小空闲时间
                config.setMinEvictableIdleTimeMillis(1800000);
                //连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放
                config.setSoftMinEvictableIdleTimeMillis(10000);
                //获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1
                config.setMaxWaitMillis(1500);
                //在获取连接的时候检查有效性, 默认false
                config.setTestOnBorrow(false);
                //在空闲时检查有效性, 默认false
                config.setTestWhileIdle(true);
                //连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
                config.setBlockWhenExhausted(false);

                //10s超長時間
                pool = new JedisPool(config, "127.0.0.1", 6767, 10000); 

            }  
            return pool;  
        }  

        /** 
         * 返还到连接池 
         *  
         * @param pool  
         * @param redis 
         */  
        public static void returnResource(JedisPool pool, Jedis redis) {  
            if (redis != null) {  
                pool.returnResource(redis);  
            }  
        }  

        /** 
         * codis读取数据
         *  
         * @param key 
         * @return 
         */  
        public static String get(String key){  
            String value = null;  

            JedisPool pool = null;  
            Jedis jedis = null;  
            try {  
                pool = getPool();  
                jedis = pool.getResource();  
                value = jedis.get(key);

            } catch (Exception e) {  
                //释放redis对象  
                pool.returnBrokenResource(jedis);  
                e.printStackTrace();  
            } finally {  
                //返还到连接池  
                returnResource(pool, jedis);  
            } 
            return value;  
        }   

        /**
         * codis写入数据
         * @param key
         * @param value
         */
        public static void set(String key, String value){
            JedisPool pool = null;  
            Jedis jedis = null;  
            try {  
                pool = getPool();  
                jedis = pool.getResource();  
                jedis.set(key, value);

            } catch (Exception e) {  
                //释放redis对象  
                pool.returnBrokenResource(jedis);  
                e.printStackTrace();  
            } finally {  
                //返还到连接池  
                returnResource(pool, jedis);  
            } 
        }

        @SuppressWarnings("unchecked")
        public static void main( String[] args ) throws IOException {
            //读取文件./DouBanData/DouBanData.txt
            String filePath = "./DouBanData/DouBanData.txt";
            File file=new File(filePath);

            //保存结果 cid,//douBanCidKeys,//cidSortedKeys
            File fileSave = new File("./result/SimData.txt");
            if(!fileSave.exists()){
                fileSave.createNewFile();
            }
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(fileSave), "utf-8");

            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),"utf-8");//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;


                int cnt = 0;
                while((lineTxt = bufferedReader.readLine()) != null){
                    //排序后的结果:cid,//cidSortedKeys
                    StringBuffer cidSortedString = new StringBuffer();
                    String Key = lineTxt.split(",//")[0];
                    String douBandListString = lineTxt.split(",//")[2];

                    cnt = cnt + 1;
                    System.out.println(cnt);

                    //組裝key
                    String key = "AlgorithmsCommonBid:ITI:"+ Key;
                    //或取結果
                    String result = get(key);

                    /**
                     * json用法
                     */
                    JSONObject json = JSONObject.parseObject(result);
                    if(json!=null){

                        cidSortedString.append(Key+",//");
                        cidSortedString.append(douBandListString+",//");

                        String rst = json.get("Results").toString().replace('{', ' ').replace('}', ' ').trim();
                        String[] rstArray = rst.split(",");
                        Map map = new HashMap();
                        for (int i=0; i<rstArray.length; i++){
                            map.put(rstArray[i].split(":")[0].replace('"', ' ').trim(), Double.parseDouble(rstArray[i].split(":")[1]));
                        }
                        //map按value降序排列
                        Map mapSorted = MapSort.sortByValue(map);
                        StringBuffer cidSortedStringTmp = new StringBuffer();
                        for (Object key1 : mapSorted.keySet()) {  
                            cidSortedStringTmp.append(key1+"/");
                        }
                        if (cidSortedStringTmp.length()<=0) {
                            cidSortedStringTmp.append("/");
                        }
                        String strTmpString = cidSortedStringTmp.substring(0,cidSortedStringTmp.length()-1);

                        cidSortedString.append(strTmpString+"\r\n");
                        System.out.println(cidSortedString);
                        osw.write(cidSortedString.toString());
                    }
                }
                read.close();
            }

          //关闭文件输出流
            osw.flush();
            osw.close();
        }
}

 
 
  • 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
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值