├── .gitattributes
├── .gitignore
├── LICENSE
├── README.md
├── build.gradle
├── debug.sql
├── gradle.properties
├── lib
├── ojdbc14.jar
└── readme.txt
├── release.bat
├── release.sh
├── runfiles
├── config-cmsdocmenttable.properties
├── config-exportdemo.properties
├── config-quartztask.properties
├── jvm.options
├── restart-cmsdocumenttable.sh
├── restart-exportdemo.sh
├── restart-quartz.bat
├── restart-quartz.sh
├── restart.bat
├── restart.sh
├── run.bat
├── run.sh
├── startup-cmsdocumenttable.sh
├── startup.bat
├── startup.sh
├── stop-cmsdocumenttable.sh
├── stop-quartz.bat
├── stop-quartz.sh
├── stop.bat
└── stop.sh
└── src
├── main
├── java
│ └── org
│ │ └── frameworkset
│ │ └── elasticsearch
│ │ └── imp
│ │ ├── BigTableDemo.java
│ │ ├── CMSDocumentImport.java
│ │ ├── CustomObject.java
│ │ ├── DMAdaptor.java
│ │ ├── Db2DBdemo.java
│ │ ├── DbClientOptionsDemo.java
│ │ ├── DbClientOptionsDemo7.java
│ │ ├── DbLocalDatedemo.java
│ │ ├── DbTimestamp.java
│ │ ├── Dbdemo.java
│ │ ├── DbdemoFromSQLFile.java
│ │ ├── DbdemoFull.java
│ │ ├── DbdemoWithSplitRecord.java
│ │ ├── DbdemoWithStop.java
│ │ ├── DruidAdaptor.java
│ │ ├── ES2DBDemo.java
│ │ ├── ES2DBScrollDemo.java
│ │ ├── ES2DBScrollTimestampDemo.java
│ │ ├── ES2DBSliceScrollDemo.java
│ │ ├── ES2DBSliceScrollResultCallbackDemo.java
│ │ ├── ES2PostgresDBFullDemo.java
│ │ ├── ES2PostgresDBScrollTimestampDemo.java
│ │ ├── NewDbdemoFromSQLFile.java
│ │ ├── PostgresCollecttimeIncreamentDbdemo.java
│ │ ├── PostgresDbdemo.java
│ │ ├── PostgresDefaultDbdemo.java
│ │ ├── PostgresIncreaseDbdemo.java
│ │ ├── QuartzES2DBImportTask.java
│ │ ├── QuartzImportTask.java
│ │ ├── QuartzTimestampImportTask.java
│ │ ├── TestMysqlAdaptor.java
│ │ ├── dummy
│ │ ├── DB2DummyDemo.java
│ │ ├── DB2DummyOnceDemo.java
│ │ └── ES2DummyDemo.java
│ │ ├── http
│ │ ├── ES2HttpDemo.java
│ │ ├── Http2ESDemo.java
│ │ ├── Http2ESFullQueryDslDemo.java
│ │ ├── Http2ESPagineDemo.java
│ │ ├── Http2ESQueryDslDemo.java
│ │ ├── ParrelHttp2ESDemo.java
│ │ └── ParrelHttpPagine2ESDemo.java
│ │ └── quartz
│ │ ├── Bootstrap.java
│ │ └── ImportDataJob.java
└── resources
│ ├── application.properties
│ ├── dsl2ndSqlFile.xml
│ ├── httpdsl.xml
│ ├── log4j2.xml
│ ├── org
│ └── frameworkset
│ │ └── task
│ │ └── quarts-task.xml
│ └── sql.xml
└── test
└── java
└── org
└── frameworkset
└── elasticsearch
└── imp
├── CMSDocumentImportTest.java
├── DBTest.java
├── DbdemoFromSQLFileTest.java
├── DbdemoTest.java
├── ESDemoTest.java
├── QuartzTest.java
└── SqliteUpdate.java
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 | /build/
3 | /.classpath
4 | /.gradle/
5 | /.idea/
6 | /.svn/
7 | /.project
8 | /.settings/
9 | /elktask.iml
10 | /out/
11 |
12 | es.log
13 | *.bak
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # 数据同步工具
3 | 通过本工具可以非常方便地实现数据库和Elasticsearch之间的数据同步功能,数据库与数据库之间的数据同步功能
4 |
5 |
6 |
7 | # BBoss Environmental requirements
8 |
9 | JDK requirement: JDK 1.8+
10 |
11 | Elasticsearch version requirements: 1.x,2.X,5.X,6.X,7.x,8,x,+
12 |
13 | Spring booter 1.x,2.x,+
14 | # bboss elasticsearch 数据导入工具demo
15 | 使用本demo所带的应用程序运行容器环境,可以快速编写,打包发布可运行的数据导入工具
16 |
17 | 支持的数据库:
18 | mysql,maridb,postgress,oracle ,sqlserver,db2等
19 |
20 | 支持的Elasticsearch版本:
21 | 1.x,2.X,5.X,6.X,7.x,8,x,+
22 |
23 | 支持海量PB级数据同步导入功能
24 |
25 | [使用参考文档](https://esdoc.bbossgroups.com/#/db-es-tool)
26 |
27 | # 建表sql
28 | ```
29 | mysql :
30 | CREATE TABLE
31 | batchtest
32 | (
33 | id bigint NOT NULL AUTO_INCREMENT,
34 | name VARCHAR(4000),
35 | author VARCHAR(1000),
36 | content longtext,
37 | title VARCHAR(1000),
38 | optime DATETIME,
39 | oper VARCHAR(1000),
40 | subtitle VARCHAR(1000),
41 | collecttime DATETIME,
42 | ipinfo VARCHAR(2000),
43 | PRIMARY KEY (id)
44 | )
45 | ENGINE=InnoDB DEFAULT CHARSET=utf8;
46 | postgresql:
47 |
48 | CREATE TABLE
49 | batchtest
50 | (
51 | id bigint ,
52 | name VARCHAR(4000),
53 | author VARCHAR(1000),
54 | content text,
55 | title VARCHAR(1000),
56 | optime timestamp,
57 | oper VARCHAR(1000),
58 | subtitle VARCHAR(1000),
59 | collecttime timestamp,
60 | ipinfo VARCHAR(2000),
61 | PRIMARY KEY (id)
62 | )
63 | ```
64 | # 构建部署
65 | ## 准备工作
66 | 需要通过gradle构建发布版本,gradle安装配置参考文档:
67 |
68 | https://esdoc.bbossgroups.com/#/bboss-build
69 |
70 | ## 下载源码工程-基于gradle
71 | https://gitee.com/bboss/db-elasticsearch-tool
72 |
73 | 从上面的地址下载源码工程,然后导入idea或者eclipse,根据自己的需求,修改导入程序逻辑
74 |
75 | org.frameworkset.elasticsearch.imp.Dbdemo
76 |
77 | 如果需要测试和调试导入功能,运行Dbdemo的main方法即可即可:
78 |
79 |
80 | ```java
81 | public class Dbdemo {
82 | public static void main(String args[]){
83 |
84 | long t = System.currentTimeMillis();
85 | Dbdemo dbdemo = new Dbdemo();
86 | String repsonse = ElasticSearchHelper.getRestClientUtil().getIndice("dbdemo");
87 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
88 | dbdemo.scheduleImportData( dropIndice);//定时增量导入
89 | // dbdemo.scheduleFullImportData(dropIndice);//定时全量导入
90 |
91 | // dbdemo.scheduleFullAutoUUIDImportData(dropIndice);//定时全量导入,自动生成UUID
92 | // dbdemo.scheduleDatePatternImportData(dropIndice);//定时增量导入,按日期分表yyyy.MM.dd
93 | }
94 | .....
95 | }
96 | ```
97 |
98 | 修改es和数据库配置-db-elasticsearch-tool\src\main\resources\application.properties
99 |
100 | db-elasticsearch-tool工程已经内置mysql jdbc驱动,如果有依赖的第三方jdbc包(比如oracle驱动),可以将第三方jdbc依赖包放入db-elasticsearch-tool\lib目录下
101 |
102 | 修改完毕配置后,就可以进行功能调试了。
103 |
104 |
105 | 测试调试通过后,就可以构建发布可运行的版本了:进入命令行模式,在源码工程根目录db-elasticsearch-tool下运行以下gradle指令打包发布版本
106 |
107 | release.bat
108 |
109 | ## 运行作业
110 | gradle构建成功后,在build/distributions目录下会生成可以运行的zip包,解压运行导入程序
111 |
112 | linux:
113 |
114 | chmod +x restart.sh
115 |
116 | ./restart.sh
117 |
118 | windows: restart.bat
119 |
120 | ## 作业jvm配置
121 | 修改jvm.options,设置内存大小和其他jvm参数
122 |
123 | -Xms1g
124 |
125 | -Xmx1g
126 |
127 | ## 在工程中添加多个表同步作业
128 | 默认的作业任务是Dbdemo,同步表td_sm_log的数据到索引dbdemo/dbdemo中
129 |
130 | 现在我们在工程中添加另外一张表td_cms_document的同步到索引cms_document/cms_document的作业步骤:
131 |
132 | 1.首先,新建一个带main方法的类org.frameworkset.elasticsearch.imp.CMSDocumentImport,实现同步的逻辑
133 |
134 | 如果需要测试调试,就在test目录下面编写 src\test\java\org\frameworkset\elasticsearch\imp\CMSDocumentImportTest.java测试类,然后debug即可
135 |
136 | 2.然后,在runfiles目录下新建CMSDocumentImport作业主程序和作业进程配置文件:runfiles/config-cmsdocmenttable.properties,内容如下:
137 |
138 | mainclass=org.frameworkset.elasticsearch.imp.CMSDocumentImport
139 |
140 | pidfile=CMSDocumentImport.pid
141 |
142 |
143 | 3.最后在runfiles目录下新建作业启动sh文件(这里只新建linux/unix指令,windows的类似):runfiles/restart-cmsdocumenttable.sh
144 |
145 | 内容与默认的作业任务是Dbdemo内容一样,只是在java命令后面多加了一个参数,用来指定作业配置文件:--conf=config-cmsdocmenttable.properties
146 |
147 | nohup java \$RT_JAVA_OPTS -jar ${project}-${bboss_version}.jar restart --conf=config-cmsdocmenttable.properties --shutdownLevel=9 > ${project}.log &
148 |
149 | 其他stop shell指令也类似建立即可
150 |
151 | # 管理提取数据的sql语句
152 |
153 | db2es工具管理提取数据的sql语句有两种方法:代码中直接编写sql语句,配置文件中采用sql语句
154 | ## 1.代码中写sql
155 |
156 | `//指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
157 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
158 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
159 | // log_id和数据库对应的字段一致,就不需要设置setLastValueColumn信息,
160 | // 但是需要设置setLastValueType告诉工具增量字段的类型
161 |
162 | importBuilder.setSql("select * from td_sm_log where log_id > #[log_id]");`
163 | ## 2.在配置文件中管理sql
164 | 设置sql语句配置文件路径和对应在配置文件中sql name
165 | `importBuilder.setSqlFilepath("sql.xml")
166 | .setSqlName("demoexportFull"); `
167 |
168 | 配置文件sql.xml,编译到classes根目录即可:
169 |
170 |
171 |
172 |
173 |
176 |
177 | #[log_id]]]>
178 |
179 |
180 |
181 |
182 | 在sql配置文件中可以配置多条sql语句
183 |
184 | # 作业参数配置
185 |
186 | 在使用[db-elasticsearch-tool](https://github.com/bbossgroups/db-elasticsearch-tool)时,为了避免调试过程中不断打包发布数据同步工具,可以将部分控制参数配置到启动配置文件resources/application.properties中,然后在代码中通过以下方法获取配置的参数:
187 |
188 | ```ini
189 | #工具主程序
190 | mainclass=org.frameworkset.elasticsearch.imp.Dbdemo
191 |
192 | # 参数配置
193 | # 在代码中获取方法:CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值false
194 | dropIndice=false
195 | ```
196 |
197 | 在代码中获取参数dropIndice方法:
198 |
199 | ```java
200 | boolean dropIndice = CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值false
201 | ```
202 |
203 | 另外可以在resources/application.properties配置控制作业执行的一些参数,例如工作线程数,等待队列数,批处理size等等:
204 |
205 | ```
206 | queueSize=50
207 | workThreads=10
208 | batchSize=20
209 | ```
210 |
211 | 在作业执行方法中获取并使用上述参数:
212 |
213 | ```java
214 | int batchSize = CommonLauncher.getIntProperty("batchSize",10);//同时指定了默认值
215 | int queueSize = CommonLauncher.getIntProperty("queueSize",50);//同时指定了默认值
216 | int workThreads = CommonLauncher.getIntProperty("workThreads",10);//同时指定了默认值
217 | importBuilder.setBatchSize(batchSize);
218 | importBuilder.setQueue(queueSize);//设置批量导入线程池等待队列长度
219 | importBuilder.setThreadCount(workThreads);//设置批量导入线程池工作线程数量
220 | ```
221 |
222 |
223 |
224 | ## 技术交流群:166471282
225 |
226 | ## 微信公众号:bbossgroup
227 | 
228 |
229 |
230 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | def profile = System.getProperty("profile") ?: "dev"
2 | println(profile)
3 | configure(allprojects) { project ->
4 | group = PROJ_GROUP
5 | version = PROJ_VERSION
6 |
7 | apply plugin: "java"
8 | apply plugin: "maven-publish"
9 | apply plugin: "eclipse"
10 | apply plugin: "idea"
11 | apply plugin: "signing"
12 | apply plugin: "java-library"
13 | eclipse {
14 | jdt {
15 | //if you want to alter the java versions (by default they are configured with gradle java plugin settings):
16 | sourceCompatibility = SOURCE_COMPILER_LEVEL
17 | targetCompatibility = TARGET_COMPILER_LEVEL
18 | //javaRuntimeName = "../../org.eclipse.jdt.launching.JRE_CONTAINER"
19 |
20 | }
21 | }
22 |
23 | tasks.withType(JavaCompile) {
24 |
25 | sourceCompatibility = SOURCE_COMPILER_LEVEL
26 | targetCompatibility = TARGET_COMPILER_LEVEL
27 | options.encoding = 'UTF-8'
28 | }
29 |
30 | tasks.withType(Javadoc) {
31 | sourceCompatibility = JAVADOC_SOURCE_LEVEL
32 | targetCompatibility = JAVADOC_COMPILER_LEVEL
33 | options.encoding = 'UTF-8'
34 | // disable the crazy super-strict doclint tool in Java 8
35 | // noinspection SpellCheckingInspection
36 | if (JavaVersion.current().isJava8Compatible()) {
37 | options.addStringOption('Xdoclint:none', '-quiet')
38 | }
39 | }
40 | task sourcesJar(type: Jar) {
41 | archiveClassifier = "sources"
42 | from sourceSets.main.allJava
43 | duplicatesStrategy = DuplicatesStrategy.INCLUDE
44 | }
45 | processResources {
46 | duplicatesStrategy = DuplicatesStrategy.INCLUDE
47 | }
48 | task javaDocJar(type: Jar, dependsOn: javadoc) {
49 | archiveClassifier = "javadoc"
50 | from javadoc.destinationDir
51 | }
52 | artifacts {
53 | archives sourcesJar, javaDocJar
54 | }
55 |
56 | jar {
57 | manifest {
58 | attributes (
59 | 'Implementation': archiveVersion,
60 | 'Specification-Version': archiveVersion,
61 | 'Implementation-Vendor': 'bbossgroups',
62 | 'Implementation-ProductID': project.name,
63 | 'Compile-Timestamp': new Date().format('yyyy-MM-dd HH:mm:ss'),
64 | 'Compile-User': DEVELOPER_NAME
65 | )
66 | }
67 | }
68 |
69 |
70 |
71 |
72 | repositories {
73 | mavenLocal()
74 |
75 | mavenCentral()
76 |
77 | }
78 | if(project.getProperty('skipTest').equals("true"))
79 | {
80 | compileTestJava.enabled=false
81 | processTestResources.enabled=false
82 | testClasses.enabled = false
83 | test.enabled = false
84 | }
85 |
86 | sourceSets {
87 | main {
88 | java {
89 | srcDir 'src/main/java'
90 |
91 | }
92 | if(profile == "dev") {
93 | resources {
94 | srcDir 'src/main/resources'
95 | srcDir 'src/main/java' exclude '**/*.java'
96 | }
97 | }
98 | else{
99 | resources {
100 | srcDir 'src/main/resources' exclude '**/*'
101 | srcDir 'src/main/java' exclude '**/*.java'
102 | }
103 | }
104 |
105 |
106 | // compileClasspath = configurations.api + configurations.runtime
107 | }
108 | test {
109 | java {
110 | srcDir 'src/test/java'
111 |
112 | }
113 | resources {
114 | srcDir 'src/test/resources'
115 | srcDir 'src/test/java' exclude '**/*.java'
116 | }
117 |
118 | }
119 |
120 | }
121 |
122 |
123 | configurations {
124 | //屏蔽log4j
125 | api.exclude group: 'org.slf4j', module: 'slf4j-log4j12'
126 | }
127 | dependencies {
128 | testImplementation 'junit:junit:4.12'
129 | //采用log4j2记录日志
130 | api(
131 | [group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4j2_version}", transitive: false],
132 | [group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4j2_version}", transitive: false],
133 | [group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: "${log4j2_version}", transitive: true],
134 | )
135 | api(
136 | [group: 'com.bbossgroups.plugins', name: 'bboss-datatran-jdbc', version: "${bboss_es_version}", transitive: true],
137 | [group: 'com.bbossgroups', name: 'bboss-schedule', version: "${bboss_version}", transitive: true],
138 | )
139 | api 'org.postgresql:postgresql:42.3.3'
140 | // api 'org.apache-extras.beanshell:bsh:2.0b6'
141 | api 'org.xerial:sqlite-jdbc:3.45.1.0'
142 | //mysql 8.0依赖
143 | //api group: 'mysql', name: 'mysql-connector-java', version: '8.0.33'
144 | //mysql 5.x依赖
145 | api 'com.mysql:mysql-connector-j:8.2.0'
146 | api (
147 | [group: 'com.bbossgroups', name: 'bboss-bootstrap-rt', version: "${bboss_version}", transitive: true]
148 | )
149 | api (
150 | fileTree(dir: 'lib', include: '*.jar')
151 | )
152 | }
153 |
154 |
155 |
156 | task copyJarFiles(type: Sync,dependsOn:'jar'){
157 | from configurations.api{
158 | canBeResolved=true
159 |
160 | }
161 | from jar.outputs
162 | exclude { details -> details.file.name.startsWith('bboss-rt') }
163 | into 'build/dist/lib'
164 |
165 | }
166 |
167 | task copyRTJarFiles(type: Copy,dependsOn:'copyJarFiles'){
168 |
169 | from configurations.api{
170 | canBeResolved=true
171 |
172 | }
173 | include { details -> details.file.name.startsWith('bboss-rt') }
174 | into 'build/dist'
175 | rename ("bboss-rt-${bboss_version}.jar", "${project.name}-${bboss_version}.jar")
176 | }
177 | task copyToolFiles(type: Copy ,dependsOn:'copyRTJarFiles') {
178 |
179 | from ('runfiles')
180 | {
181 | expand(
182 | bboss_version:"${bboss_version}",
183 | project:"${project.name}"
184 | )
185 |
186 | }
187 | filteringCharset = 'UTF-8'
188 | into 'build/dist/'
189 |
190 |
191 |
192 | }
193 | task copyResourcesfiles(type: Sync ) {
194 | from fileTree(dir: 'src/main/resources')
195 | filteringCharset = 'UTF-8'
196 | into 'build/dist/resources'
197 | }
198 |
199 | task releaseVersion(type: Zip,dependsOn:['copyToolFiles','copyResourcesfiles']) {
200 |
201 | //appendix = 'wrapper'
202 | archiveClassifier = 'released'
203 | from 'build/dist/'
204 |
205 | }
206 |
207 | }
208 |
209 |
210 |
211 |
--------------------------------------------------------------------------------
/debug.sql:
--------------------------------------------------------------------------------
1 | CREATE database if NOT EXISTS `xxl-job` default character set utf8 collate utf8_general_ci;
2 | use `xxl-job`;
3 |
4 |
5 |
6 | CREATE TABLE XXL_JOB_QRTZ_JOB_DETAILS
7 | (
8 | SCHED_NAME VARCHAR(120) NOT NULL,
9 | JOB_NAME VARCHAR(200) NOT NULL,
10 | JOB_GROUP VARCHAR(200) NOT NULL,
11 | DESCRIPTION VARCHAR(250) NULL,
12 | JOB_CLASS_NAME VARCHAR(250) NOT NULL,
13 | IS_DURABLE VARCHAR(1) NOT NULL,
14 | IS_NONCONCURRENT VARCHAR(1) NOT NULL,
15 | IS_UPDATE_DATA VARCHAR(1) NOT NULL,
16 | REQUESTS_RECOVERY VARCHAR(1) NOT NULL,
17 | JOB_DATA BLOB NULL,
18 | PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
19 | );
20 |
21 | CREATE TABLE XXL_JOB_QRTZ_TRIGGERS
22 | (
23 | SCHED_NAME VARCHAR(120) NOT NULL,
24 | TRIGGER_NAME VARCHAR(200) NOT NULL,
25 | TRIGGER_GROUP VARCHAR(200) NOT NULL,
26 | JOB_NAME VARCHAR(200) NOT NULL,
27 | JOB_GROUP VARCHAR(200) NOT NULL,
28 | DESCRIPTION VARCHAR(250) NULL,
29 | NEXT_FIRE_TIME BIGINT(13) NULL,
30 | PREV_FIRE_TIME BIGINT(13) NULL,
31 | PRIORITY INTEGER NULL,
32 | TRIGGER_STATE VARCHAR(16) NOT NULL,
33 | TRIGGER_TYPE VARCHAR(8) NOT NULL,
34 | START_TIME BIGINT(13) NOT NULL,
35 | END_TIME BIGINT(13) NULL,
36 | CALENDAR_NAME VARCHAR(200) NULL,
37 | MISFIRE_INSTR SMALLINT(2) NULL,
38 | JOB_DATA BLOB NULL,
39 | PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
40 | FOREIGN KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
41 | REFERENCES XXL_JOB_QRTZ_JOB_DETAILS(SCHED_NAME,JOB_NAME,JOB_GROUP)
42 | );
43 |
44 | CREATE TABLE XXL_JOB_QRTZ_SIMPLE_TRIGGERS
45 | (
46 | SCHED_NAME VARCHAR(120) NOT NULL,
47 | TRIGGER_NAME VARCHAR(200) NOT NULL,
48 | TRIGGER_GROUP VARCHAR(200) NOT NULL,
49 | REPEAT_COUNT BIGINT(7) NOT NULL,
50 | REPEAT_INTERVAL BIGINT(12) NOT NULL,
51 | TIMES_TRIGGERED BIGINT(10) NOT NULL,
52 | PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
53 | FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
54 | REFERENCES XXL_JOB_QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
55 | );
56 |
57 | CREATE TABLE XXL_JOB_QRTZ_CRON_TRIGGERS
58 | (
59 | SCHED_NAME VARCHAR(120) NOT NULL,
60 | TRIGGER_NAME VARCHAR(200) NOT NULL,
61 | TRIGGER_GROUP VARCHAR(200) NOT NULL,
62 | CRON_EXPRESSION VARCHAR(200) NOT NULL,
63 | TIME_ZONE_ID VARCHAR(80),
64 | PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
65 | FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
66 | REFERENCES XXL_JOB_QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
67 | );
68 |
69 | CREATE TABLE XXL_JOB_QRTZ_SIMPROP_TRIGGERS
70 | (
71 | SCHED_NAME VARCHAR(120) NOT NULL,
72 | TRIGGER_NAME VARCHAR(200) NOT NULL,
73 | TRIGGER_GROUP VARCHAR(200) NOT NULL,
74 | STR_PROP_1 VARCHAR(512) NULL,
75 | STR_PROP_2 VARCHAR(512) NULL,
76 | STR_PROP_3 VARCHAR(512) NULL,
77 | INT_PROP_1 INT NULL,
78 | INT_PROP_2 INT NULL,
79 | LONG_PROP_1 BIGINT NULL,
80 | LONG_PROP_2 BIGINT NULL,
81 | DEC_PROP_1 NUMERIC(13,4) NULL,
82 | DEC_PROP_2 NUMERIC(13,4) NULL,
83 | BOOL_PROP_1 VARCHAR(1) NULL,
84 | BOOL_PROP_2 VARCHAR(1) NULL,
85 | PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
86 | FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
87 | REFERENCES XXL_JOB_QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
88 | );
89 |
90 | CREATE TABLE XXL_JOB_QRTZ_BLOB_TRIGGERS
91 | (
92 | SCHED_NAME VARCHAR(120) NOT NULL,
93 | TRIGGER_NAME VARCHAR(200) NOT NULL,
94 | TRIGGER_GROUP VARCHAR(200) NOT NULL,
95 | BLOB_DATA BLOB NULL,
96 | PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
97 | FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
98 | REFERENCES XXL_JOB_QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
99 | );
100 |
101 | CREATE TABLE XXL_JOB_QRTZ_CALENDARS
102 | (
103 | SCHED_NAME VARCHAR(120) NOT NULL,
104 | CALENDAR_NAME VARCHAR(200) NOT NULL,
105 | CALENDAR BLOB NOT NULL,
106 | PRIMARY KEY (SCHED_NAME,CALENDAR_NAME)
107 | );
108 |
109 | CREATE TABLE XXL_JOB_QRTZ_PAUSED_TRIGGER_GRPS
110 | (
111 | SCHED_NAME VARCHAR(120) NOT NULL,
112 | TRIGGER_GROUP VARCHAR(200) NOT NULL,
113 | PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP)
114 | );
115 |
116 | CREATE TABLE XXL_JOB_QRTZ_FIRED_TRIGGERS
117 | (
118 | SCHED_NAME VARCHAR(120) NOT NULL,
119 | ENTRY_ID VARCHAR(95) NOT NULL,
120 | TRIGGER_NAME VARCHAR(200) NOT NULL,
121 | TRIGGER_GROUP VARCHAR(200) NOT NULL,
122 | INSTANCE_NAME VARCHAR(200) NOT NULL,
123 | FIRED_TIME BIGINT(13) NOT NULL,
124 | SCHED_TIME BIGINT(13) NOT NULL,
125 | PRIORITY INTEGER NOT NULL,
126 | STATE VARCHAR(16) NOT NULL,
127 | JOB_NAME VARCHAR(200) NULL,
128 | JOB_GROUP VARCHAR(200) NULL,
129 | IS_NONCONCURRENT VARCHAR(1) NULL,
130 | REQUESTS_RECOVERY VARCHAR(1) NULL,
131 | PRIMARY KEY (SCHED_NAME,ENTRY_ID)
132 | );
133 |
134 | CREATE TABLE XXL_JOB_QRTZ_SCHEDULER_STATE
135 | (
136 | SCHED_NAME VARCHAR(120) NOT NULL,
137 | INSTANCE_NAME VARCHAR(200) NOT NULL,
138 | LAST_CHECKIN_TIME BIGINT(13) NOT NULL,
139 | CHECKIN_INTERVAL BIGINT(13) NOT NULL,
140 | PRIMARY KEY (SCHED_NAME,INSTANCE_NAME)
141 | );
142 |
143 | CREATE TABLE XXL_JOB_QRTZ_LOCKS
144 | (
145 | SCHED_NAME VARCHAR(120) NOT NULL,
146 | LOCK_NAME VARCHAR(40) NOT NULL,
147 | PRIMARY KEY (SCHED_NAME,LOCK_NAME)
148 | );
149 |
150 |
151 |
152 | CREATE TABLE `XXL_JOB_QRTZ_TRIGGER_INFO` (
153 | `id` int(11) NOT NULL AUTO_INCREMENT,
154 | `job_group` int(11) NOT NULL COMMENT '执行器主键ID',
155 | `job_cron` varchar(128) NOT NULL COMMENT '任务执行CRON',
156 | `job_desc` varchar(255) NOT NULL,
157 | `add_time` datetime DEFAULT NULL,
158 | `update_time` datetime DEFAULT NULL,
159 | `author` varchar(64) DEFAULT NULL COMMENT '作者',
160 | `alarm_email` varchar(255) DEFAULT NULL COMMENT '报警邮件',
161 | `executor_route_strategy` varchar(50) DEFAULT NULL COMMENT '执行器路由策略',
162 | `executor_handler` varchar(255) DEFAULT NULL COMMENT '执行器任务handler',
163 | `executor_param` varchar(512) DEFAULT NULL COMMENT '执行器任务参数',
164 | `executor_block_strategy` varchar(50) DEFAULT NULL COMMENT '阻塞处理策略',
165 | `executor_timeout` int(11) NOT NULL DEFAULT '0' COMMENT '任务执行超时时间,单位秒',
166 | `executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失败重试次数',
167 | `glue_type` varchar(50) NOT NULL COMMENT 'GLUE类型',
168 | `glue_source` mediumtext COMMENT 'GLUE源代码',
169 | `glue_remark` varchar(128) DEFAULT NULL COMMENT 'GLUE备注',
170 | `glue_updatetime` datetime DEFAULT NULL COMMENT 'GLUE更新时间',
171 | `child_jobid` varchar(255) DEFAULT NULL COMMENT '子任务ID,多个逗号分隔',
172 | PRIMARY KEY (`id`)
173 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
174 |
175 | CREATE TABLE `XXL_JOB_QRTZ_TRIGGER_LOG` (
176 | `id` int(11) NOT NULL AUTO_INCREMENT,
177 | `job_group` int(11) NOT NULL COMMENT '执行器主键ID',
178 | `job_id` int(11) NOT NULL COMMENT '任务,主键ID',
179 | `executor_address` varchar(255) DEFAULT NULL COMMENT '执行器地址,本次执行的地址',
180 | `executor_handler` varchar(255) DEFAULT NULL COMMENT '执行器任务handler',
181 | `executor_param` varchar(512) DEFAULT NULL COMMENT '执行器任务参数',
182 | `executor_sharding_param` varchar(20) DEFAULT NULL COMMENT '执行器任务分片参数,格式如 1/2',
183 | `executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失败重试次数',
184 | `trigger_time` datetime DEFAULT NULL COMMENT '调度-时间',
185 | `trigger_code` int(11) NOT NULL COMMENT '调度-结果',
186 | `trigger_msg` text COMMENT '调度-日志',
187 | `handle_time` datetime DEFAULT NULL COMMENT '执行-时间',
188 | `handle_code` int(11) NOT NULL COMMENT '执行-状态',
189 | `handle_msg` text COMMENT '执行-日志',
190 | `alarm_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '告警状态:0-默认、1-无需告警、2-告警成功、3-告警失败',
191 | PRIMARY KEY (`id`),
192 | KEY `I_trigger_time` (`trigger_time`),
193 | KEY `I_handle_code` (`handle_code`)
194 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
195 |
196 | CREATE TABLE `XXL_JOB_QRTZ_TRIGGER_LOGGLUE` (
197 | `id` int(11) NOT NULL AUTO_INCREMENT,
198 | `job_id` int(11) NOT NULL COMMENT '任务,主键ID',
199 | `glue_type` varchar(50) DEFAULT NULL COMMENT 'GLUE类型',
200 | `glue_source` mediumtext COMMENT 'GLUE源代码',
201 | `glue_remark` varchar(128) NOT NULL COMMENT 'GLUE备注',
202 | `add_time` timestamp NULL DEFAULT NULL,
203 | `update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
204 | PRIMARY KEY (`id`)
205 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
206 |
207 | CREATE TABLE XXL_JOB_QRTZ_TRIGGER_REGISTRY (
208 | `id` int(11) NOT NULL AUTO_INCREMENT,
209 | `registry_group` varchar(255) NOT NULL,
210 | `registry_key` varchar(255) NOT NULL,
211 | `registry_value` varchar(255) NOT NULL,
212 | `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
213 | PRIMARY KEY (`id`)
214 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
215 |
216 | CREATE TABLE `XXL_JOB_QRTZ_TRIGGER_GROUP` (
217 | `id` int(11) NOT NULL AUTO_INCREMENT,
218 | `app_name` varchar(64) NOT NULL COMMENT '执行器AppName',
219 | `title` varchar(12) NOT NULL COMMENT '执行器名称',
220 | `order` tinyint(4) NOT NULL DEFAULT '0' COMMENT '排序',
221 | `address_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '执行器地址类型:0=自动注册、1=手动录入',
222 | `address_list` varchar(512) DEFAULT NULL COMMENT '执行器地址列表,多地址逗号分隔',
223 | PRIMARY KEY (`id`)
224 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
225 |
226 |
227 |
228 | INSERT INTO `XXL_JOB_QRTZ_TRIGGER_GROUP`(`id`, `app_name`, `title`, `order`, `address_type`, `address_list`) VALUES (1, 'xxl-job-executor-sample', '示例执行器', 1, 0, NULL);
229 | INSERT INTO `XXL_JOB_QRTZ_TRIGGER_INFO`(`id`, `job_group`, `job_cron`, `job_desc`, `add_time`, `update_time`, `author`, `alarm_email`, `executor_route_strategy`, `executor_handler`, `executor_param`, `executor_block_strategy`, `executor_timeout`, `executor_fail_retry_count`, `glue_type`, `glue_source`, `glue_remark`, `glue_updatetime`, `child_jobid`) VALUES (1, 1, '0 0 0 * * ? *', '测试任务1', '2018-11-03 22:21:31', '2018-11-03 22:21:31', 'XXL', '', 'FIRST', 'demoJobHandler', '', 'SERIAL_EXECUTION', 0, 0, 'BEAN', '', 'GLUE代码初始化', '2018-11-03 22:21:31', '');
230 |
231 | commit;
232 |
233 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | PROJ_GROUP=org.frameworkset.db-elasticsearch-tool
2 | PROJ_VERSION=7.3.8
3 |
4 | bboss_version=6.2.9
5 | bboss_es_version=7.3.8
6 | log4j2_version=2.24.3
7 | skipTest=true
8 | PROJ_WEBSITEURL=http://www.bbossgroups.com
9 | PROJ_ISSUETRACKERURL=https://github.com/bbossgroups/db-elasticsearch-tool/issues
10 | PROJ_VCSURL=https://github.com/bbossgroups/db-elasticsearch-tool.git
11 | PROJ_DESCRIPTION=bboss es demo base elasticsearcch.
12 |
13 | PROJ_LICENCE_NAME=The Apache Software License, Version 2.0
14 | PROJ_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
15 | PROJ_LICENCE_DEST=repo
16 |
17 | DEVELOPER_ID=yin-bp
18 | DEVELOPER_NAME=biaoping.yin
19 | DEVELOPER_EMAIL=yin-bp@163.com
20 |
21 | SOURCE_COMPILER_LEVEL=1.8
22 | TARGET_COMPILER_LEVEL=1.8
23 | JAVADOC_SOURCE_LEVEL=1.8
24 | JAVADOC_COMPILER_LEVEL=1.8
25 |
26 |
27 |
28 | sonatype_url=https://oss.sonatype.org/service/local/staging/deploy/maven2
29 | sonatype_username=
30 | sonatype_password=
31 | scm_connection=scm:git:git@github.com:bbossgroups/db-elasticsearch-tool.git
32 | scm_developerConnection=scm:git:git@github.com:bbossgroups/db-elasticsearch-tool.git
33 | scm_url=https://github.com/bbossgroups/db-elasticsearch-tool
34 |
35 | uploadArchivesToMavenCenter=true
36 |
--------------------------------------------------------------------------------
/lib/ojdbc14.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bbossgroups/db-elasticsearch-tool/3980e475fc83003b2d572b80cbd8979daaec8f06/lib/ojdbc14.jar
--------------------------------------------------------------------------------
/lib/readme.txt:
--------------------------------------------------------------------------------
1 | 可以将maven中央库不存在的第三方的包放入lib目录
--------------------------------------------------------------------------------
/release.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | start cmd /k "gradle clean -Dprofile=releaseVersion && gradle releaseVersion -Dprofile=releaseVersion"
3 |
--------------------------------------------------------------------------------
/release.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | gradle clean -Dprofile=releaseVersion && gradle releaseVersion -Dprofile=releaseVersion
3 |
--------------------------------------------------------------------------------
/runfiles/config-cmsdocmenttable.properties:
--------------------------------------------------------------------------------
1 | #工具主程序
2 | mainclass=org.frameworkset.elasticsearch.imp.CMSDocumentImport
3 | pidfile=CMSDocumentImport.pid
--------------------------------------------------------------------------------
/runfiles/config-exportdemo.properties:
--------------------------------------------------------------------------------
1 | #工具主程序
2 | mainclass=org.frameworkset.elasticsearch.imp.ESDemo
3 | pidfile=ESDemo.pid
--------------------------------------------------------------------------------
/runfiles/config-quartztask.properties:
--------------------------------------------------------------------------------
1 | #工具主程序
2 | mainclass=org.frameworkset.task.Main
3 | pidfile=quartz.pid
--------------------------------------------------------------------------------
/runfiles/jvm.options:
--------------------------------------------------------------------------------
1 | ## JVM configuration
2 |
3 | ################################################################
4 | ## IMPORTANT: JVM heap size
5 | ################################################################
6 | ##
7 | ## You should always set the min and max JVM heap
8 | ## size to the same value. For example, to set
9 | ## the heap to 4 GB, set:
10 | ##
11 | ## -Xms4g
12 | ## -Xmx4g
13 | ##
14 | ## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
15 | ## for more information
16 | ##
17 | ################################################################
18 |
19 | # Xms represents the initial size of total heap space
20 | # Xmx represents the maximum size of total heap space
21 |
22 | -Xms1g
23 | -Xmx1g
24 | -XX:NewSize=512m
25 | -XX:MaxNewSize=512m
26 | # explicitly set the stack size
27 | -Xss1m
28 |
29 | ################################################################
30 | ## Expert settings
31 | ################################################################
32 | ##
33 | ## All settings below this section are considered
34 | ## expert settings. Don't tamper with them unless
35 | ## you understand what you are doing
36 | ##
37 | ################################################################
38 |
39 | ## GC configuration
40 | -XX:+UseConcMarkSweepGC
41 | -XX:CMSInitiatingOccupancyFraction=75
42 | -XX:+UseCMSInitiatingOccupancyOnly
43 |
44 | ## optimizations
45 |
46 | # pre-touch memory pages used by the JVM during initialization
47 | -XX:+AlwaysPreTouch
48 |
49 | ## basic
50 |
51 | # force the server VM
52 | -server
53 |
54 |
55 | # set to headless, just in case
56 | -Djava.awt.headless=true
57 |
58 | # ensure UTF-8 encoding by default (e.g. filenames)
59 | #-Dfile.encoding=UTF-8
60 |
61 | # use our provided JNA always versus the system one
62 | -Djna.nosys=true
63 |
64 | # turn off a JDK optimization that throws away stack traces for common
65 | # exceptions because stack traces are important for debugging
66 | -XX:-OmitStackTraceInFastThrow
67 |
68 | # flags to configure Netty
69 | -Dio.netty.noUnsafe=true
70 | -Dio.netty.noKeySetOptimization=true
71 | -Dio.netty.recycler.maxCapacityPerThread=0
72 |
73 | # log4j 2
74 | -Dlog4j.shutdownHookEnabled=false
75 | -Dlog4j2.disable.jmx=true
76 |
77 | ## heap dumps
78 |
79 | # generate a heap dump when an allocation from the Java heap fails
80 | # heap dumps are created in the working directory of the JVM
81 | -XX:+HeapDumpOnOutOfMemoryError
82 |
83 | # specify an alternative path for heap dumps
84 | # ensure the directory exists and has sufficient space
85 | #-XX:HeapDumpPath=/heap/dump/path
86 |
87 | ## GC logging
88 |
89 | #-XX:+PrintGCDetails
90 | #-XX:+PrintGCTimeStamps
91 | #-XX:+PrintGCDateStamps
92 | #-XX:+PrintClassHistogram
93 | #-XX:+PrintTenuringDistribution
94 | #-XX:+PrintGCApplicationStoppedTime
95 |
96 | # log GC status to a file with time stamps
97 | # ensure the directory exists
98 | #-Xloggc:\${loggc}
99 |
100 | # By default, the GC log file will not rotate.
101 | # By uncommenting the lines below, the GC log file
102 | # will be rotated every 128MB at most 32 times.
103 | #-XX:+UseGCLogFileRotation
104 | #-XX:NumberOfGCLogFiles=32
105 | #-XX:GCLogFileSize=128M
106 |
--------------------------------------------------------------------------------
/runfiles/restart-cmsdocumenttable.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | cd `dirname \$0`
3 |
4 | parse_jvm_options() {
5 | if [ -f "\$1" ]; then
6 | echo "`grep "^-" "\$1" | tr '\n' ' '`"
7 | fi
8 | }
9 |
10 | JVM_OPTIONS_FILE=jvm.options
11 |
12 | RT_JAVA_OPTS="`parse_jvm_options "\$JVM_OPTIONS_FILE"` \$RT_JAVA_OPTS"
13 | echo \$RT_JAVA_OPTS
14 | nohup java \$RT_JAVA_OPTS -jar ${project}-${bboss_version}.jar restart --conf=config-cmsdocmenttable.properties --shutdownLevel=9 > ${project}.log &
15 | tail -f ${project}.log
16 |
--------------------------------------------------------------------------------
/runfiles/restart-exportdemo.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | cd `dirname \$0`
3 |
4 | parse_jvm_options() {
5 | if [ -f "\$1" ]; then
6 | echo "`grep "^-" "\$1" | tr '\n' ' '`"
7 | fi
8 | }
9 |
10 | JVM_OPTIONS_FILE=jvm.options
11 |
12 | RT_JAVA_OPTS="`parse_jvm_options "\$JVM_OPTIONS_FILE"` \$RT_JAVA_OPTS"
13 | echo \$RT_JAVA_OPTS
14 | nohup java \$RT_JAVA_OPTS -jar ${project}-${bboss_version}.jar restart --conf=config-exportdemo.properties --shutdownLevel=9 > ${project}.log &
15 | tail -f ${project}.log
16 |
--------------------------------------------------------------------------------
/runfiles/restart-quartz.bat:
--------------------------------------------------------------------------------
1 | %~d0
2 | cd %~dp0
3 | @echo off
4 |
5 | setlocal enabledelayedexpansion
6 | setlocal enableextensions
7 |
8 |
9 | set "JVM_OPTIONS_FILE=jvm.options"
10 |
11 | @setlocal
12 | rem extract the options from the JVM options file %JVM_OPTIONS_FILE%
13 | rem such options are the lines beginning with '-', thus "findstr /b"
14 | for /F "usebackq delims=" %%a in (`findstr /b \\- "%JVM_OPTIONS_FILE%"`) do set JVM_OPTIONS=!JVM_OPTIONS! %%a
15 | @endlocal & set RT_JAVA_OPTS=%JVM_OPTIONS% %RT_JAVA_OPTS%
16 | @echo JVM argements %RT_JAVA_OPTS%
17 | java %RT_JAVA_OPTS% -jar ${project}-${bboss_version}.jar --conf=config-quartztask.properties restart
18 |
19 | endlocal
20 | endlocal
21 |
--------------------------------------------------------------------------------
/runfiles/restart-quartz.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | cd `dirname \$0`
3 |
4 | parse_jvm_options() {
5 | if [ -f "\$1" ]; then
6 | echo "`grep "^-" "\$1" | tr '\n' ' '`"
7 | fi
8 | }
9 |
10 | JVM_OPTIONS_FILE=jvm.options
11 |
12 | RT_JAVA_OPTS="`parse_jvm_options "\$JVM_OPTIONS_FILE"` \$RT_JAVA_OPTS"
13 | echo \$RT_JAVA_OPTS
14 | nohup java \$RT_JAVA_OPTS -jar ${project}-${bboss_version}.jar --conf=config-quartztask.properties restart --shutdownLevel=9 > ${project}.log &
15 | tail -f ${project}.log
16 |
--------------------------------------------------------------------------------
/runfiles/restart.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | echo stop ...
3 | call stop.bat
4 | echo over
5 | echo starting ...
6 | call startup.bat
--------------------------------------------------------------------------------
/runfiles/restart.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | sh stop.sh
3 | sh startup.sh
--------------------------------------------------------------------------------
/runfiles/run.bat:
--------------------------------------------------------------------------------
1 | %~d0
2 | cd %~dp0
3 | @echo off
4 |
5 |
6 | setlocal enabledelayedexpansion
7 | setlocal enableextensions
8 | set "JVM_OPTIONS_FILE=jvm.options"
9 |
10 | @setlocal
11 | rem extract the options from the JVM options file %JVM_OPTIONS_FILE%
12 | rem such options are the lines beginning with '-', thus "findstr /b"
13 | for /F "usebackq delims=" %%a in (`findstr /b \\- "%JVM_OPTIONS_FILE%"`) do set JVM_OPTIONS=!JVM_OPTIONS! %%a
14 | @endlocal & set RT_JAVA_OPTS=%JVM_OPTIONS% %RT_JAVA_OPTS%
15 | @echo JVM argements %RT_JAVA_OPTS%
16 | if ""%1""==""start"" goto start
17 | if ""%1""==""stop"" goto stop
18 | if ""%1""==""restart"" goto restart
19 | if ""%1""=="""" goto info
20 | :info
21 |
22 | echo Usage:
23 | echo ${project} keyword
24 | echo ----------------------------------------------------------------
25 | echo start -- Start ${project}
26 | echo stop -- stop ${project}
27 | echo restart -- Restart ${project}
28 | echo ----------------------------------------------------------------
29 |
30 | goto end
31 |
32 |
33 | :start
34 | java %RT_JAVA_OPTS% -jar ${project}-${bboss_version}.jar --conf=resources/application.properties
35 | goto end
36 | :stop
37 | java -jar ${project}-${bboss_version}.jar stop --conf=resources/application.properties
38 | goto end
39 | :restart
40 | java %RT_JAVA_OPTS% -jar ${project}-${bboss_version}.jar restart --conf=resources/application.properties
41 | goto end
42 |
43 | :end
44 | echo end
45 | endlocal
46 | endlocal
--------------------------------------------------------------------------------
/runfiles/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | cd `dirname \$0`
3 |
4 | parse_jvm_options() {
5 | if [ -f "\$1" ]; then
6 | echo "`grep "^-" "\$1" | tr '\n' ' '`"
7 | fi
8 | }
9 |
10 | JVM_OPTIONS_FILE=jvm.options
11 |
12 | RT_JAVA_OPTS="`parse_jvm_options "\$JVM_OPTIONS_FILE"` \$RT_JAVA_OPTS"
13 | echo \$RT_JAVA_OPTS
14 | case \$1 in
15 | start)
16 | nohup java \$RT_JAVA_OPTS -jar ${project}-${bboss_version}.jar --conf=resources/application.properties > ${project}.log &
17 | tail -f ${project}.log
18 | ;;
19 | stop)
20 | java -jar ${project}-${bboss_version}.jar stop --shutdownLevel=9 --conf=resources/application.properties
21 | ;;
22 | restart)
23 | nohup java \$RT_JAVA_OPTS -jar ${project}-${bboss_version}.jar restart --shutdownLevel=9 --conf=resources/application.properties > ${project}.log &
24 | tail -f ${project}.log
25 | ;;
26 | *)
27 | echo
28 | echo "Usage:";
29 | echo " ${project} keyword [value1 [value2]] ";
30 | echo " ----------------------------------------------------------------";
31 | echo " start -- Start ${project}";
32 | echo " stop -- stop ${project}";
33 | echo " restart -- Restart ${project}";
34 | echo " ----------------------------------------------------------------";
35 | echo
36 | ;;
37 | esac
38 |
--------------------------------------------------------------------------------
/runfiles/startup-cmsdocumenttable.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | cd `dirname \$0`
3 |
4 | parse_jvm_options() {
5 | if [ -f "\$1" ]; then
6 | echo "`grep "^-" "\$1" | tr '\n' ' '`"
7 | fi
8 | }
9 |
10 | JVM_OPTIONS_FILE=jvm.options
11 |
12 | RT_JAVA_OPTS="`parse_jvm_options "\$JVM_OPTIONS_FILE"` \$RT_JAVA_OPTS"
13 | echo \$RT_JAVA_OPTS
14 | nohup java \$RT_JAVA_OPTS -jar ${project}-${bboss_version}.jar --conf=config-cmsdocmenttable.properties > ${project}.log &
15 | tail -f ${project}.log
16 |
--------------------------------------------------------------------------------
/runfiles/startup.bat:
--------------------------------------------------------------------------------
1 | %~d0
2 | cd %~dp0
3 | @echo off
4 |
5 | setlocal enabledelayedexpansion
6 | setlocal enableextensions
7 |
8 |
9 | set "JVM_OPTIONS_FILE=jvm.options"
10 |
11 | @setlocal
12 | rem extract the options from the JVM options file %JVM_OPTIONS_FILE%
13 | rem such options are the lines beginning with '-', thus "findstr /b"
14 | for /F "usebackq delims=" %%a in (`findstr /b \\- "%JVM_OPTIONS_FILE%"`) do set JVM_OPTIONS=!JVM_OPTIONS! %%a
15 | @endlocal & set RT_JAVA_OPTS=%JVM_OPTIONS% %RT_JAVA_OPTS%
16 | @echo JVM argements %RT_JAVA_OPTS%
17 | java %RT_JAVA_OPTS% -jar ${project}-${bboss_version}.jar --conf=resources/application.properties
18 |
19 | endlocal
20 | endlocal
21 |
--------------------------------------------------------------------------------
/runfiles/startup.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | cd `dirname \$0`
3 |
4 | parse_jvm_options() {
5 | if [ -f "\$1" ]; then
6 | echo "`grep "^-" "\$1" | tr '\n' ' '`"
7 | fi
8 | }
9 |
10 | JVM_OPTIONS_FILE=jvm.options
11 |
12 | RT_JAVA_OPTS="`parse_jvm_options "\$JVM_OPTIONS_FILE"` \$RT_JAVA_OPTS"
13 | echo \$RT_JAVA_OPTS
14 | nohup java \$RT_JAVA_OPTS -jar ${project}-${bboss_version}.jar --conf=resources/application.properties > ${project}.log &
15 | tail -f ${project}.log
16 |
--------------------------------------------------------------------------------
/runfiles/stop-cmsdocumenttable.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | cd `dirname \$0`
3 | java -jar ${project}-${bboss_version}.jar stop --conf=config-cmsdocmenttable.properties --shutdownLevel=C
4 |
--------------------------------------------------------------------------------
/runfiles/stop-quartz.bat:
--------------------------------------------------------------------------------
1 | %~d0
2 | cd %~dp0
3 | java -jar ${project}-${bboss_version}.jar --conf=config-quartztask.properties stop
--------------------------------------------------------------------------------
/runfiles/stop-quartz.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | cd `dirname \$0`
3 | java -jar ${project}-${bboss_version}.jar stop --conf=config-quartztask.properties --shutdownLevel=C
4 |
--------------------------------------------------------------------------------
/runfiles/stop.bat:
--------------------------------------------------------------------------------
1 | %~d0
2 | cd %~dp0
3 | java -jar ${project}-${bboss_version}.jar --conf=resources/application.properties stop
--------------------------------------------------------------------------------
/runfiles/stop.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | cd `dirname \$0`
3 | java -jar ${project}-${bboss_version}.jar stop --conf=resources/application.properties --shutdownLevel=C
4 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/BigTableDemo.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2020 bboss
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.elasticsearch.ElasticSearchHelper;
19 | import org.frameworkset.tran.DataStream;
20 | import org.frameworkset.tran.ExportResultHandler;
21 | import org.frameworkset.tran.config.ImportBuilder;
22 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
23 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
24 | import org.frameworkset.tran.schedule.ImportIncreamentConfig;
25 | import org.frameworkset.tran.task.TaskCommand;
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | *
Description:
31 | *
32 | * Copyright (c) 2020
33 | * @Date 2020/5/29 10:45
34 | * @author biaoping.yin
35 | * @version 1.0
36 | */
37 | public class BigTableDemo {
38 | private static Logger logger = LoggerFactory.getLogger(BigTableDemo.class);
39 | public static void main(String args[]){
40 | BigTableDemo dbdemo = new BigTableDemo();
41 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
42 | dbdemo.fullAutoUUIDImportData(dropIndice);
43 | }
44 | /**
45 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
46 | */
47 | public void fullAutoUUIDImportData(boolean dropIndice){
48 | ImportBuilder importBuilder = ImportBuilder.newInstance();
49 | //增量定时任务不要删表,但是可以通过删表来做初始化操作
50 | if(dropIndice) {
51 | try {
52 | //清除测试表,导入的时候回重建表,测试的时候加上为了看测试效果,实际线上环境不要删表
53 | String repsonse = ElasticSearchHelper.getRestClientUtil().dropIndice("bigtable");
54 | System.out.println(repsonse);
55 | } catch (Exception e) {
56 | }
57 | }
58 |
59 |
60 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
61 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
62 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
63 | // log_id和数据库对应的字段一致,就不需要设置setLastValueColumn信息,
64 | // 但是需要设置setLastValueType告诉工具增量字段的类型
65 | DBInputConfig dbInputConfig = new DBInputConfig();
66 | dbInputConfig.setSql("select * from bigtable ");
67 | importBuilder.setInputConfig(dbInputConfig);
68 | // importBuilder.setSql("select * from td_sm_log ");
69 | /**
70 | * es相关配置
71 | */
72 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
73 | elasticsearchOutputConfig
74 | .setIndex("bigtable") ;//必填项
75 | // .setIndexType("bigtable") //es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType
76 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
77 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
78 | importBuilder.setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
79 | .setUseLowcase(false) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
80 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
81 | .setBatchSize(5000); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
82 |
83 | // //定时任务配置,
84 | // importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
85 | //// .setScheduleDate(date) //指定任务开始执行时间:日期
86 | // .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
87 | // .setPeriod(10000L); //每隔period毫秒执行,如果不设置,只执行一次
88 | //定时任务配置结束
89 | //
90 | // //设置任务执行拦截器,可以添加多个
91 | // importBuilder.addCallInterceptor(new CallInterceptor() {
92 | // @Override
93 | // public void preCall(TaskContext taskContext) {
94 | // System.out.println("preCall");
95 | // }
96 | //
97 | // @Override
98 | // public void afterCall(TaskContext taskContext) {
99 | // System.out.println("afterCall");
100 | // }
101 | //
102 | // @Override
103 | // public void throwException(TaskContext taskContext, Throwable e) {
104 | // System.out.println("throwException");
105 | // }
106 | // }).addCallInterceptor(new CallInterceptor() {
107 | // @Override
108 | // public void preCall(TaskContext taskContext) {
109 | // System.out.println("preCall 1");
110 | // }
111 | //
112 | // @Override
113 | // public void afterCall(TaskContext taskContext) {
114 | // System.out.println("afterCall 1");
115 | // }
116 | //
117 | // @Override
118 | // public void throwException(TaskContext taskContext, Throwable e) {
119 | // System.out.println("throwException 1");
120 | // }
121 | // });
122 | // //设置任务执行拦截器结束,可以添加多个
123 | //增量配置开始
124 | // importBuilder.setLastValueColumn("log_id");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段
125 | importBuilder.setFromFirst(true);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据,
126 | //setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据
127 | importBuilder.setLastValueStorePath("logtable_import");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样
128 | // importBuilder.setLastValueStoreTableName("logs");//记录上次采集的增量字段值的表,可以不指定,采用默认表名increament_tab
129 | importBuilder.setLastValueType(ImportIncreamentConfig.NUMBER_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型
130 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型
131 | //增量配置结束
132 |
133 | //映射和转换配置开始
134 | // /**
135 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
136 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
137 | // */
138 | // importBuilder.addFieldMapping("document_id","docId")
139 | // .addFieldMapping("docwtime","docwTime")
140 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
141 | //
142 | //
143 | // /**
144 | // * 为每条记录添加额外的字段和值
145 | // * 可以为基本数据类型,也可以是复杂的对象
146 | // */
147 | // importBuilder.addFieldValue("testF1","f1value");
148 | // importBuilder.addFieldValue("testInt",0);
149 | // importBuilder.addFieldValue("testDate",new Date());
150 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
151 | // TestObject testObject = new TestObject();
152 | // testObject.setId("testid");
153 | // testObject.setName("jackson");
154 | // importBuilder.addFieldValue("testObject",testObject);
155 | //
156 | // /**
157 | // * 重新设置es数据结构
158 | // */
159 | // importBuilder.setDataRefactor(new DataRefactor() {
160 | // public void refactor(Context context) throws Exception {
161 | // CustomObject customObject = new CustomObject();
162 | // customObject.setAuthor((String)context.getValue("author"));
163 | // customObject.setTitle((String)context.getValue("title"));
164 | // customObject.setSubtitle((String)context.getValue("subtitle"));
165 | // customObject.setIds(new int[]{1,2,3});
166 | // context.addFieldValue("docInfo",customObject);//如果还需要构建更多的内部对象,可以继续构建
167 | //
168 | // //上述三个属性已经放置到docInfo中,如果无需再放置到索引文档中,可以忽略掉这些属性
169 | // context.addIgnoreFieldMapping("author");
170 | // context.addIgnoreFieldMapping("title");
171 | // context.addIgnoreFieldMapping("subtitle");
172 | // }
173 | // });
174 | //映射和转换配置结束
175 |
176 | /**
177 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
178 | */
179 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行
180 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
181 | importBuilder.setThreadCount(10);//设置批量导入线程池工作线程数量
182 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
183 |
184 | /**
185 | importBuilder.setEsIdGenerator(new EsIdGenerator() {
186 | //如果指定EsIdGenerator,则根据下面的方法生成文档id,
187 | // 否则根据setEsIdField方法设置的字段值作为文档id,
188 | // 如果默认没有配置EsIdField和如果指定EsIdGenerator,则由es自动生成文档id
189 |
190 | @Override
191 | public Object genId(Context context) throws Exception {
192 | return SimpleStringUtil.getUUID();//返回null,则由es自动生成文档id
193 | }
194 | });
195 | */
196 |
197 | //设置数据bulk导入任务结果处理回调函数,对每次bulk任务的结果进行成功和失败反馈,然后针对失败的bulk任务通过error方法进行相应处理
198 | importBuilder.setExportResultHandler(new ExportResultHandler() {
199 | @Override
200 | public void success(TaskCommand taskCommand, String result) {
201 | // String datas = taskCommand.getDatas();//执行的批量数据
202 | // System.out.println(result);//打印成功结果
203 | }
204 |
205 | @Override
206 | public void error(TaskCommand taskCommand, String result) {
207 | //任务执行完毕,但是结果中包含错误信息
208 | //具体怎么处理失败数据可以自行决定,下面的示例显示重新导入失败数据的逻辑:
209 | // 从result中分析出导入失败的记录,然后重新构建data,设置到taskCommand中,重新导入,
210 | // 支持的导入次数由getMaxRetry方法返回的数字决定
211 | // String failDatas = ...;
212 | //taskCommand.setDatas(failDatas);
213 | //taskCommand.execute();
214 | // String datas = taskCommand.getDatas();//执行的批量数据
215 | // System.out.println(result);//打印成功结果
216 | logger.info("error occour-------------------------------------------");
217 |
218 | }
219 |
220 | @Override
221 | public void exception(TaskCommand< String> taskCommand, Throwable exception) {
222 | //任务执行抛出异常,失败处理方法
223 | logger.error("",exception);
224 | }
225 |
226 | /**
227 | * 如果对于执行有错误的任务,可以进行修正后重新执行,通过本方法
228 | * 返回允许的最大重试次数
229 | * @return
230 | */
231 | // @Override
232 | // public int getMaxRetry() {
233 | // return -1;
234 | // }
235 | });
236 | /**
237 | * 执行数据库表数据导入es操作
238 | */
239 | DataStream dataStream = importBuilder.builder();
240 | dataStream.execute();//执行导入操作
241 |
242 |
243 |
244 | }
245 | }
246 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/CMSDocumentImport.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.elasticsearch.ElasticSearchHelper;
19 | import org.frameworkset.tran.DataStream;
20 | import org.frameworkset.tran.config.ImportBuilder;
21 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
22 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
23 | import org.frameworkset.tran.schedule.ImportIncreamentConfig;
24 |
25 | /**
26 | *
Description: 同步处理程序,如需调试同步功能,直接运行main方法即可
27 | *
28 | * Copyright (c) 2018
29 | * @Date 2018/10/16 18:58
30 | * @author biaoping.yin
31 | * @version 1.0
32 | */
33 | public class CMSDocumentImport {
34 | public static void main(String args[]){
35 | CMSDocumentImport dbdemo = new CMSDocumentImport();
36 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
37 | dbdemo.scheduleImportData( dropIndice);
38 | }
39 |
40 | /**
41 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
42 | */
43 | public void scheduleImportData(boolean dropIndice){
44 | ImportBuilder importBuilder = ImportBuilder.newInstance();
45 | //增量定时任务不要删表,但是可以通过删表来做初始化操作
46 | if(dropIndice) {
47 | try {
48 | //清除测试表,导入的时候回重建表,测试的时候加上为了看测试效果,实际线上环境不要删表
49 | ElasticSearchHelper.getRestClientUtil().dropIndice("dbclobdemo");
50 | } catch (Exception e) {
51 | }
52 | }
53 | // importBuilder.setDbAdaptor("org.frameworkset.elasticsearch.imp.DMAdaptor");
54 |
55 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
56 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
57 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
58 | // log_id和数据库对应的字段一致,就不需要设置setLastValueColumn信息,
59 | // 但是需要设置setLastValueType告诉工具增量字段的类型
60 | DBInputConfig dbInputConfig = new DBInputConfig();
61 | dbInputConfig.setSql("select * from td_cms_document where document_id > #[document_id]");
62 | importBuilder.setInputConfig(dbInputConfig);
63 | /**
64 | * es相关配置
65 | */
66 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
67 | elasticsearchOutputConfig
68 | .setIndex("dbclobdemo") //必填项
69 | .setIndexType("_doc")
70 | .setEsIdField("document_id")//设置文档主键,不设置,则自动产生文档id
71 | .setDebugResponse(false)//设置是否将每次处理的reponse打印到日志文件中,默认false
72 | .setDiscardBulkResponse(true);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false;//es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType
73 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
74 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
75 | importBuilder.setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
76 | .setUseLowcase(true) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
77 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
78 | .setBatchSize(5000); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
79 |
80 | //定时任务配置,
81 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
82 | // .setScheduleDate(date) //指定任务开始执行时间:日期
83 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
84 | .setPeriod(10000L); //每隔period毫秒执行,如果不设置,只执行一次
85 | //定时任务配置结束
86 | //
87 | // //设置任务执行拦截器,可以添加多个
88 | // importBuilder.addCallInterceptor(new CallInterceptor() {
89 | // @Override
90 | // public void preCall(TaskContext taskContext) {
91 | // System.out.println("preCall");
92 | // }
93 | //
94 | // @Override
95 | // public void afterCall(TaskContext taskContext) {
96 | // System.out.println("afterCall");
97 | // }
98 | //
99 | // @Override
100 | // public void throwException(TaskContext taskContext, Throwable e) {
101 | // System.out.println("throwException");
102 | // }
103 | // }).addCallInterceptor(new CallInterceptor() {
104 | // @Override
105 | // public void preCall(TaskContext taskContext) {
106 | // System.out.println("preCall 1");
107 | // }
108 | //
109 | // @Override
110 | // public void afterCall(TaskContext taskContext) {
111 | // System.out.println("afterCall 1");
112 | // }
113 | //
114 | // @Override
115 | // public void throwException(TaskContext taskContext, Throwable e) {
116 | // System.out.println("throwException 1");
117 | // }
118 | // });
119 | // //设置任务执行拦截器结束,可以添加多个
120 | //增量配置开始
121 | // importBuilder.setLastValueColumn("log_id");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段
122 | importBuilder.setFromFirst(true);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据,
123 | //setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据
124 | importBuilder.setLastValueStorePath("cmsdocumenttable_import");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样
125 | // importBuilder.setLastValueStoreTableName("logs");//记录上次采集的增量字段值的表,可以不指定,采用默认表名increament_tab
126 | importBuilder.setLastValueType(ImportIncreamentConfig.NUMBER_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型
127 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型
128 | //增量配置结束
129 |
130 | //映射和转换配置开始
131 | // /**
132 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
133 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
134 | // */
135 | // importBuilder.addFieldMapping("document_id","docId")
136 | // .addFieldMapping("docwtime","docwTime")
137 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
138 | //
139 | //
140 | // /**
141 | // * 为每条记录添加额外的字段和值
142 | // * 可以为基本数据类型,也可以是复杂的对象
143 | // */
144 | // importBuilder.addFieldValue("testF1","f1value");
145 | // importBuilder.addFieldValue("testInt",0);
146 | // importBuilder.addFieldValue("testDate",new Date());
147 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
148 | // TestObject testObject = new TestObject();
149 | // testObject.setId("testid");
150 | // testObject.setName("jackson");
151 | // importBuilder.addFieldValue("testObject",testObject);
152 | //
153 | // /**
154 | // * 重新设置es数据结构
155 | // */
156 | // importBuilder.setDataRefactor(new DataRefactor() {
157 | // public void refactor(Context context) throws Exception {
158 | // CustomObject customObject = new CustomObject();
159 | // customObject.setAuthor((String)context.getValue("author"));
160 | // customObject.setTitle((String)context.getValue("title"));
161 | // customObject.setSubtitle((String)context.getValue("subtitle"));
162 | // context.addFieldValue("docInfo",customObject);//如果还需要构建更多的内部对象,可以继续构建
163 | //
164 | // //上述三个属性已经放置到docInfo中,如果无需再放置到索引文档中,可以忽略掉这些属性
165 | // context.addIgnoreFieldMapping("author");
166 | // context.addIgnoreFieldMapping("title");
167 | // context.addIgnoreFieldMapping("subtitle");
168 | // }
169 | // });
170 | //映射和转换配置结束
171 |
172 | /**
173 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
174 | */
175 | importBuilder.setParallel(false);//设置为多线程并行批量导入,false串行
176 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
177 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
178 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
179 |
180 |
181 | /**
182 | * 执行数据库表数据导入es操作
183 | */
184 | DataStream dataStream = importBuilder.builder();
185 | dataStream.execute();//执行导入操作
186 |
187 |
188 |
189 | }
190 | }
191 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/CustomObject.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | *
Description:
20 | *
21 | * Copyright (c) 2018
22 | * @Date 2018/9/4 10:37
23 | * @author biaoping.yin
24 | * @version 1.0
25 | */
26 | public class CustomObject {
27 | private String title;
28 | private String subtitle;
29 | private String author;
30 | private int[] ids;
31 |
32 | public String getTitle() {
33 | return title;
34 | }
35 |
36 | public void setTitle(String title) {
37 | this.title = title;
38 | }
39 |
40 | public String getSubtitle() {
41 | return subtitle;
42 | }
43 |
44 | public void setSubtitle(String subtitle) {
45 | this.subtitle = subtitle;
46 | }
47 |
48 | public String getAuthor() {
49 | return author;
50 | }
51 |
52 | public void setAuthor(String author) {
53 | this.author = author;
54 | }
55 |
56 | public int[] getIds() {
57 | return ids;
58 | }
59 |
60 | public void setIds(int[] ids) {
61 | this.ids = ids;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/DMAdaptor.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.frameworkset.orm.adapter.DBOracle;
19 |
20 | /**
21 | *
Description: 达梦数据库adaptor
22 | *
23 | * Copyright (c) 2018
24 | * @Date 2019/4/21 13:28
25 | * @author biaoping.yin
26 | * @version 1.0
27 | */
28 | public class DMAdaptor extends DBOracle {
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/DbTimestamp.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.frameworkset.util.SimpleStringUtil;
19 | import org.frameworkset.elasticsearch.ElasticSearchHelper;
20 | import org.frameworkset.tran.DataRefactor;
21 | import org.frameworkset.tran.DataStream;
22 | import org.frameworkset.tran.EsIdGenerator;
23 | import org.frameworkset.tran.ExportResultHandler;
24 | import org.frameworkset.tran.config.ImportBuilder;
25 | import org.frameworkset.tran.context.Context;
26 | import org.frameworkset.tran.metrics.TaskMetrics;
27 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
28 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
29 | import org.frameworkset.tran.schedule.CallInterceptor;
30 | import org.frameworkset.tran.schedule.ImportIncreamentConfig;
31 | import org.frameworkset.tran.schedule.TaskContext;
32 | import org.frameworkset.tran.task.TaskCommand;
33 | import org.slf4j.Logger;
34 | import org.slf4j.LoggerFactory;
35 |
36 | import java.util.Date;
37 |
38 | /**
39 | *
Description: 基于时间戳db-es增量同步案例,如需调试同步功能,直接运行main方法debug即可
40 | *
41 | * Copyright (c) 2018
42 | * @Date 2018/9/27 20:38
43 | * @author biaoping.yin
44 | * @version 1.0
45 | */
46 | public class DbTimestamp {
47 | private static Logger logger = LoggerFactory.getLogger(DbTimestamp.class);
48 | public static void main(String args[]){
49 | DbTimestamp dbdemo = new DbTimestamp();
50 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
51 | // dbdemo.fullImportData( dropIndice);
52 | // dbdemo.scheduleImportData(dropIndice);
53 | dbdemo.scheduleTimestampImportData(dropIndice);
54 | // dbdemo.scheduleImportData(dropIndice);
55 | // args[1].charAt(0) == args[2].charAt(0);
56 | }
57 |
58 | /**
59 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
60 | */
61 | public void scheduleTimestampImportData(boolean dropIndice){
62 | ImportBuilder importBuilder = ImportBuilder.newInstance();
63 | //增量定时任务不要删表,但是可以通过删表来做初始化操作
64 | if(dropIndice) {
65 | try {
66 | //清除测试表,导入的时候回重建表,测试的时候加上为了看测试效果,实际线上环境不要删表
67 | String repsonse = ElasticSearchHelper.getRestClientUtil().dropIndice("dbtimedemo");
68 | System.out.println(repsonse);
69 | } catch (Exception e) {
70 | }
71 | }
72 |
73 |
74 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
75 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
76 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
77 | // 需要设置setLastValueColumn信息log_id,
78 | // 通过setLastValueType方法告诉工具增量字段的类型,默认是数字类型
79 | DBInputConfig dbInputConfig = new DBInputConfig();
80 | dbInputConfig.setDbName("test")
81 | .setSql("select * from td_sm_log where LOG_OPERTIME > #[LOG_OPERTIME]");
82 | importBuilder.setInputConfig(dbInputConfig);
83 |
84 | /**
85 | * es相关配置
86 | */
87 | // importBuilder.setTargetElasticsearch("default,test");//同步数据到两个es集群
88 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
89 | elasticsearchOutputConfig.setTargetElasticsearch("test");
90 | elasticsearchOutputConfig
91 | .setIndex("dbtimedemo") ;//必填项
92 | // .setIndexType("dbdemo") //es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType
93 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
94 | //elasticsearchOutputConfig.setEsIdField("log_id");//设置文档主键,不设置,则自动产生文档id
95 |
96 | elasticsearchOutputConfig.setDebugResponse(false);//设置是否将每次处理的reponse打印到日志文件中,默认false
97 | elasticsearchOutputConfig.setDiscardBulkResponse(false);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false
98 |
99 | elasticsearchOutputConfig.setEsIdGenerator(new EsIdGenerator() {
100 | //如果指定EsIdGenerator,则根据下面的方法生成文档id,
101 | // 否则根据setEsIdField方法设置的字段值作为文档id,
102 | // 如果默认没有配置EsIdField和如果指定EsIdGenerator,则由es自动生成文档id
103 |
104 | @Override
105 | public Object genId(Context context) throws Exception {
106 | return SimpleStringUtil.getUUID();//返回null,则由es自动生成文档id
107 | }
108 | });
109 |
110 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
111 |
112 | importBuilder.setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
113 | .setUseLowcase(true) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
114 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
115 | .setBatchSize(10); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
116 |
117 | //定时任务配置,
118 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
119 | // .setScheduleDate(date) //指定任务开始执行时间:日期
120 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
121 | .setPeriod(5000L); //每隔period毫秒执行,如果不设置,只执行一次
122 | //定时任务配置结束
123 | //
124 | // //设置任务执行拦截器,可以添加多个,定时任务每次执行的拦截器
125 | importBuilder.addCallInterceptor(new CallInterceptor() {
126 | @Override
127 | public void preCall(TaskContext taskContext) {
128 |
129 | }
130 |
131 | @Override
132 | public void afterCall(TaskContext taskContext) {
133 | logger.info(taskContext.getJobTaskMetrics().toString());
134 | }
135 |
136 | @Override
137 | public void throwException(TaskContext taskContext, Throwable e) {
138 | System.out.println("throwException");
139 | }
140 | });
141 | // //设置任务执行拦截器结束,可以添加多个
142 | //增量配置开始
143 | // importBuilder.setStatusDbname("test");//设置增量状态数据源名称
144 | importBuilder.setLastValueColumn("LOG_OPERTIME");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段
145 | importBuilder.setFromFirst(false);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据,
146 | // setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据
147 | importBuilder.setLastValueStorePath("dbtimees_import");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样
148 | importBuilder.setLastValueType(ImportIncreamentConfig.TIMESTAMP_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型
149 | // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
150 | // try {
151 | // Date date = format.parse("2000-01-01");
152 | // importBuilder.setLastValue(date);//增量起始值配置
153 | // }
154 | // catch (Exception e){
155 | // e.printStackTrace();
156 | // }
157 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型
158 | //增量配置结束
159 |
160 | //
161 | /**
162 | * 重新设置es数据结构
163 | */
164 | importBuilder.setDataRefactor(new DataRefactor() {
165 | public void refactor(Context context) throws Exception {
166 | // Date date = context.getDateValue("LOG_OPERTIME");
167 | context.addFieldValue("collecttime",new Date());
168 | }
169 | });
170 | //映射和转换配置结束
171 |
172 | /**
173 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
174 | */
175 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行
176 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
177 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
178 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
179 |
180 | importBuilder.setExportResultHandler(new ExportResultHandler() {
181 | @Override
182 | public void success(TaskCommand taskCommand, String result) {
183 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
184 | logger.debug(taskMetrics.toString());
185 | logger.debug(result);
186 | }
187 |
188 | @Override
189 | public void error(TaskCommand taskCommand, String result) {
190 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
191 | logger.info(taskMetrics.toString());
192 | logger.warn(result);
193 | }
194 |
195 | @Override
196 | public void exception(TaskCommand taskCommand, Throwable exception) {
197 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
198 | logger.warn(taskMetrics.toString(),exception);
199 | }
200 |
201 |
202 | });
203 | /**
204 | * 执行数据库表数据导入es操作
205 | */
206 | DataStream dataStream = importBuilder.builder();
207 | dataStream.execute();//执行导入操作
208 | // dataStream.destroy();//释放资源
209 |
210 |
211 | }
212 |
213 |
214 | }
215 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/Dbdemo.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.tran.DataRefactor;
19 | import org.frameworkset.tran.DataStream;
20 | import org.frameworkset.tran.ExportResultHandler;
21 | import org.frameworkset.tran.config.ImportBuilder;
22 | import org.frameworkset.tran.context.Context;
23 | import org.frameworkset.tran.metrics.TaskMetrics;
24 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
25 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
26 | import org.frameworkset.tran.schedule.CallInterceptor;
27 | import org.frameworkset.tran.schedule.ImportIncreamentConfig;
28 | import org.frameworkset.tran.schedule.TaskContext;
29 | import org.frameworkset.tran.task.TaskCommand;
30 | import org.slf4j.Logger;
31 | import org.slf4j.LoggerFactory;
32 |
33 | import java.util.Date;
34 |
35 | /**
36 | *
Description: 基于数字类型db-es增量同步案例,同步处理程序,如需调试同步功能,直接运行main方法即可
37 | *
38 | * Copyright (c) 2018
39 | * @Date 2018/9/27 20:38
40 | * @author biaoping.yin
41 | * @version 1.0
42 | */
43 | public class Dbdemo {
44 | private static Logger logger = LoggerFactory.getLogger(Dbdemo.class);
45 | public static void main(String args[]){
46 | Dbdemo dbdemo = new Dbdemo();
47 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
48 | // dbdemo.fullImportData( dropIndice);
49 | // dbdemo.scheduleImportData(dropIndice);
50 | dbdemo.scheduleTimestampImportData(dropIndice);
51 | // dbdemo.scheduleImportData(dropIndice);
52 | // args[1].charAt(0) == args[2].charAt(0);
53 | }
54 |
55 | /**
56 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
57 | */
58 | public void scheduleTimestampImportData(boolean dropIndice){
59 | ImportBuilder importBuilder = new ImportBuilder() ;
60 | DBInputConfig dbInputConfig = new DBInputConfig();
61 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
62 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
63 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
64 | // 需要设置setLastValueColumn信息log_id,
65 | // 通过setLastValueType方法告诉工具增量字段的类型,默认是数字类型
66 |
67 | // importBuilder.setSql("select * from td_sm_log where LOG_OPERTIME > #[LOG_OPERTIME]");
68 | dbInputConfig.setSql("select log_id, log_operuser, op_orgid, oper_module, log_visitorial, log_opertime, log_content, remark1, oper_type, testint " +
69 | "from td_sm_log where log_id > #[log_id]")
70 | .setDbName("test");
71 | dbInputConfig.setParallelDatarefactor(true);
72 | importBuilder.setInputConfig(dbInputConfig);
73 |
74 |
75 | // importBuilder.addFieldMapping("LOG_CONTENT","message");
76 | // importBuilder.addIgnoreFieldMapping("remark1");
77 | // importBuilder.setSql("select * from td_sm_log ");
78 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
79 | elasticsearchOutputConfig.setTargetElasticsearch("default")
80 | .setIndex("dbdemo")
81 | .setEsIdField("log_id")//设置文档主键,不设置,则自动产生文档id
82 | .setDebugResponse(false)//设置是否将每次处理的reponse打印到日志文件中,默认false
83 | .setDiscardBulkResponse(false);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false
84 | /**
85 | elasticsearchOutputConfig.setEsIdGenerator(new EsIdGenerator() {
86 | //如果指定EsIdGenerator,则根据下面的方法生成文档id,
87 | // 否则根据setEsIdField方法设置的字段值作为文档id,
88 | // 如果默认没有配置EsIdField和如果指定EsIdGenerator,则由es自动生成文档id
89 |
90 | @Override
91 | public Object genId(Context context) throws Exception {
92 | return SimpleStringUtil.getUUID();//返回null,则由es自动生成文档id
93 | }
94 | });
95 | */
96 | // .setIndexType("dbdemo") ;//es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType;
97 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
98 | /**
99 | * es相关配置
100 | */
101 | // elasticsearchOutputConfig.setTargetElasticsearch("default,test");//同步数据到两个es集群
102 |
103 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
104 | importBuilder
105 | //
106 | .setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
107 | .setUseLowcase(true) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
108 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
109 | .setBatchSize(10); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
110 |
111 | //定时任务配置,
112 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
113 | // .setScheduleDate(date) //指定任务开始执行时间:日期
114 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
115 | .setPeriod(5000L); //每隔period毫秒执行,如果不设置,只执行一次
116 | //定时任务配置结束
117 | //
118 | // //设置任务执行拦截器,可以添加多个,定时任务每次执行的拦截器
119 | importBuilder.addCallInterceptor(new CallInterceptor() {
120 | @Override
121 | public void preCall(TaskContext taskContext) {
122 | System.out.println("preCall");
123 | }
124 |
125 | @Override
126 | public void afterCall(TaskContext taskContext) {
127 | System.out.println("afterCall");
128 | }
129 |
130 | @Override
131 | public void throwException(TaskContext taskContext, Throwable e) {
132 | System.out.println("throwException");
133 | }
134 | });
135 | // //设置任务执行拦截器结束,可以添加多个
136 | //增量配置开始
137 | // importBuilder.setStatusDbname("test");//设置增量状态数据源名称
138 | importBuilder.setLastValueColumn("log_id");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段
139 | importBuilder.setFromFirst(true);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据,
140 | // setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据
141 | importBuilder.setStatusDbname("logtable");
142 | importBuilder.setLastValueStorePath("logtable_import");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样
143 | importBuilder.setLastValueStoreTableName("logstable");//记录上次采集的增量字段值的表,可以不指定,采用默认表名increament_tab
144 | importBuilder.setLastValueType(ImportIncreamentConfig.NUMBER_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型
145 | // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
146 | // try {
147 | // Date date = format.parse("2000-01-01");
148 | // importBuilder.setLastValue(date);//增量起始值配置
149 | // }
150 | // catch (Exception e){
151 | // e.printStackTrace();
152 | // }
153 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型
154 | //增量配置结束
155 |
156 | //映射和转换配置开始
157 | // /**
158 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
159 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
160 | // */
161 | // importBuilder.addFieldMapping("document_id","docId")
162 | // .addFieldMapping("docwtime","docwTime")
163 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
164 | //
165 | //
166 | // /**
167 | // * 为每条记录添加额外的字段和值
168 | // * 可以为基本数据类型,也可以是复杂的对象
169 | // */
170 | // importBuilder.addFieldValue("testF1","f1value");
171 | // importBuilder.addFieldValue("testInt",0);
172 | // importBuilder.addFieldValue("testDate",new Date());
173 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
174 | // TestObject testObject = new TestObject();
175 | // testObject.setId("testid");
176 | // testObject.setName("jackson");
177 | // importBuilder.addFieldValue("testObject",testObject);
178 | //
179 | /**
180 | * 重新设置es数据结构
181 | */
182 | importBuilder.setDataRefactor(new DataRefactor() {
183 | public void refactor(Context context) throws Exception {
184 | // Date date = context.getDateValue("LOG_OPERTIME");
185 | // Date date = context.getDateValue("LOG_OPERTIME");
186 | // context.addFieldValue("logOpertime",date);
187 | context.addFieldValue("collecttime",new Date());
188 | }
189 | });
190 | //映射和转换配置结束
191 |
192 | /**
193 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
194 | */
195 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行
196 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
197 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
198 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
199 |
200 | importBuilder.setExportResultHandler(new ExportResultHandler() {
201 | @Override
202 | public void success(TaskCommand taskCommand, String result) {
203 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
204 | logger.info(taskMetrics.toString());
205 | logger.debug(result);
206 | }
207 |
208 | @Override
209 | public void error(TaskCommand taskCommand, String result) {
210 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
211 | logger.info(taskMetrics.toString());
212 | logger.debug(result);
213 | }
214 |
215 | @Override
216 | public void exception(TaskCommand taskCommand, Throwable exception) {
217 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
218 | logger.debug(taskMetrics.toString());
219 | }
220 |
221 |
222 | });
223 | /**
224 | * 执行数据库表数据导入es操作
225 | */
226 | DataStream dataStream = importBuilder.builder();
227 | dataStream.execute();//执行导入操作
228 | // dataStream.destroy();//释放资源
229 |
230 |
231 | }
232 |
233 |
234 | }
235 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/DbdemoFromSQLFile.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.elasticsearch.ElasticSearchHelper;
19 | import org.frameworkset.tran.DataStream;
20 | import org.frameworkset.tran.config.ImportBuilder;
21 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
22 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
23 | import org.frameworkset.tran.schedule.ImportIncreamentConfig;
24 |
25 | /**
26 | *
Description: 同步处理程序,如需调试同步功能,直接运行main方法即可
27 | *
28 | * Copyright (c) 2018
29 | * @Date 2018/9/27 20:38
30 | * @author biaoping.yin
31 | * @version 1.0
32 | */
33 | public class DbdemoFromSQLFile {
34 | public static void main(String args[]){
35 | DbdemoFromSQLFile dbdemo = new DbdemoFromSQLFile();
36 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
37 | dbdemo.scheduleImportData( dropIndice);
38 | }
39 |
40 | /**
41 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
42 | */
43 | public void scheduleImportData(boolean dropIndice){
44 | ImportBuilder importBuilder = ImportBuilder.newInstance();
45 | //增量定时任务不要删表,但是可以通过删表来做初始化操作
46 | if(dropIndice) {
47 | try {
48 | //清除测试表,导入的时候回重建表,测试的时候加上为了看测试效果,实际线上环境不要删表
49 | ElasticSearchHelper.getRestClientUtil().dropIndice("dbdemo1");
50 | } catch (Exception e) {
51 | }
52 | }
53 |
54 |
55 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
56 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
57 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
58 | // 需要设置setLastValueColumn信息log_id,
59 | // 通过setLastValueType方法告诉工具增量字段的类型,默认是数字类型
60 |
61 | // importBuilder.setSql("select * from td_sm_log where log_id > #[log_id]");
62 | // importBuilder.setSql("select * from td_sm_log ");
63 | DBInputConfig dbInputConfig = new DBInputConfig();
64 | dbInputConfig.setSqlFilepath("sql.xml")
65 | .setSqlName("demoexportFull")
66 | .setJdbcFetchSize(-2147483648)
67 | .setColumnLableUpperCase(false);
68 | importBuilder.setInputConfig(dbInputConfig);
69 | /**
70 | * es相关配置
71 | */
72 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
73 | elasticsearchOutputConfig
74 | .setIndex("dbdemo1") //必填项
75 | .setEsIdField("log_id")//设置文档主键,不设置,则自动产生文档id
76 | .setDebugResponse(false)//设置是否将每次处理的reponse打印到日志文件中,默认false
77 | .setDiscardBulkResponse(true);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false
78 |
79 | // .setIndexType("dbdemo") //es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType
80 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
81 | importBuilder.setUseJavaName(false) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
82 | // .setUseLowcase(false) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
83 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
84 | .setBatchSize(5000); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
85 |
86 | //定时任务配置,
87 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
88 | // .setScheduleDate(date) //指定任务开始执行时间:日期
89 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
90 | .setPeriod(10000L); //每隔period毫秒执行,如果不设置,只执行一次
91 | //定时任务配置结束
92 | //
93 | // //设置任务执行拦截器,可以添加多个
94 | // importBuilder.addCallInterceptor(new CallInterceptor() {
95 | // @Override
96 | // public void preCall(TaskContext taskContext) {
97 | // System.out.println("preCall");
98 | // }
99 | //
100 | // @Override
101 | // public void afterCall(TaskContext taskContext) {
102 | // System.out.println("afterCall");
103 | // }
104 | //
105 | // @Override
106 | // public void throwException(TaskContext taskContext, Throwable e) {
107 | // System.out.println("throwException");
108 | // }
109 | // }).addCallInterceptor(new CallInterceptor() {
110 | // @Override
111 | // public void preCall(TaskContext taskContext) {
112 | // System.out.println("preCall 1");
113 | // }
114 | //
115 | // @Override
116 | // public void afterCall(TaskContext taskContext) {
117 | // System.out.println("afterCall 1");
118 | // }
119 | //
120 | // @Override
121 | // public void throwException(TaskContext taskContext, Throwable e) {
122 | // System.out.println("throwException 1");
123 | // }
124 | // });
125 | // //设置任务执行拦截器结束,可以添加多个
126 | //增量配置开始
127 | // importBuilder.setLastValueColumn("log_id");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段
128 | importBuilder.setFromFirst(true);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据,
129 | //setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据
130 | importBuilder.setLastValueStorePath("logtable_import1");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样
131 | // importBuilder.setLastValueStoreTableName("logs");//记录上次采集的增量字段值的表,可以不指定,采用默认表名increament_tab
132 | importBuilder.setLastValueType(ImportIncreamentConfig.NUMBER_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型
133 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型
134 | //增量配置结束
135 |
136 | //映射和转换配置开始
137 | // /**
138 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
139 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
140 | // */
141 | // importBuilder.addFieldMapping("document_id","docId")
142 | // .addFieldMapping("docwtime","docwTime")
143 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
144 | //
145 | //
146 | // /**
147 | // * 为每条记录添加额外的字段和值
148 | // * 可以为基本数据类型,也可以是复杂的对象
149 | // */
150 | // importBuilder.addFieldValue("testF1","f1value");
151 | // importBuilder.addFieldValue("testInt",0);
152 | // importBuilder.addFieldValue("testDate",new Date());
153 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
154 | // TestObject testObject = new TestObject();
155 | // testObject.setId("testid");
156 | // testObject.setName("jackson");
157 | // importBuilder.addFieldValue("testObject",testObject);
158 | //
159 | // /**
160 | // * 重新设置es数据结构
161 | // */
162 | // importBuilder.setDataRefactor(new DataRefactor() {
163 | // public void refactor(Context context) throws Exception {
164 | // CustomObject customObject = new CustomObject();
165 | // customObject.setAuthor((String)context.getValue("author"));
166 | // customObject.setTitle((String)context.getValue("title"));
167 | // customObject.setSubtitle((String)context.getValue("subtitle"));
168 | // customObject.setIds(new int[]{1,2,3});
169 | // context.addFieldValue("docInfo",customObject);//如果还需要构建更多的内部对象,可以继续构建
170 | //
171 | // //上述三个属性已经放置到docInfo中,如果无需再放置到索引文档中,可以忽略掉这些属性
172 | // context.addIgnoreFieldMapping("author");
173 | // context.addIgnoreFieldMapping("title");
174 | // context.addIgnoreFieldMapping("subtitle");
175 | // }
176 | // });
177 | //映射和转换配置结束
178 |
179 | /**
180 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
181 | */
182 | importBuilder.setParallel(false);//设置为多线程并行批量导入,false串行
183 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
184 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
185 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
186 |
187 | /**
188 | * 执行数据库表数据导入es操作
189 | */
190 | DataStream dataStream = importBuilder.builder();
191 | dataStream.execute();//执行导入操作
192 |
193 |
194 |
195 | }
196 | }
197 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/DbdemoFull.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.tran.DataRefactor;
19 | import org.frameworkset.tran.DataStream;
20 | import org.frameworkset.tran.ExportResultHandler;
21 | import org.frameworkset.tran.config.ImportBuilder;
22 | import org.frameworkset.tran.context.Context;
23 | import org.frameworkset.tran.metrics.TaskMetrics;
24 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
25 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
26 | import org.frameworkset.tran.task.TaskCommand;
27 | import org.slf4j.Logger;
28 | import org.slf4j.LoggerFactory;
29 |
30 | import java.util.Date;
31 |
32 | /**
33 | *
Description: 基于数字类型db-es增量同步案例,同步处理程序,如需调试同步功能,直接运行main方法即可
34 | *
35 | * Copyright (c) 2018
36 | * @Date 2018/9/27 20:38
37 | * @author biaoping.yin
38 | * @version 1.0
39 | */
40 | public class DbdemoFull {
41 | private static Logger logger = LoggerFactory.getLogger(DbdemoFull.class);
42 | public static void main(String args[]){
43 | DbdemoFull dbdemo = new DbdemoFull();
44 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
45 | // dbdemo.fullImportData( dropIndice);
46 | // dbdemo.scheduleImportData(dropIndice);
47 | dbdemo.scheduleTimestampImportData(dropIndice);
48 | // dbdemo.scheduleImportData(dropIndice);
49 | // args[1].charAt(0) == args[2].charAt(0);
50 | }
51 |
52 | /**
53 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
54 | */
55 | public void scheduleTimestampImportData(boolean dropIndice){
56 | ImportBuilder importBuilder = new ImportBuilder() ;
57 | DBInputConfig dbInputConfig = new DBInputConfig();
58 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
59 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
60 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
61 | // 需要设置setLastValueColumn信息log_id,
62 | // 通过setLastValueType方法告诉工具增量字段的类型,默认是数字类型
63 |
64 | // importBuilder.setSql("select * from td_sm_log where LOG_OPERTIME > #[LOG_OPERTIME]");
65 | dbInputConfig.setSql("select * from td_sm_log")
66 | .setDbName("test");
67 | importBuilder.setInputConfig(dbInputConfig);
68 |
69 |
70 | // importBuilder.addFieldMapping("LOG_CONTENT","message");
71 | // importBuilder.addIgnoreFieldMapping("remark1");
72 | // importBuilder.setSql("select * from td_sm_log ");
73 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
74 | elasticsearchOutputConfig.setTargetElasticsearch("default")
75 | .setIndex("dbdemofull")
76 |
77 | .setDebugResponse(false)//设置是否将每次处理的reponse打印到日志文件中,默认false
78 | .setDiscardBulkResponse(false);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false
79 | /**
80 | elasticsearchOutputConfig.setEsIdGenerator(new EsIdGenerator() {
81 | //如果指定EsIdGenerator,则根据下面的方法生成文档id,
82 | // 否则根据setEsIdField方法设置的字段值作为文档id,
83 | // 如果默认没有配置EsIdField和如果指定EsIdGenerator,则由es自动生成文档id
84 |
85 | @Override
86 | public Object genId(Context context) throws Exception {
87 | return SimpleStringUtil.getUUID();//返回null,则由es自动生成文档id
88 | }
89 | });
90 | */
91 | // .setIndexType("dbdemo") ;//es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType;
92 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
93 | /**
94 | * es相关配置
95 | */
96 | // elasticsearchOutputConfig.setTargetElasticsearch("default,test");//同步数据到两个es集群
97 |
98 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
99 | importBuilder
100 | //
101 | .setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
102 | .setUseLowcase(true) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
103 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
104 | .setBatchSize(10); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
105 |
106 | // //设置任务执行拦截器结束,可以添加多个
107 |
108 |
109 | //映射和转换配置开始
110 | // /**
111 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
112 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
113 | // */
114 | // importBuilder.addFieldMapping("document_id","docId")
115 | // .addFieldMapping("docwtime","docwTime")
116 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
117 | //
118 | //
119 | // /**
120 | // * 为每条记录添加额外的字段和值
121 | // * 可以为基本数据类型,也可以是复杂的对象
122 | // */
123 | // importBuilder.addFieldValue("testF1","f1value");
124 | // importBuilder.addFieldValue("testInt",0);
125 | // importBuilder.addFieldValue("testDate",new Date());
126 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
127 | // TestObject testObject = new TestObject();
128 | // testObject.setId("testid");
129 | // testObject.setName("jackson");
130 | // importBuilder.addFieldValue("testObject",testObject);
131 | //
132 | /**
133 | * 重新设置es数据结构
134 | */
135 | importBuilder.setDataRefactor(new DataRefactor() {
136 | public void refactor(Context context) throws Exception {
137 | // Date date = context.getDateValue("LOG_OPERTIME");
138 | context.addFieldValue("collecttime",new Date());
139 | }
140 | });
141 | //映射和转换配置结束
142 |
143 | /**
144 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
145 | */
146 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行
147 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
148 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
149 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
150 |
151 | importBuilder.setExportResultHandler(new ExportResultHandler() {
152 | @Override
153 | public void success(TaskCommand taskCommand, String result) {
154 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
155 | logger.info(taskMetrics.toString());
156 | logger.debug(result);
157 | }
158 |
159 | @Override
160 | public void error(TaskCommand taskCommand, String result) {
161 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
162 | logger.info(taskMetrics.toString());
163 | logger.debug(result);
164 | }
165 |
166 | @Override
167 | public void exception(TaskCommand taskCommand, Throwable exception) {
168 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
169 | logger.debug(taskMetrics.toString());
170 | }
171 |
172 |
173 | });
174 | /**
175 | * 执行数据库表数据导入es操作
176 | */
177 | DataStream dataStream = importBuilder.builder();
178 | dataStream.execute();//执行导入操作
179 | // dataStream.destroy();//释放资源
180 |
181 |
182 | }
183 |
184 |
185 | }
186 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/DruidAdaptor.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.frameworkset.orm.adapter.DBMM;
19 |
20 | /**
21 | *
Description: 替换mysql的adaptor为自定义的adaptor演示
22 | *
23 | * Copyright (c) 2018
24 | * @Date 2019/4/21 13:28
25 | * @author biaoping.yin
26 | * @version 1.0
27 | */
28 | public class DruidAdaptor extends DBMM {
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/NewDbdemoFromSQLFile.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.elasticsearch.ElasticSearchHelper;
19 | import org.frameworkset.elasticsearch.bulk.BulkConfig;
20 | import org.frameworkset.tran.DataStream;
21 | import org.frameworkset.tran.config.ClientOptions;
22 | import org.frameworkset.tran.config.ImportBuilder;
23 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
24 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
25 | import org.frameworkset.tran.schedule.ImportIncreamentConfig;
26 |
27 | /**
28 | *
Description: 同步处理程序,如需调试同步功能,直接运行main方法即可
29 | *
30 | * Copyright (c) 2018
31 | * @Date 2018/9/27 20:38
32 | * @author biaoping.yin
33 | * @version 1.0
34 | */
35 | public class NewDbdemoFromSQLFile {
36 | public static void main(String args[]){
37 | NewDbdemoFromSQLFile dbdemo = new NewDbdemoFromSQLFile();
38 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
39 | dbdemo.scheduleImportData( dropIndice);
40 | }
41 |
42 | /**
43 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
44 | */
45 | public void scheduleImportData(boolean dropIndice){
46 | ImportBuilder importBuilder = new ImportBuilder() ;
47 | //增量定时任务不要删表,但是可以通过删表来做初始化操作
48 | if(dropIndice) {
49 | try {
50 | //清除测试表,导入的时候回重建表,测试的时候加上为了看测试效果,实际线上环境不要删表
51 | ElasticSearchHelper.getRestClientUtil().dropIndice("dbdemo1");
52 | } catch (Exception e) {
53 | }
54 | }
55 |
56 |
57 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
58 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
59 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
60 | // log_id和数据库对应的字段一致,就不需要设置setLastValueColumn信息,
61 | // 但是需要设置setLastValueType告诉工具增量字段的类型
62 | DBInputConfig dbInputConfig = new DBInputConfig();
63 | // importBuilder.setSql("select * from td_sm_log where log_id > #[log_id]");
64 | // importBuilder.setSql("select * from td_sm_log ");
65 | dbInputConfig.setSqlFilepath("sql.xml")
66 | .setSqlName("demoexportFull");
67 | dbInputConfig.setJdbcFetchSize(-2147483648);
68 | dbInputConfig.setColumnLableUpperCase(false);
69 | importBuilder.setInputConfig(dbInputConfig);
70 |
71 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
72 | elasticsearchOutputConfig.setTargetElasticsearch("default").setIndex("dbdemo1");
73 | ClientOptions clientOptions = new ClientOptions();
74 | // clientOptions.setPipeline("1");
75 | clientOptions.setRefresh("true");
76 | // routing
77 | // (Optional, string) Target the specified primary shard.
78 | clientOptions.setRouting("2");
79 | clientOptions.setTimeout("50s");
80 | clientOptions.setWaitForActiveShards(2);
81 | elasticsearchOutputConfig.setClientOptions(clientOptions);
82 | elasticsearchOutputConfig.setEsIdField("log_id");//设置文档主键,不设置,则自动产生文档id
83 |
84 | elasticsearchOutputConfig.setDebugResponse(false);//设置是否将每次处理的reponse打印到日志文件中,默认false
85 | elasticsearchOutputConfig.setDiscardBulkResponse(true);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false
86 | elasticsearchOutputConfig.setFilterPath(BulkConfig.ERROR_FILTER_PATH_ONLY);//指定响应报文只返回错误信息
87 |
88 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
89 |
90 | /**
91 | * es相关配置
92 | */
93 | importBuilder
94 | // .setIndex("dbdemo1") //必填项
95 | // .setIndexType("dbdemo") //es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType
96 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
97 | .setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
98 | // .setUseLowcase(false) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
99 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
100 | .setBatchSize(5000); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
101 |
102 | //定时任务配置,
103 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
104 | // .setScheduleDate(date) //指定任务开始执行时间:日期
105 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
106 | .setPeriod(10000L); //每隔period毫秒执行,如果不设置,只执行一次
107 | //定时任务配置结束
108 | //
109 | // //设置任务执行拦截器,可以添加多个
110 | // importBuilder.addCallInterceptor(new CallInterceptor() {
111 | // @Override
112 | // public void preCall(TaskContext taskContext) {
113 | // System.out.println("preCall");
114 | // }
115 | //
116 | // @Override
117 | // public void afterCall(TaskContext taskContext) {
118 | // System.out.println("afterCall");
119 | // }
120 | //
121 | // @Override
122 | // public void throwException(TaskContext taskContext, Throwable e) {
123 | // System.out.println("throwException");
124 | // }
125 | // }).addCallInterceptor(new CallInterceptor() {
126 | // @Override
127 | // public void preCall(TaskContext taskContext) {
128 | // System.out.println("preCall 1");
129 | // }
130 | //
131 | // @Override
132 | // public void afterCall(TaskContext taskContext) {
133 | // System.out.println("afterCall 1");
134 | // }
135 | //
136 | // @Override
137 | // public void throwException(TaskContext taskContext, Throwable e) {
138 | // System.out.println("throwException 1");
139 | // }
140 | // });
141 | // //设置任务执行拦截器结束,可以添加多个
142 | //增量配置开始
143 | // importBuilder.setLastValueColumn("log_id");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段
144 | importBuilder.setFromFirst(true);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据,
145 | //setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据
146 | importBuilder.setLastValueStorePath("logtable_import1");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样
147 | // importBuilder.setLastValueStoreTableName("logs");//记录上次采集的增量字段值的表,可以不指定,采用默认表名increament_tab
148 | importBuilder.setLastValueType(ImportIncreamentConfig.NUMBER_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型
149 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型
150 | //增量配置结束
151 |
152 | //映射和转换配置开始
153 | // /**
154 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
155 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
156 | // */
157 | // importBuilder.addFieldMapping("document_id","docId")
158 | // .addFieldMapping("docwtime","docwTime")
159 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
160 | //
161 | //
162 | // /**
163 | // * 为每条记录添加额外的字段和值
164 | // * 可以为基本数据类型,也可以是复杂的对象
165 | // */
166 | // importBuilder.addFieldValue("testF1","f1value");
167 | // importBuilder.addFieldValue("testInt",0);
168 | // importBuilder.addFieldValue("testDate",new Date());
169 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
170 | // TestObject testObject = new TestObject();
171 | // testObject.setId("testid");
172 | // testObject.setName("jackson");
173 | // importBuilder.addFieldValue("testObject",testObject);
174 | //
175 | // /**
176 | // * 重新设置es数据结构
177 | // */
178 | // importBuilder.setDataRefactor(new DataRefactor() {
179 | // public void refactor(Context context) throws Exception {
180 | // CustomObject customObject = new CustomObject();
181 | // customObject.setAuthor((String)context.getValue("author"));
182 | // customObject.setTitle((String)context.getValue("title"));
183 | // customObject.setSubtitle((String)context.getValue("subtitle"));
184 | // customObject.setIds(new int[]{1,2,3});
185 | // context.addFieldValue("docInfo",customObject);//如果还需要构建更多的内部对象,可以继续构建
186 | //
187 | // //上述三个属性已经放置到docInfo中,如果无需再放置到索引文档中,可以忽略掉这些属性
188 | // context.addIgnoreFieldMapping("author");
189 | // context.addIgnoreFieldMapping("title");
190 | // context.addIgnoreFieldMapping("subtitle");
191 | // }
192 | // });
193 | //映射和转换配置结束
194 |
195 | /**
196 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
197 | */
198 | importBuilder.setParallel(false);//设置为多线程并行批量导入,false串行
199 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
200 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
201 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
202 |
203 | /**
204 | * 执行数据库表数据导入es操作
205 | */
206 | DataStream dataStream = importBuilder.builder();
207 | dataStream.execute();//执行导入操作
208 |
209 |
210 |
211 | }
212 | }
213 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/PostgresDbdemo.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.elasticsearch.ElasticSearchHelper;
19 | import org.frameworkset.tran.DataRefactor;
20 | import org.frameworkset.tran.DataStream;
21 | import org.frameworkset.tran.ExportResultHandler;
22 | import org.frameworkset.tran.config.ImportBuilder;
23 | import org.frameworkset.tran.context.Context;
24 | import org.frameworkset.tran.metrics.TaskMetrics;
25 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
26 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
27 | import org.frameworkset.tran.schedule.CallInterceptor;
28 | import org.frameworkset.tran.schedule.TaskContext;
29 | import org.frameworkset.tran.schedule.timer.TimeUtil;
30 | import org.frameworkset.tran.task.TaskCommand;
31 | import org.slf4j.Logger;
32 | import org.slf4j.LoggerFactory;
33 |
34 | /**
35 | *
Description: 同步处理程序,如需调试同步功能,直接运行main方法即可,定时按特定条件导入数据
36 | *
37 | * Copyright (c) 2018
38 | * @Date 2018/9/27 20:38
39 | * @author biaoping.yin
40 | * @version 1.0
41 | */
42 | public class PostgresDbdemo {
43 | private static Logger logger = LoggerFactory.getLogger(PostgresDbdemo.class);
44 | public static void main(String args[]){
45 | PostgresDbdemo dbdemo = new PostgresDbdemo();
46 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
47 | // dbdemo.fullImportData( dropIndice);
48 | // dbdemo.scheduleImportData(dropIndice);
49 | dbdemo.scheduleTimestampImportData(dropIndice);
50 | // dbdemo.scheduleImportData(dropIndice);
51 | // args[1].charAt(0) == args[2].charAt(0);
52 | }
53 |
54 | /**
55 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
56 | */
57 | public void scheduleTimestampImportData(boolean dropIndice){
58 | ImportBuilder importBuilder = new ImportBuilder() ;
59 | //增量定时任务不要删表,但是可以通过删表来做初始化操作
60 | if(dropIndice) {
61 | try {
62 | //清除测试表,导入的时候回重建表,测试的时候加上为了看测试效果,实际线上环境不要删表
63 | String repsonse = ElasticSearchHelper.getRestClientUtil().dropIndice("postgresdbdemo");
64 | System.out.println(repsonse);
65 | } catch (Exception e) {
66 | }
67 | }
68 |
69 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
70 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
71 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
72 | // log_id和数据库对应的字段一致,就不需要设置setLastValueColumn信息,
73 | // 但是需要设置setLastValueType告诉工具增量字段的类型
74 | DBInputConfig dbInputConfig = new DBInputConfig();
75 | dbInputConfig.setDbName("postgres");
76 | dbInputConfig.setJdbcFetchSize(2000);
77 | // importBuilder.setSql("select * from td_sm_log where LOG_OPERTIME > #[LOG_OPERTIME]");
78 | dbInputConfig.setSql("select * from batchtest1 where optime >= #[start_optime] and optime < #[end_optime]");
79 | importBuilder.setInputConfig(dbInputConfig);
80 |
81 | importBuilder.addParam("start_optime", TimeUtil.parserDate("yyyy-MM-dd HH:mm:ss","2018-03-21 00:27:21"))
82 | .addParam("end_optime",TimeUtil.parserDate("yyyy-MM-dd HH:mm:ss","2019-12-30 00:27:21"));
83 | // importBuilder.addFieldMapping("LOG_CONTENT","message");
84 | // importBuilder.addIgnoreFieldMapping("remark1");
85 | // importBuilder.setSql("select * from td_sm_log ");
86 | /**
87 | * es相关配置
88 | */
89 | // importBuilder.setTargetElasticsearch("default,test");//同步数据到两个es集群
90 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
91 | elasticsearchOutputConfig.setTargetElasticsearch("test");
92 | elasticsearchOutputConfig.setIndex("postgresdbdemo"); //必填项
93 | // .setIndexType("dbdemo") //es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType
94 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
95 |
96 | //elasticsearchOutputConfig.setEsIdField("log_id");//设置文档主键,不设置,则自动产生文档id
97 |
98 | elasticsearchOutputConfig.setDebugResponse(false);//设置是否将每次处理的reponse打印到日志文件中,默认false
99 | elasticsearchOutputConfig.setDiscardBulkResponse(false);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false
100 | /**
101 | elasticsearchOutputConfig.setEsIdGenerator(new EsIdGenerator() {
102 | //如果指定EsIdGenerator,则根据下面的方法生成文档id,
103 | // 否则根据setEsIdField方法设置的字段值作为文档id,
104 | // 如果默认没有配置EsIdField和如果指定EsIdGenerator,则由es自动生成文档id
105 |
106 | @Override
107 | public Object genId(Context context) throws Exception {
108 | return SimpleStringUtil.getUUID();//返回null,则由es自动生成文档id
109 | }
110 | });
111 | */
112 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
113 | importBuilder.setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
114 | .setUseLowcase(true) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
115 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
116 | .setBatchSize(10); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
117 |
118 | //定时任务配置,
119 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
120 | // .setScheduleDate(date) //指定任务开始执行时间:日期
121 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
122 | .setPeriod(5000L); //每隔period毫秒执行,如果不设置,只执行一次
123 | //定时任务配置结束
124 | //
125 | // //设置任务执行拦截器,可以添加多个,定时任务每次执行的拦截器
126 | importBuilder.addCallInterceptor(new CallInterceptor() {
127 | @Override
128 | public void preCall(TaskContext taskContext) {
129 | System.out.println("preCall");
130 | }
131 |
132 | @Override
133 | public void afterCall(TaskContext taskContext) {
134 | System.out.println("afterCall");
135 | }
136 |
137 | @Override
138 | public void throwException(TaskContext taskContext, Throwable e) {
139 | System.out.println("throwException");
140 | }
141 | }).addCallInterceptor(new CallInterceptor() {
142 | @Override
143 | public void preCall(TaskContext taskContext) {
144 | System.out.println("preCall 1");
145 | }
146 |
147 | @Override
148 | public void afterCall(TaskContext taskContext) {
149 | logger.info("afterCall ----------"+taskContext.getJobTaskMetrics().toString());
150 | }
151 |
152 | @Override
153 | public void throwException(TaskContext taskContext, Throwable e) {
154 | logger.info("afterCall ----------"+taskContext.getJobTaskMetrics().toString(),e);
155 | }
156 | });
157 | // //设置任务执行拦截器结束,可以添加多个
158 | //增量配置开始
159 |
160 | //映射和转换配置开始
161 | // /**
162 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
163 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
164 | // */
165 | // importBuilder.addFieldMapping("document_id","docId")
166 | // .addFieldMapping("docwtime","docwTime")
167 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
168 | //
169 | //
170 | // /**
171 | // * 为每条记录添加额外的字段和值
172 | // * 可以为基本数据类型,也可以是复杂的对象
173 | // */
174 | // importBuilder.addFieldValue("testF1","f1value");
175 | // importBuilder.addFieldValue("testInt",0);
176 | // importBuilder.addFieldValue("testDate",new Date());
177 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
178 | // TestObject testObject = new TestObject();
179 | // testObject.setId("testid");
180 | // testObject.setName("jackson");
181 | // importBuilder.addFieldValue("testObject",testObject);
182 | //
183 | /**
184 | * 重新设置es数据结构
185 | */
186 | importBuilder.setDataRefactor(new DataRefactor() {
187 | public void refactor(Context context) throws Exception {
188 | // Date date = context.getDateValue("LOG_OPERTIME");
189 | // context.addFieldValue("collecttime",new Date());
190 | logger.info("refactor");
191 | }
192 | });
193 | //映射和转换配置结束
194 |
195 | /**
196 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
197 | */
198 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行
199 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
200 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
201 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
202 |
203 | importBuilder.setExportResultHandler(new ExportResultHandler() {
204 | @Override
205 | public void success(TaskCommand taskCommand, String result) {
206 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
207 | logger.info(taskMetrics.toString());
208 | logger.debug(result);
209 | }
210 |
211 | @Override
212 | public void error(TaskCommand taskCommand, String result) {
213 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
214 | logger.info(taskMetrics.toString());
215 | logger.debug(result);
216 | }
217 |
218 | @Override
219 | public void exception(TaskCommand taskCommand, Throwable exception) {
220 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
221 | logger.debug(taskMetrics.toString());
222 | }
223 |
224 |
225 | });
226 | /**
227 | * 执行数据库表数据导入es操作
228 | */
229 | DataStream dataStream = importBuilder.builder();
230 | dataStream.execute();//执行导入操作
231 | // dataStream.destroy();//释放资源
232 |
233 |
234 | }
235 |
236 |
237 | }
238 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/PostgresDefaultDbdemo.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.elasticsearch.ElasticSearchHelper;
19 | import org.frameworkset.tran.DataRefactor;
20 | import org.frameworkset.tran.DataStream;
21 | import org.frameworkset.tran.ExportResultHandler;
22 | import org.frameworkset.tran.config.ImportBuilder;
23 | import org.frameworkset.tran.context.Context;
24 | import org.frameworkset.tran.metrics.TaskMetrics;
25 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
26 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
27 | import org.frameworkset.tran.schedule.CallInterceptor;
28 | import org.frameworkset.tran.schedule.TaskContext;
29 | import org.frameworkset.tran.task.TaskCommand;
30 | import org.slf4j.Logger;
31 | import org.slf4j.LoggerFactory;
32 |
33 | /**
34 | *
Description: 同步处理程序,如需调试同步功能,直接运行main方法即可,定时全量表数据导入
35 | *
36 | * Copyright (c) 2018
37 | * @Date 2018/9/27 20:38
38 | * @author biaoping.yin
39 | * @version 1.0
40 | */
41 | public class PostgresDefaultDbdemo {
42 | private static Logger logger = LoggerFactory.getLogger(PostgresDefaultDbdemo.class);
43 | public static void main(String args[]){
44 | PostgresDefaultDbdemo dbdemo = new PostgresDefaultDbdemo();
45 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
46 | // dbdemo.fullImportData( dropIndice);
47 | // dbdemo.scheduleImportData(dropIndice);
48 | dbdemo.scheduleTimestampImportData(dropIndice);
49 | // dbdemo.scheduleImportData(dropIndice);
50 | // args[1].charAt(0) == args[2].charAt(0);
51 | }
52 |
53 | /**
54 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
55 | */
56 | public void scheduleTimestampImportData(boolean dropIndice){
57 | ImportBuilder importBuilder = new ImportBuilder() ;
58 | //增量定时任务不要删表,但是可以通过删表来做初始化操作
59 | if(dropIndice) {
60 | try {
61 | //清除测试表,导入的时候回重建表,测试的时候加上为了看测试效果,实际线上环境不要删表
62 | String repsonse = ElasticSearchHelper.getRestClientUtil().dropIndice("postgresdbdemo");
63 | System.out.println(repsonse);
64 | } catch (Exception e) {
65 | }
66 | }
67 |
68 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
69 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
70 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
71 | // log_id和数据库对应的字段一致,就不需要设置setLastValueColumn信息,
72 | // 但是需要设置setLastValueType告诉工具增量字段的类型
73 | DBInputConfig dbInputConfig = new DBInputConfig();
74 | dbInputConfig.setDbName("postgres");
75 | // importBuilder.setSql("select * from td_sm_log where LOG_OPERTIME > #[LOG_OPERTIME]");
76 | dbInputConfig.setSql("select * from batchtest1 ");
77 |
78 | importBuilder.setInputConfig(dbInputConfig);
79 | // importBuilder.addFieldMapping("LOG_CONTENT","message");
80 | // importBuilder.addIgnoreFieldMapping("remark1");
81 | /**
82 | * es相关配置
83 | */
84 | // ElasticsearchOutputConfig.setTargetElasticsearch("default,test");//同步数据到两个es集群
85 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
86 | elasticsearchOutputConfig.setTargetElasticsearch("test");
87 | elasticsearchOutputConfig.setIndex("postgresdbdemo") ;//必填项
88 | // .setIndexType("dbdemo") //es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType
89 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
90 | //elasticsearchOutputConfig.setEsIdField("log_id");//设置文档主键,不设置,则自动产生文档id
91 |
92 | elasticsearchOutputConfig.setDebugResponse(false);//设置是否将每次处理的reponse打印到日志文件中,默认false
93 | elasticsearchOutputConfig.setDiscardBulkResponse(false);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false
94 | /**
95 | elasticsearchOutputConfig.setEsIdGenerator(new EsIdGenerator() {
96 | //如果指定EsIdGenerator,则根据下面的方法生成文档id,
97 | // 否则根据setEsIdField方法设置的字段值作为文档id,
98 | // 如果默认没有配置EsIdField和如果指定EsIdGenerator,则由es自动生成文档id
99 |
100 | @Override
101 | public Object genId(Context context) throws Exception {
102 | return SimpleStringUtil.getUUID();//返回null,则由es自动生成文档id
103 | }
104 | });
105 | */
106 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
107 | importBuilder.setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
108 | .setUseLowcase(true) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
109 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
110 | .setBatchSize(10); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
111 |
112 | //定时任务配置,
113 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
114 | // .setScheduleDate(date) //指定任务开始执行时间:日期
115 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
116 | .setPeriod(5000L); //每隔period毫秒执行,如果不设置,只执行一次
117 | //定时任务配置结束
118 | //
119 | // //设置任务执行拦截器,可以添加多个,定时任务每次执行的拦截器
120 | importBuilder.addCallInterceptor(new CallInterceptor() {
121 | @Override
122 | public void preCall(TaskContext taskContext) {
123 | System.out.println("preCall");
124 | }
125 |
126 | @Override
127 | public void afterCall(TaskContext taskContext) {
128 | System.out.println("afterCall");
129 | }
130 |
131 | @Override
132 | public void throwException(TaskContext taskContext, Throwable e) {
133 | System.out.println("throwException");
134 | }
135 | }).addCallInterceptor(new CallInterceptor() {
136 | @Override
137 | public void preCall(TaskContext taskContext) {
138 | System.out.println("preCall 1");
139 | }
140 |
141 | @Override
142 | public void afterCall(TaskContext taskContext) {
143 | logger.info("afterCall ----------"+taskContext.getJobTaskMetrics().toString());
144 | }
145 |
146 | @Override
147 | public void throwException(TaskContext taskContext, Throwable e) {
148 | logger.info("afterCall ----------"+taskContext.getJobTaskMetrics().toString(),e);
149 | }
150 | });
151 | // //设置任务执行拦截器结束,可以添加多个
152 | //增量配置开始
153 |
154 | //映射和转换配置开始
155 | // /**
156 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
157 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
158 | // */
159 | // importBuilder.addFieldMapping("document_id","docId")
160 | // .addFieldMapping("docwtime","docwTime")
161 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
162 | //
163 | //
164 | // /**
165 | // * 为每条记录添加额外的字段和值
166 | // * 可以为基本数据类型,也可以是复杂的对象
167 | // */
168 | // importBuilder.addFieldValue("testF1","f1value");
169 | // importBuilder.addFieldValue("testInt",0);
170 | // importBuilder.addFieldValue("testDate",new Date());
171 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
172 | // TestObject testObject = new TestObject();
173 | // testObject.setId("testid");
174 | // testObject.setName("jackson");
175 | // importBuilder.addFieldValue("testObject",testObject);
176 | //
177 | /**
178 | * 重新设置es数据结构
179 | */
180 | importBuilder.setDataRefactor(new DataRefactor() {
181 | public void refactor(Context context) throws Exception {
182 | // Date date = context.getDateValue("LOG_OPERTIME");
183 | // context.addFieldValue("collecttime",new Date());
184 | logger.info("refactor");
185 | // Date date = context.getDateValue("aaaa","yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
186 | }
187 | });
188 | //映射和转换配置结束
189 |
190 | /**
191 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
192 | */
193 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行
194 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
195 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
196 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
197 |
198 | importBuilder.setExportResultHandler(new ExportResultHandler() {
199 | @Override
200 | public void success(TaskCommand taskCommand, String result) {
201 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
202 | logger.info(taskMetrics.toString());
203 | logger.debug(result);
204 | }
205 |
206 | @Override
207 | public void error(TaskCommand taskCommand, String result) {
208 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
209 | logger.info(taskMetrics.toString());
210 | logger.debug(result);
211 | }
212 |
213 | @Override
214 | public void exception(TaskCommand taskCommand, Throwable exception) {
215 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
216 | logger.debug(taskMetrics.toString());
217 | }
218 |
219 | });
220 | /**
221 | * 执行数据库表数据导入es操作
222 | */
223 | DataStream dataStream = importBuilder.builder();
224 | dataStream.execute();//执行导入操作
225 | // dataStream.destroy();//释放资源
226 |
227 |
228 | }
229 |
230 |
231 | }
232 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/PostgresIncreaseDbdemo.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.elasticsearch.ElasticSearchHelper;
19 | import org.frameworkset.tran.DataRefactor;
20 | import org.frameworkset.tran.DataStream;
21 | import org.frameworkset.tran.ExportResultHandler;
22 | import org.frameworkset.tran.config.ImportBuilder;
23 | import org.frameworkset.tran.context.Context;
24 | import org.frameworkset.tran.metrics.TaskMetrics;
25 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
26 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
27 | import org.frameworkset.tran.schedule.CallInterceptor;
28 | import org.frameworkset.tran.schedule.ImportIncreamentConfig;
29 | import org.frameworkset.tran.schedule.TaskContext;
30 | import org.frameworkset.tran.task.TaskCommand;
31 | import org.slf4j.Logger;
32 | import org.slf4j.LoggerFactory;
33 |
34 | /**
35 | *
Description: 同步处理程序,如需调试同步功能,直接运行main方法即可,定时按特定条件导入数据
36 | *
37 | * Copyright (c) 2018
38 | * @Date 2018/9/27 20:38
39 | * @author biaoping.yin
40 | * @version 1.0
41 | */
42 | public class PostgresIncreaseDbdemo {
43 | private static Logger logger = LoggerFactory.getLogger(PostgresIncreaseDbdemo.class);
44 | public static void main(String args[]){
45 | PostgresIncreaseDbdemo dbdemo = new PostgresIncreaseDbdemo();
46 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值
47 | // dbdemo.fullImportData( dropIndice);
48 | // dbdemo.scheduleImportData(dropIndice);
49 | dbdemo.scheduleTimestampImportData(dropIndice);
50 | // dbdemo.scheduleImportData(dropIndice);
51 | // args[1].charAt(0) == args[2].charAt(0);
52 | }
53 |
54 | /**
55 | * elasticsearch地址和数据库地址都从外部配置文件application.properties中获取,加载数据源配置和es配置
56 | */
57 | public void scheduleTimestampImportData(boolean dropIndice){
58 | ImportBuilder importBuilder = new ImportBuilder() ;
59 | //增量定时任务不要删表,但是可以通过删表来做初始化操作
60 | if(dropIndice) {
61 | try {
62 | //清除测试表,导入的时候回重建表,测试的时候加上为了看测试效果,实际线上环境不要删表
63 | String repsonse = ElasticSearchHelper.getRestClientUtil().dropIndice("postgresdbdemo");
64 | System.out.println(repsonse);
65 | } catch (Exception e) {
66 | }
67 | }
68 |
69 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
70 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
71 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
72 | // log_id和数据库对应的字段一致,就不需要设置setLastValueColumn信息,
73 | // 但是需要设置setLastValueType告诉工具增量字段的类型
74 | DBInputConfig dbInputConfig = new DBInputConfig();
75 | dbInputConfig.setDbName("postgres");
76 | dbInputConfig.setJdbcFetchSize(2000);
77 | // importBuilder.setSql("select * from td_sm_log where LOG_OPERTIME > #[LOG_OPERTIME]");
78 | dbInputConfig.setSql("select * from batchtest1 where optime > #[optime]");
79 | importBuilder.setInputConfig(dbInputConfig);
80 |
81 |
82 | // importBuilder.addFieldMapping("LOG_CONTENT","message");
83 | // importBuilder.addIgnoreFieldMapping("remark1");
84 | // importBuilder.setSql("select * from td_sm_log ");
85 | /**
86 | * es相关配置
87 | */
88 | // importBuilder.setTargetElasticsearch("default,test");//同步数据到两个es集群
89 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
90 | elasticsearchOutputConfig.setTargetElasticsearch("test");
91 | elasticsearchOutputConfig.setIndex("postgresdbdemo"); //必填项
92 | // .setIndexType("dbdemo") //es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType
93 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
94 |
95 | //elasticsearchOutputConfig.setEsIdField("log_id");//设置文档主键,不设置,则自动产生文档id
96 |
97 | elasticsearchOutputConfig.setDebugResponse(false);//设置是否将每次处理的reponse打印到日志文件中,默认false
98 | elasticsearchOutputConfig.setDiscardBulkResponse(false);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false
99 | /**
100 | elasticsearchOutputConfig.setEsIdGenerator(new EsIdGenerator() {
101 | //如果指定EsIdGenerator,则根据下面的方法生成文档id,
102 | // 否则根据setEsIdField方法设置的字段值作为文档id,
103 | // 如果默认没有配置EsIdField和如果指定EsIdGenerator,则由es自动生成文档id
104 |
105 | @Override
106 | public Object genId(Context context) throws Exception {
107 | return SimpleStringUtil.getUUID();//返回null,则由es自动生成文档id
108 | }
109 | });
110 | */
111 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
112 | importBuilder.setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
113 | .setUseLowcase(true) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
114 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
115 | .setBatchSize(10); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
116 |
117 | //定时任务配置,
118 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
119 | // .setScheduleDate(date) //指定任务开始执行时间:日期
120 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
121 | .setPeriod(5000L); //每隔period毫秒执行,如果不设置,只执行一次
122 | //定时任务配置结束
123 | //
124 | // //设置任务执行拦截器,可以添加多个,定时任务每次执行的拦截器
125 | importBuilder.addCallInterceptor(new CallInterceptor() {
126 | @Override
127 | public void preCall(TaskContext taskContext) {
128 | System.out.println("preCall 1");
129 | }
130 |
131 | @Override
132 | public void afterCall(TaskContext taskContext) {
133 | logger.info("afterCall ----------"+taskContext.getJobTaskMetrics().toString());
134 | }
135 |
136 | @Override
137 | public void throwException(TaskContext taskContext, Throwable e) {
138 | logger.info("afterCall ----------"+taskContext.getJobTaskMetrics().toString(),e);
139 | }
140 | });
141 | // //设置任务执行拦截器结束,可以添加多个
142 | //增量配置开始
143 | importBuilder.setStatusDbname("postgres");//设置增量状态数据源名称
144 | importBuilder.setLastValueColumn("optime");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段
145 | importBuilder.setFromFirst(false);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据,
146 | // setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据
147 | importBuilder.setLastValueType(ImportIncreamentConfig.TIMESTAMP_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型
148 | // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
149 | // try {
150 | // Date date = format.parse("2000-01-01");
151 | // importBuilder.setLastValue(date);//增量起始值配置
152 | // }
153 | // catch (Exception e){
154 | // e.printStackTrace();
155 | // }
156 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型
157 | //增量配置结束
158 |
159 | //映射和转换配置开始
160 | // /**
161 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
162 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
163 | // */
164 | // importBuilder.addFieldMapping("document_id","docId")
165 | // .addFieldMapping("docwtime","docwTime")
166 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
167 | //
168 | //
169 | // /**
170 | // * 为每条记录添加额外的字段和值
171 | // * 可以为基本数据类型,也可以是复杂的对象
172 | // */
173 | // importBuilder.addFieldValue("testF1","f1value");
174 | // importBuilder.addFieldValue("testInt",0);
175 | // importBuilder.addFieldValue("testDate",new Date());
176 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
177 | // TestObject testObject = new TestObject();
178 | // testObject.setId("testid");
179 | // testObject.setName("jackson");
180 | // importBuilder.addFieldValue("testObject",testObject);
181 | //
182 | /**
183 | * 重新设置es数据结构
184 | */
185 | importBuilder.setDataRefactor(new DataRefactor() {
186 | public void refactor(Context context) throws Exception {
187 | // Date date = context.getDateValue("LOG_OPERTIME");
188 | // context.addFieldValue("collecttime",new Date());
189 | logger.info("refactor");
190 | }
191 | });
192 | //映射和转换配置结束
193 |
194 | /**
195 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
196 | */
197 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行
198 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
199 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
200 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
201 |
202 | importBuilder.setExportResultHandler(new ExportResultHandler() {
203 | @Override
204 | public void success(TaskCommand taskCommand, String result) {
205 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
206 | logger.info(taskMetrics.toString());
207 | logger.debug(result);
208 | }
209 |
210 | @Override
211 | public void error(TaskCommand taskCommand, String result) {
212 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
213 | logger.info(taskMetrics.toString());
214 | logger.debug(result);
215 | }
216 |
217 | @Override
218 | public void exception(TaskCommand taskCommand, Throwable exception) {
219 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics();
220 | logger.debug(taskMetrics.toString());
221 | }
222 |
223 |
224 | });
225 | /**
226 | * 执行数据库表数据导入es操作
227 | */
228 | DataStream dataStream = importBuilder.builder();
229 | dataStream.execute();//执行导入操作
230 | // dataStream.destroy();//释放资源
231 |
232 |
233 | }
234 |
235 |
236 | }
237 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/QuartzTimestampImportTask.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.frameworkset.tran.config.ImportBuilder;
19 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
20 | import org.frameworkset.tran.plugin.es.output.ElasticsearchOutputConfig;
21 | import org.frameworkset.tran.schedule.ExternalScheduler;
22 | import org.frameworkset.tran.schedule.ImportIncreamentConfig;
23 | import org.frameworkset.tran.schedule.quartz.AbstractQuartzJobHandler;
24 | import org.slf4j.Logger;
25 | import org.slf4j.LoggerFactory;
26 |
27 | import java.text.SimpleDateFormat;
28 | import java.util.Date;
29 |
30 | /**
31 | *
Description: 使用quartz等外部环境定时运行导入数据,需要调试测试quatz作业同步功能,按如下配置进行操作:
32 | * * * 1.在配置文件中添加quartz作业配置-resources/org/frameworkset/task/quarts-task.xml相关内容
33 | * * *
34 | * * *
35 | * * *
41 | * * *
42 | * * *
43 | * * *
44 | * * *
48 | * * *
49 | * * * 2.添加一个带main方法的作业运行
50 | * * * public class QuartzTest {
51 | * * * public static void main(String[] args){
52 | * * * TaskService.getTaskService().startService();
53 | * * * }
54 | * * * }
55 | * * * 然后运行main方法即可
56 | *
57 | * Copyright (c) 2018
58 | * @Date 2019/4/13 13:45
59 | * @author biaoping.yin
60 | * @version 1.0
61 | */
62 | public class QuartzTimestampImportTask extends AbstractQuartzJobHandler {
63 | private Logger logger = LoggerFactory.getLogger(this.getClass());
64 | public void init(){
65 | externalScheduler = new ExternalScheduler();
66 | externalScheduler.dataStream((Object params)->{
67 | ImportBuilder importBuilder = new ImportBuilder() ;
68 | // //增量定时任务不要删表,但是可以通过删表来做初始化操作
69 | // if(dropIndice) {
70 | // try {
71 | // //清除测试表,导入的时候回重建表,测试的时候加上为了看测试效果,实际线上环境不要删表
72 | // String repsonse = ElasticSearchHelper.getRestClientUtil().dropIndice("dbdemo");
73 | // System.out.println(repsonse);
74 | // } catch (Exception e) {
75 | // }
76 | // }
77 |
78 |
79 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
80 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
81 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
82 | // log_id和数据库对应的字段一致,就不需要设置setLastValueColumn信息,
83 | // 但是需要设置setLastValueType告诉工具增量字段的类型
84 | DBInputConfig dbInputConfig = new DBInputConfig();
85 | dbInputConfig.setSql("select * from td_sm_log where LOG_OPERTIME > #[LOG_OPERTIME]");
86 | importBuilder.setInputConfig(dbInputConfig);
87 | // importBuilder.addIgnoreFieldMapping("remark1");
88 | /**
89 | * es相关配置
90 | */
91 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig();
92 | elasticsearchOutputConfig
93 | .setIndex("dbdemo"); //必填项
94 | // .setIndexType("dbdemo") //es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType
95 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新
96 | elasticsearchOutputConfig.setEsIdField("log_id");//设置文档主键,不设置,则自动产生文档id
97 |
98 | elasticsearchOutputConfig.setDebugResponse(false);//设置是否将每次处理的reponse打印到日志文件中,默认false
99 | elasticsearchOutputConfig.setDiscardBulkResponse(false);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false
100 | importBuilder.setOutputConfig(elasticsearchOutputConfig);
101 |
102 | importBuilder.setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId
103 | .setUseLowcase(false) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用
104 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false
105 | .setBatchSize(10); //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理
106 |
107 |
108 | //
109 | // //设置任务执行拦截器,可以添加多个,定时任务每次执行的拦截器
110 | // importBuilder.addCallInterceptor(new CallInterceptor() {
111 | // @Override
112 | // public void preCall(TaskContext taskContext) {
113 | // System.out.println("preCall");
114 | // }
115 | //
116 | // @Override
117 | // public void afterCall(TaskContext taskContext) {
118 | // System.out.println("afterCall");
119 | // }
120 | //
121 | // @Override
122 | // public void throwException(TaskContext taskContext, Throwable e) {
123 | // System.out.println("throwException");
124 | // }
125 | // }).addCallInterceptor(new CallInterceptor() {
126 | // @Override
127 | // public void preCall(TaskContext taskContext) {
128 | // System.out.println("preCall 1");
129 | // }
130 | //
131 | // @Override
132 | // public void afterCall(TaskContext taskContext) {
133 | // System.out.println("afterCall 1");
134 | // }
135 | //
136 | // @Override
137 | // public void throwException(TaskContext taskContext, Throwable e) {
138 | // System.out.println("throwException 1");
139 | // }
140 | // });
141 | // //设置任务执行拦截器结束,可以添加多个
142 | //增量配置开始
143 | // importBuilder.setLastValueColumn("log_id");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段
144 | importBuilder.setFromFirst(true);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据,
145 | //setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据
146 | importBuilder.setLastValueStorePath("quartzlogtable_import");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样
147 | // importBuilder.setLastValueStoreTableName("logs");//记录上次采集的增量字段值的表,可以不指定,采用默认表名increament_tab
148 | importBuilder.setLastValueType(ImportIncreamentConfig.TIMESTAMP_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型
149 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
150 | try {
151 | Date date = format.parse("2000-01-01");
152 | importBuilder.setLastValue(date);
153 | }
154 | catch (Exception e){
155 | e.printStackTrace();
156 | }
157 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型
158 | //增量配置结束
159 |
160 | //映射和转换配置开始
161 | // /**
162 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
163 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
164 | // */
165 | // importBuilder.addFieldMapping("document_id","docId")
166 | // .addFieldMapping("docwtime","docwTime")
167 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
168 | //
169 | //
170 | // /**
171 | // * 为每条记录添加额外的字段和值
172 | // * 可以为基本数据类型,也可以是复杂的对象
173 | // */
174 | // importBuilder.addFieldValue("testF1","f1value");
175 | // importBuilder.addFieldValue("testInt",0);
176 | // importBuilder.addFieldValue("testDate",new Date());
177 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
178 | // TestObject testObject = new TestObject();
179 | // testObject.setId("testid");
180 | // testObject.setName("jackson");
181 | // importBuilder.addFieldValue("testObject",testObject);
182 | //
183 | // /**
184 | // * 重新设置es数据结构
185 | // */
186 | // importBuilder.setDataRefactor(new DataRefactor() {
187 | // public void refactor(Context context) throws Exception {
188 | // CustomObject customObject = new CustomObject();
189 | // customObject.setAuthor((String)context.getValue("author"));
190 | // customObject.setTitle((String)context.getValue("title"));
191 | // customObject.setSubtitle((String)context.getValue("subtitle"));
192 | // customObject.setIds(new int[]{1,2,3});
193 | // context.addFieldValue("docInfo",customObject);//如果还需要构建更多的内部对象,可以继续构建
194 | //
195 | // //上述三个属性已经放置到docInfo中,如果无需再放置到索引文档中,可以忽略掉这些属性
196 | // context.addIgnoreFieldMapping("author");
197 | // context.addIgnoreFieldMapping("title");
198 | // context.addIgnoreFieldMapping("subtitle");
199 | // }
200 | // });
201 | //映射和转换配置结束
202 |
203 | /**
204 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池
205 | */
206 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行
207 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度
208 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量
209 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行
210 |
211 | return importBuilder;
212 | });
213 |
214 | }
215 |
216 |
217 | }
218 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/TestMysqlAdaptor.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.frameworkset.orm.adapter.DBMM;
19 |
20 | /**
21 | *
Description: 替换mysql的adaptor为自定义的adaptor演示
22 | *
23 | * Copyright (c) 2018
24 | * @Date 2019/4/21 13:28
25 | * @author biaoping.yin
26 | * @version 1.0
27 | */
28 | public class TestMysqlAdaptor extends DBMM {
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/org/frameworkset/elasticsearch/imp/dummy/DB2DummyDemo.java:
--------------------------------------------------------------------------------
1 | package org.frameworkset.elasticsearch.imp.dummy;
2 | /**
3 | * Copyright 2020 bboss
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.frameworkset.util.SimpleStringUtil;
19 | import org.frameworkset.elasticsearch.serial.SerialUtil;
20 | import org.frameworkset.tran.CommonRecord;
21 | import org.frameworkset.tran.DataRefactor;
22 | import org.frameworkset.tran.DataStream;
23 | import org.frameworkset.tran.config.ImportBuilder;
24 | import org.frameworkset.tran.context.Context;
25 | import org.frameworkset.tran.plugin.db.input.DBInputConfig;
26 | import org.frameworkset.tran.plugin.dummy.output.DummyOutputConfig;
27 | import org.frameworkset.tran.schedule.CallInterceptor;
28 | import org.frameworkset.tran.schedule.ImportIncreamentConfig;
29 | import org.frameworkset.tran.schedule.TaskContext;
30 | import org.frameworkset.tran.util.RecordGenerator;
31 |
32 | import java.io.Writer;
33 | import java.text.DateFormat;
34 | import java.util.Date;
35 |
36 | /**
37 | *
Description:
38 | *
39 | * Copyright (c) 2020
40 | * @Date 2021/5/26 9:44
41 | * @author biaoping.yin
42 | * @version 1.0
43 | */
44 | public class DB2DummyDemo {
45 | public static void main(String[] args){
46 | ImportBuilder importBuilder = new ImportBuilder();
47 | importBuilder.setBatchSize(1000).setFetchSize(5000);
48 |
49 |
50 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑,
51 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如:
52 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id]
53 | // 需要设置setLastValueColumn信息log_id,
54 | // 通过setLastValueType方法告诉工具增量字段的类型,默认是数字类型
55 | // importBuilder.setSqlName("insertSQLnew"); //指定将es文档数据同步到数据库的sql语句名称,配置在dsl2ndSqlFile.xml中
56 | DummyOutputConfig dummyOutputConfig = new DummyOutputConfig();
57 | dummyOutputConfig.setRecordGenerator(new RecordGenerator() {
58 | @Override
59 | public void buildRecord(TaskContext taskContext, CommonRecord record, Writer builder) throws Exception{
60 | SimpleStringUtil.object2json(record.getDatas(),builder);
61 |
62 | }
63 | }).setPrintRecord(true);
64 |
65 | importBuilder.setOutputConfig(dummyOutputConfig);
66 |
67 | DBInputConfig dbInputConfig = new DBInputConfig();
68 | dbInputConfig.setSql("select * from td_sm_log where log_id > #[log_id]");
69 | importBuilder.setInputConfig(dbInputConfig);
70 | //定时任务配置,
71 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明
72 | // .setScheduleDate(date) //指定任务开始执行时间:日期
73 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行
74 | .setPeriod(10000L); //每隔period毫秒执行,如果不设置,只执行一次
75 | //定时任务配置结束
76 |
77 | //设置任务执行拦截器,可以添加多个
78 | importBuilder.addCallInterceptor(new CallInterceptor() {
79 | @Override
80 | public void preCall(TaskContext taskContext) {
81 | System.out.println("preCall");
82 | }
83 |
84 | @Override
85 | public void afterCall(TaskContext taskContext) {
86 | System.out.println("afterCall");
87 | }
88 |
89 | @Override
90 | public void throwException(TaskContext taskContext, Throwable e) {
91 | System.out.println("throwException");
92 | }
93 | });
94 | // //设置任务执行拦截器结束,可以添加多个
95 | //增量配置开始
96 | importBuilder.setLastValueColumn("log_id");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段
97 | importBuilder.setFromFirst(true);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据,
98 | // setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据
99 | importBuilder.setLastValueStorePath("dummydbtable_import");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样
100 | importBuilder.setLastValueType(ImportIncreamentConfig.NUMBER_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型
101 | //增量配置结束
102 |
103 | //映射和转换配置开始
104 | // /**
105 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId
106 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换
107 | // */
108 | // importBuilder.addFieldMapping("document_id","docId")
109 | // .addFieldMapping("docwtime","docwTime")
110 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段
111 | //
112 | //
113 | // /**
114 | // * 为每条记录添加额外的字段和值
115 | // * 可以为基本数据类型,也可以是复杂的对象
116 | // */
117 | // importBuilder.addFieldValue("testF1","f1value");
118 | // importBuilder.addFieldValue("testInt",0);
119 | // importBuilder.addFieldValue("testDate",new Date());
120 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date());
121 | // TestObject testObject = new TestObject();
122 | // testObject.setId("testid");
123 | // testObject.setName("jackson");
124 | // importBuilder.addFieldValue("testObject",testObject);
125 | importBuilder.addFieldValue("author","张无忌");
126 | // importBuilder.addFieldMapping("operModule","OPER_MODULE");
127 | // importBuilder.addFieldMapping("logContent","LOG_CONTENT");
128 | // importBuilder.addFieldMapping("logOperuser","LOG_OPERUSER");
129 |
130 |
131 | /**
132 | * 重新设置es数据结构
133 | */
134 | importBuilder.setDataRefactor(new DataRefactor() {
135 | public void refactor(Context context) throws Exception {
136 | //可以根据条件定义是否丢弃当前记录
137 | //context.setDrop(true);return;
138 | // if(s.incrementAndGet() % 2 == 0) {
139 | // context.setDrop(true);
140 | // return;
141 | // }
142 |
143 |
144 | context.addFieldValue("author","duoduo");//将会覆盖全局设置的author变量
145 | context.addFieldValue("title","解放");
146 | context.addFieldValue("subtitle","小康");
147 |
148 | // context.addIgnoreFieldMapping("title");
149 | //上述三个属性已经放置到docInfo中,如果无需再放置到索引文档中,可以忽略掉这些属性
150 | // context.addIgnoreFieldMapping("author");
151 |
152 | // //修改字段名称title为新名称newTitle,并且修改字段的值
153 | // context.newName2ndData("title","newTitle",(String)context.getValue("title")+" append new Value");
154 | context.addIgnoreFieldMapping("subtitle");
155 | /**
156 | * 获取ip对应的运营商和区域信息
157 | */
158 |
159 | DateFormat dateFormat = SerialUtil.getDateFormateMeta().toDateFormat();
160 | Date optime = context.getDateValue("LOG_OPERTIME",dateFormat);
161 | context.addFieldValue("logOpertime",optime);
162 |
163 | /**
164 | //关联查询数据,单值查询
165 | Map headdata = SQLExecutor.queryObjectWithDBName(Map.class,"test",
166 | "select * from head where billid = ? and othercondition= ?",
167 | context.getIntegerValue("billid"),"otherconditionvalue");//多个条件用逗号分隔追加
168 | //将headdata中的数据,调用addFieldValue方法将数据加入当前es文档,具体如何构建文档数据结构根据需求定
169 | context.addFieldValue("headdata",headdata);
170 | //关联查询数据,多值查询
171 | List