java使用redis資料庫,javaredis資料庫

來源:互聯網
上載者:User

java使用redis資料庫,javaredis資料庫

使用maven管理jar,添加依賴:   

<dependency>

     <groupId>redis.clients</groupId>

     <artifactId>jedis</artifactId>

     <version>2.7.0</version>

 </dependency>//需要的jar

使用redis資料庫的配置(設定屬性的值,可以根據具體需要),存資料

  JedisPool jedisPool = null;

        JedisPoolConfig poolConfig = new JedisPoolConfig();

        poolConfig.setMaxIdle(10);

        poolConfig.setMaxTotal(100);

        poolConfig.setMaxWaitMillis(10000);

        poolConfig.setTestOnBorrow(true);

        jedisPool = new JedisPool(poolConfig, "192.168.0.109", 6379);    //配置Jedis的配置,連接埠,伺服器位址

預存程序

  List<String> lines = Files.readAllLines(Paths.get("E:/oooooooo/task.txt"));//要儲存的資料來源,資料格式多為json形式的字串

        Jedis jedis = jedisPool.getResource();

        jedis.select(3);// 3是指在把資料庫上的分區,可以自由設定

        Pipeline pipelined = jedis.pipelined();

        lines.forEach(l -> {

            pipelined.lpush(資料庫名, l);

        });

        pipelined.sync(); //這段代碼的作用是通過管道(Pipeline)批量儲存到redis資料庫

通過java擷取redis上的資料

  JedisPoolConfig poolConfig = new JedisPoolConfig();

        poolConfig.setMaxIdle(10);

        poolConfig.setMaxTotal(500);

        poolConfig.setMaxWaitMillis(100000);

        poolConfig.setTestOnBorrow(true);

        jedisPool = new JedisPool(poolConfig, 伺服器位址, 連接埠, timeout, 資料庫密碼);

  Jedis jedis = jedisPool.getResource();

   jedis.select(3);// 3是指在把資料庫上的分區,可以自由設定

       String targetString = jedis.lpop(資料庫名);;//targetString就是取出來的目標資料

  jedisPool.returnResourceObject(jedis); //一頓操作之後,記得釋放jedis的資源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.