├── .gitignore
├── README.md
├── pom.xml
├── rediswriter.iml
└── src
├── main
├── assembly
│ └── package.xml
├── java
│ └── com
│ │ └── alibaba
│ │ └── datax
│ │ └── plugin
│ │ └── writer
│ │ └── rediswriter
│ │ ├── Constant.java
│ │ ├── JedisClusterPipeline.java
│ │ ├── Key.java
│ │ ├── RedisWriteAbstract.java
│ │ ├── RedisWriter.java
│ │ ├── RedisWriterHelper.java
│ │ └── writer
│ │ ├── DeleteWriter.java
│ │ ├── HashTypeWriter.java
│ │ ├── ListTypeWriter.java
│ │ └── StringTypeWriter.java
└── resources
│ ├── plugin.json
│ └── plugin_job_template.json
└── test
├── demo
├── data
│ ├── create_hive_table.sql
│ └── hive_data.txt
├── hive_delete_to_redis_hash_filed.json
├── hive_delete_to_redis_string.json
├── hive_to_redis_hash.json
├── hive_to_redis_list.json
└── hive_to_redis_string.json
└── java
└── JedisTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 | derby.log
19 |
20 | ### NetBeans ###
21 | /nbproject/private/
22 | /build/
23 | /nbbuild/
24 | /dist/
25 | /nbdist/
26 | /.nb-gradle/
27 |
28 | /logs/
29 | /spark-warehouse/
30 | /metastore_db/
31 | /out/
32 | /venv/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DataX-redis-writer
2 | 详情参见博客:[https://blog.csdn.net/u013289115/article/details/106277937](https://blog.csdn.net/u013289115/article/details/106277937)
3 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | datax-all
7 | com.alibaba.datax
8 | 0.0.1-SNAPSHOT
9 |
10 | 4.0.0
11 | rediswriter
12 | rediswriter
13 | jar
14 |
15 |
16 |
17 | com.alibaba.datax
18 | datax-common
19 | ${datax-project-version}
20 |
21 |
22 |
23 | redis.clients
24 | jedis
25 | 2.9.0
26 |
27 |
28 |
29 |
30 |
31 |
32 | maven-compiler-plugin
33 |
34 | ${jdk-version}
35 | ${jdk-version}
36 | ${project-sourceEncoding}
37 |
38 |
39 |
40 | maven-assembly-plugin
41 |
42 |
43 | src/main/assembly/package.xml
44 |
45 | datax
46 |
47 |
48 |
49 | dwzip
50 | package
51 |
52 | single
53 |
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/rediswriter.iml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/src/main/assembly/package.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 | dir
8 |
9 | false
10 |
11 |
12 | src/main/resources
13 |
14 | plugin.json
15 | plugin_job_template.json
16 |
17 | plugin/writer/rediswriter
18 |
19 |
20 | target/
21 |
22 | rediswriter-0.0.1-SNAPSHOT.jar
23 |
24 | plugin/writer/rediswriter
25 |
26 |
27 |
28 |
29 |
30 | false
31 | plugin/writer/rediswriter/libs
32 | runtime
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/datax/plugin/writer/rediswriter/Constant.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.datax.plugin.writer.rediswriter;
2 |
3 | public final class Constant {
4 |
5 | public static final String STANDALONE = "singleton";
6 | public static final String CLUSTER = "cluster";
7 |
8 | // 支持的redis的三种数据类型
9 | public static final String WRITE_TYPE_STRING = "string";
10 | public static final String WRITE_TYPE_LIST= "list";
11 | public static final String WRITE_TYPE_HASH = "hash";
12 |
13 | // 两种redis的操作类型,delete和insert
14 | public static final String WRITE_MODE_DELETE = "delete";
15 | public static final String WRITE_MODE_INSERT = "insert";
16 |
17 | // 导入redis list的模式,有lpush,rpush,overwrite,默认overwrite
18 | public static final String LIST_PUSH_TYPE_OVERWRITE = "overwrite";
19 | public static final String LIST_PUSH_TYPE_LPUSH = "lpush";
20 | public static final String LIST_PUSH_TYPE_RPUSH = "rpush";
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/datax/plugin/writer/rediswriter/JedisClusterPipeline.java:
--------------------------------------------------------------------------------
1 |
2 | package com.alibaba.datax.plugin.writer.rediswriter;
3 |
4 | import redis.clients.jedis.*;
5 | import redis.clients.jedis.exceptions.JedisRedirectionException;
6 | import redis.clients.util.JedisClusterCRC16;
7 | import redis.clients.util.SafeEncoder;
8 |
9 | import java.io.Closeable;
10 | import java.lang.reflect.Field;
11 | import java.util.*;
12 |
13 | /**
14 | * JedisClusterPipeline
15 | * @Author: lijf@2345.com
16 | * @Date: 2019/1/22 16:29
17 | * @Version: 1.0
18 | */
19 | public class JedisClusterPipeline extends PipelineBase implements Closeable {
20 | // 部分字段没有对应的获取方法,只能采用反射来做
21 | // 你也可以去继承JedisCluster和JedisSlotBasedConnectionHandler来提供访问接口
22 | private static final Field FIELD_CONNECTION_HANDLER;
23 | private static final Field FIELD_CACHE;
24 | static {
25 | FIELD_CONNECTION_HANDLER = getField(BinaryJedisCluster.class, "connectionHandler");
26 | FIELD_CACHE = getField(JedisClusterConnectionHandler.class, "cache");
27 | }
28 |
29 | private JedisSlotBasedConnectionHandler connectionHandler;
30 | private JedisClusterInfoCache clusterInfoCache;
31 | /**根据顺序存储每个命令对应的Client*/
32 | private Queue clients = new LinkedList<>();
33 | /**用于缓存连接*/
34 | private Map jedisMap = new HashMap<>();
35 | /**是否有数据在缓存区*/
36 | private boolean hasDataInBuf = false;
37 |
38 | /**
39 | * 根据jedisCluster实例生成对应的JedisClusterPipeline
40 | * @param jedisCluster jedisCluster
41 | * @return JedisClusterPipeline
42 | */
43 | public static JedisClusterPipeline pipelined(JedisCluster jedisCluster) {
44 | JedisClusterPipeline pipeline = new JedisClusterPipeline();
45 | pipeline.setJedisCluster(jedisCluster);
46 | return pipeline;
47 | }
48 |
49 |
50 |
51 | public void setJedisCluster(JedisCluster jedis) {
52 | connectionHandler = getValue(jedis, FIELD_CONNECTION_HANDLER);
53 | clusterInfoCache = getValue(connectionHandler, FIELD_CACHE);
54 | }
55 |
56 | /**
57 | * 刷新集群信息,当集群信息发生变更时调用
58 | * @param
59 | * @return
60 | */
61 | public void refreshCluster() {
62 | connectionHandler.renewSlotCache();
63 | }
64 |
65 | /**
66 | * 同步读取所有数据. 与syncAndReturnAll()相比,sync()只是没有对数据做反序列化
67 | */
68 | public void sync() {
69 | innerSync(null);
70 | }
71 |
72 | /**
73 | * 同步读取所有数据 并按命令顺序返回一个列表
74 | *
75 | * @return 按照命令的顺序返回所有的数据
76 | */
77 | public List