mysql数据导入到redis,将mysql数据导入到redis

用脚本模拟redis的客户端redis-cli实现:(注:mysql地址为10.86.30.203 redis地址为10.86.31.50)

1.首先确定mysql表的列:

desc test;

+-----------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-----------+-------------+------+-----+---------+-------+

| id | int(11) | YES | | NULL | |

| test1 | varchar(50) | YES | | NULL | |

| test2 | varchar(64) | YES | | NULL | |

| test3 | int(11) | YES | | NULL | |

+-----------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

2.编写sql脚本:

vi /home/darren/test.sql

SELECT CONCAT(

"*8\r\n",

‘$‘, LENGTH(redis_cmd), ‘\r\n‘,redis_cmd, ‘\r\n‘,‘$‘, LENGTH(redis_key), ‘\r\n‘,redis_key, ‘\r\n‘,

‘$‘, LENGTH(hkey1), ‘\r\n‘,hkey1, ‘\r\n‘,‘$‘, LENGTH(hval1), ‘\r\n‘, hval1, ‘\r\n‘

‘$‘, LENGTH(hkey2), ‘\r\n‘,hkey2, ‘\r\n‘,‘$‘, LENGTH(hval2), ‘\r\n‘, hval2, ‘\r\n‘

‘$‘, LENGTH(hkey3), ‘\r\n‘,hkey3, ‘\r\n‘,‘$‘, LENGTH(hval3), ‘\r\n‘, hval3, ‘\r‘)

FROM (

SELECT

‘HMSET‘ AS redis_cmd, CONCAT(‘id:‘,id) AS redis_key,

‘test1‘ AS hkey1,test1 AS hval1,

‘test2‘ AS hkey2,test2 AS hval2,

‘test3‘ AS hkey3,test3 AS hval3

FROM test

) AS t

注意:其中*8的意思是一共有8个参数,$length()为参数的长度。脚本中test1,test2,test3均为mysql的列名,可自行修改。

3.执行导入命令:

mysql -uroot -pxxx -h 10.86.30.203 test --skip-column-names --raw < /home/darren/test.sql | redis-cli --pipe

----------------------------------------------------------------------------------------------------------------

mysql: [Warning] Using a password on the command line interface can be insecure.

All data transferred. Waiting for the last reply...

Last reply received from server.

errors: 0, replies: 1

出现以上errors为0为导入成功。

–raw: 使mysql不转换字段值中的换行符。–skip-column-names: 使mysql输出的每行中不包含列名。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Spring Boot整合RedisMySQL,并将数据MySQL导入Redis,可以按照以下步骤进行: 1. 在pom.xml文件中添加RedisMySQL的依赖。 2. 在application.properties文件中配置RedisMySQL的连接信息。 3. 创建一个RedisConfig类,用于配置RedisTemplate。 4. 创建一个MySQLConfig类,用于配置DataSource和JdbcTemplate。 5. 创建一个Service类,用于从MySQL中读取数据,并将数据存储到Redis中。 6. 在Controller中调用Service类中的方法,从Redis中读取数据并返回给前端。 下面是一个简单的示例代码: 1. pom.xml文件中添加RedisMySQL的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 2. application.properties文件中配置RedisMySQL的连接信息: ``` # Redis spring.redis.host=localhost spring.redis.port=6379 # MySQL spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root ``` 3. 创建一个RedisConfig类,用于配置RedisTemplate: ``` @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } ``` 4. 创建一个MySQLConfig类,用于配置DataSource和JdbcTemplate: ``` @Configuration public class MySQLConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } } ``` 5. 创建一个Service类,用于从MySQL中读取数据,并将数据存储到Redis中: ``` @Service public class UserService { private final JdbcTemplate jdbcTemplate; private final RedisTemplate<String, Object> redisTemplate; public UserService(JdbcTemplate jdbcTemplate, RedisTemplate<String, Object> redisTemplate) { this.jdbcTemplate = jdbcTemplate; this.redisTemplate = redisTemplate; } public List<User> getAllUsers() { List<User> users = jdbcTemplate.query("SELECT * FROM user", new BeanPropertyRowMapper<>(User.class)); users.forEach(user -> redisTemplate.opsForValue().set("user:" + user.getId(), user)); return users; } } ``` 6. 在Controller中调用Service类中的方法,从Redis中读取数据并返回给前端: ``` @RestController public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping("/users") public List<User> getAllUsers() { List<User> users = (List<User>) userService.redisTemplate.opsForValue().get("users"); if (users == null) { users = userService.getAllUsers(); userService.redisTemplate.opsForValue().set("users", users); } return users; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值