java - 使用 Java 将 Redis 数据库连接到 Azure 云

标签 java azure redis cloud jedis

美好的一天,我的 Java 代码中出现此错误。我正在尝试将我的数据库链接到 azure 云。我使用了列表命令:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at anothercloud.Anothercloud.main(Anothercloud.java:63)
Java Result: 1

package anothercloud;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.exceptions.JedisConnectionException;
/**
 *
 * @author ariel
 */
public class Anothercloud {
static HashMap<Double, String> redisData = new HashMap<Double, String>();
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        boolean useSsl=true;
        inclass infos=new inclass();
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<String> value = new ArrayList<String>();
        String anjiecachekey="xxxx=";
        
        JedisShardInfo shardInfo=new JedisShardInfo("anjiecloud.redis.cache.windows.net",6380,useSsl);//use secondary connection string
        shardInfo.setPassword("xxxx=");//use secondary access key
        //Simple PING command
        Jedis jedis=new Jedis(shardInfo.getHost());
        try{
            jedis.auth(anjiecachekey);
            jedis.connect();
            System.out.println("My Cache Response: "+jedis.ping());
            
            if (jedis.llen("state") == 0 && jedis.llen("number") == 0)  {
             
          
             for(Map.Entry m: inclass.map.entrySet()){
                 
                 jedis.lpush("state",(String)m.getValue());
                 jedis.lpush("number",m.getKey().toString());
             }
            
        }
        for(String s: jedis.lrange("state", 0, 1000)){
            names.add(s);
                       
          }
         for(String r: jedis.lrange("number", 0, 1000)){
               value.add(r);
            }
         for(int i =0; i < names.size(); i++){
             redisData.put(Double.parseDouble(value.get(i)), names.get(i));
         }

         
        ArrayList<String> states = new ArrayList<String>();
         for (Map.Entry m : redisData.entrySet()) {
             states.add((String)m.getValue());
         }
         
         String[] statesArray = new String[states.size()];
         states.toArray(statesArray);

        

        JComboBox<String> stateList = new JComboBox<>(statesArray);
        stateList.addItemListener(new MyHandler());
       
        JFrame jframe = new JFrame();
        JLabel item1 = new JLabel("IGR Statistics H1 2020");
        jframe.add(item1);
        
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setLayout(new FlowLayout());
        jframe.setSize(400,200);
        jframe.setVisible(true);
        
        jframe.add(stateList);
        
        

// get the selected item:
       // String selectedBook = (String) stateList.getSelectedItem();
       

        // check whether the server is running or not
        System.out.println("Server is running: " + jedis.ping());
        //getting the percentage for each state
       

        // storing the data into redis database
      
        
        for (Map.Entry m : inclass.map.entrySet()) {
            System.out.println(m.getKey() + " " + m.getValue());

  
        }
        }
        catch(JedisConnectionException e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,"You require a working internet connection");
        }
    }
    
        static class MyHandler implements ItemListener{


        @Override
        public void itemStateChanged(ItemEvent e) {
             for (Map.Entry m : redisData.entrySet()) {
if(e.getItem().toString() == m.getValue()&& e.getStateChange() == 1){
                     
                     JOptionPane.showMessageDialog(null, m.getKey(), "VALUE IN BILLIONS", 1);
                     
                     System.out.println(m.getKey());
                     break;
                     
                 }
          

            
        }
       
        }
        
         }
}

请问我该如何纠正这个错误?任何反馈将不胜感激。我对 Java 语言及其语法还很陌生。该错误似乎也来 self 的 for 循环。该错误似乎也来 self 的 for 循环。

最佳答案

如错误所述,您将看到 IndexOutOfBoundsException。这意味着您正在尝试访问大于/小于其范围的列表索引。在这种情况下,value.get(i) 抛出错误:

for (int i = 0; i < names.size(); i++)
            {
                redisData.put(Double.parseDouble(value.get(i)), names.get(i));
            }

尝试修改如下代码:

for (int i = 0; i < names.size() - 1; i++)
                {
                    redisData.put(Double.parseDouble(value.get(i)), names.get(i));
                }

此外,我建议您重新访问 Redis 缓存的 java 实现:Intro to Jedis – the Java Redis Client Library

关于java - 使用 Java 将 Redis 数据库连接到 Azure 云,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66520158/

相关文章:

java - MapReduce WordCount示例问题

java - mvn 提示 java 源,即使将使用所需的

c# - 主机启动时间比预期长 - Azure 函数

azure - Azure Web 应用程序中的 ASP.NET 5 环境名称

redis - 当 RAM 已满时,redis 中是否有基于数据库的 key 驱逐策略

java - 涉及文件读取的 JUnit 测试套件在 Windows 10 和 Fedora 23 上的表现不同

java - 在类路径上添加属性文件

azure - 如何为 Azure 容器注册表创建只读访问 key

python - 如何存储全局(url_for)URL(和其他全局变量)并在 Flask 应用程序中共享?

将 Celery 从 3.1 升级到 4.0 后 Redis 不返回结果