├── .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 | ![GitHub Logo](https://static.oschina.net/uploads/space/2017/0617/094201_QhWs_94045.jpg) 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 facedatas = SQLExecutor.queryListWithDBName(Map.class,"test", 172 | "select * from facedata where billid = ?", 173 | context.getIntegerValue("billid")); 174 | //将facedatas中的数据,调用addFieldValue方法将数据加入当前es文档,具体如何构建文档数据结构根据需求定 175 | context.addFieldValue("facedatas",facedatas); 176 | */ 177 | } 178 | }); 179 | //映射和转换配置结束 180 | 181 | /** 182 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池 183 | */ 184 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行 185 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度 186 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量 187 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行 188 | // importBuilder.setDebugResponse(false);//设置是否将每次处理的reponse打印到日志文件中,默认false,不打印响应报文将大大提升性能,只有在调试需要的时候才打开,log日志级别同时要设置为INFO 189 | // importBuilder.setDiscardBulkResponse(true);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认true,如果不需要响应报文将大大提升处理速度 190 | importBuilder.setPrintTaskLog(true); 191 | 192 | 193 | /** 194 | * 执行es数据导入数据库表操作 195 | */ 196 | DataStream dataStream = importBuilder.builder(); 197 | dataStream.execute();//执行导入操作 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /src/main/java/org/frameworkset/elasticsearch/imp/dummy/DB2DummyOnceDemo.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.TaskContext; 29 | import org.frameworkset.tran.util.RecordGenerator; 30 | 31 | import java.io.Writer; 32 | import java.text.DateFormat; 33 | import java.util.Date; 34 | 35 | /** 36 | *

Description: 只允许一次并自动结束的作业

37 | *

38 | *

Copyright (c) 2020

39 | * @Date 2021/5/26 9:44 40 | * @author biaoping.yin 41 | * @version 1.0 42 | */ 43 | public class DB2DummyOnceDemo { 44 | public static void main(String[] args){ 45 | ImportBuilder importBuilder = new ImportBuilder(); 46 | importBuilder.setBatchSize(1000).setFetchSize(5000); 47 | 48 | 49 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑, 50 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如: 51 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id] 52 | // 需要设置setLastValueColumn信息log_id, 53 | // 通过setLastValueType方法告诉工具增量字段的类型,默认是数字类型 54 | // importBuilder.setSqlName("insertSQLnew"); //指定将es文档数据同步到数据库的sql语句名称,配置在dsl2ndSqlFile.xml中 55 | DummyOutputConfig dummyOutputConfig = new DummyOutputConfig(); 56 | dummyOutputConfig.setRecordGenerator(new RecordGenerator() { 57 | @Override 58 | public void buildRecord(TaskContext taskContext, CommonRecord record, Writer builder) throws Exception{ 59 | SimpleStringUtil.object2json(record.getDatas(),builder); 60 | 61 | } 62 | }).setPrintRecord(true); 63 | 64 | importBuilder.setOutputConfig(dummyOutputConfig); 65 | 66 | DBInputConfig dbInputConfig = new DBInputConfig(); 67 | dbInputConfig.setSql("select * from td_sm_log"); 68 | importBuilder.setInputConfig(dbInputConfig); 69 | 70 | 71 | //设置任务执行拦截器,可以添加多个 72 | importBuilder.addCallInterceptor(new CallInterceptor() { 73 | @Override 74 | public void preCall(TaskContext taskContext) { 75 | System.out.println("preCall"); 76 | } 77 | 78 | @Override 79 | public void afterCall(TaskContext taskContext) { 80 | System.out.println("afterCall"); 81 | } 82 | 83 | @Override 84 | public void throwException(TaskContext taskContext, Throwable e) { 85 | System.out.println("throwException"); 86 | } 87 | }); 88 | 89 | //映射和转换配置开始 90 | // /** 91 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId 92 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换 93 | // */ 94 | // importBuilder.addFieldMapping("document_id","docId") 95 | // .addFieldMapping("docwtime","docwTime") 96 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段 97 | // 98 | // 99 | // /** 100 | // * 为每条记录添加额外的字段和值 101 | // * 可以为基本数据类型,也可以是复杂的对象 102 | // */ 103 | // importBuilder.addFieldValue("testF1","f1value"); 104 | // importBuilder.addFieldValue("testInt",0); 105 | // importBuilder.addFieldValue("testDate",new Date()); 106 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date()); 107 | // TestObject testObject = new TestObject(); 108 | // testObject.setId("testid"); 109 | // testObject.setName("jackson"); 110 | // importBuilder.addFieldValue("testObject",testObject); 111 | importBuilder.addFieldValue("author","张无忌"); 112 | // importBuilder.addFieldMapping("operModule","OPER_MODULE"); 113 | // importBuilder.addFieldMapping("logContent","LOG_CONTENT"); 114 | // importBuilder.addFieldMapping("logOperuser","LOG_OPERUSER"); 115 | 116 | 117 | /** 118 | * 重新设置es数据结构 119 | */ 120 | importBuilder.setDataRefactor(new DataRefactor() { 121 | public void refactor(Context context) throws Exception { 122 | //可以根据条件定义是否丢弃当前记录 123 | //context.setDrop(true);return; 124 | // if(s.incrementAndGet() % 2 == 0) { 125 | // context.setDrop(true); 126 | // return; 127 | // } 128 | 129 | 130 | context.addFieldValue("author","duoduo");//将会覆盖全局设置的author变量 131 | context.addFieldValue("title","解放"); 132 | context.addFieldValue("subtitle","小康"); 133 | 134 | // context.addIgnoreFieldMapping("title"); 135 | //上述三个属性已经放置到docInfo中,如果无需再放置到索引文档中,可以忽略掉这些属性 136 | // context.addIgnoreFieldMapping("author"); 137 | 138 | // //修改字段名称title为新名称newTitle,并且修改字段的值 139 | // context.newName2ndData("title","newTitle",(String)context.getValue("title")+" append new Value"); 140 | context.addIgnoreFieldMapping("subtitle"); 141 | /** 142 | * 获取ip对应的运营商和区域信息 143 | */ 144 | 145 | DateFormat dateFormat = SerialUtil.getDateFormateMeta().toDateFormat(); 146 | Date optime = context.getDateValue("LOG_OPERTIME",dateFormat); 147 | context.addFieldValue("logOpertime",optime); 148 | 149 | /** 150 | //关联查询数据,单值查询 151 | Map headdata = SQLExecutor.queryObjectWithDBName(Map.class,"test", 152 | "select * from head where billid = ? and othercondition= ?", 153 | context.getIntegerValue("billid"),"otherconditionvalue");//多个条件用逗号分隔追加 154 | //将headdata中的数据,调用addFieldValue方法将数据加入当前es文档,具体如何构建文档数据结构根据需求定 155 | context.addFieldValue("headdata",headdata); 156 | //关联查询数据,多值查询 157 | List facedatas = SQLExecutor.queryListWithDBName(Map.class,"test", 158 | "select * from facedata where billid = ?", 159 | context.getIntegerValue("billid")); 160 | //将facedatas中的数据,调用addFieldValue方法将数据加入当前es文档,具体如何构建文档数据结构根据需求定 161 | context.addFieldValue("facedatas",facedatas); 162 | */ 163 | } 164 | }); 165 | //映射和转换配置结束 166 | 167 | /** 168 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池 169 | */ 170 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行 171 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度 172 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量 173 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行 174 | // importBuilder.setDebugResponse(false);//设置是否将每次处理的reponse打印到日志文件中,默认false,不打印响应报文将大大提升性能,只有在调试需要的时候才打开,log日志级别同时要设置为INFO 175 | // importBuilder.setDiscardBulkResponse(true);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认true,如果不需要响应报文将大大提升处理速度 176 | importBuilder.setPrintTaskLog(true); 177 | 178 | 179 | /** 180 | * 执行es数据导入数据库表操作 181 | */ 182 | DataStream dataStream = importBuilder.builder(); 183 | dataStream.execute();//执行导入操作 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /src/main/java/org/frameworkset/elasticsearch/imp/dummy/ES2DummyDemo.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.dummy.output.DummyOutputConfig; 26 | import org.frameworkset.tran.plugin.es.input.ElasticsearchInputConfig; 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 | import java.util.Map; 36 | 37 | /** 38 | *

Description:

39 | *

40 | *

Copyright (c) 2020

41 | * @Date 2021/5/26 9:44 42 | * @author biaoping.yin 43 | * @version 1.0 44 | */ 45 | public class ES2DummyDemo { 46 | public static void main(String[] args){ 47 | ImportBuilder importBuilder = new ImportBuilder(); 48 | importBuilder.setBatchSize(1000).setFetchSize(5000); 49 | 50 | 51 | //指定导入数据的sql语句,必填项,可以设置自己的提取逻辑, 52 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如: 53 | // select * from td_sm_log where log_id > #[log_id] and parent_id = #[log_id] 54 | // 需要设置setLastValueColumn信息log_id, 55 | // 通过setLastValueType方法告诉工具增量字段的类型,默认是数字类型 56 | // importBuilder.setSqlName("insertSQLnew"); //指定将es文档数据同步到数据库的sql语句名称,配置在dsl2ndSqlFile.xml中 57 | DummyOutputConfig dummyOupputConfig = new DummyOutputConfig(); 58 | dummyOupputConfig.setRecordGenerator(new RecordGenerator() { 59 | @Override 60 | public void buildRecord(TaskContext taskContext, CommonRecord record, Writer builder) throws Exception{ 61 | SimpleStringUtil.object2json(record.getDatas(),builder); 62 | 63 | } 64 | }).setPrintRecord(false); 65 | 66 | importBuilder.setOutputConfig(dummyOupputConfig); 67 | /** 68 | * es相关配置 69 | */ 70 | ElasticsearchInputConfig elasticsearchInputConfig = new ElasticsearchInputConfig(); 71 | elasticsearchInputConfig 72 | .setDslFile("dsl2ndSqlFile.xml") 73 | .setDslName("scrollQuery") 74 | .setScrollLiveTime("10m") 75 | // .setSliceQuery(true) 76 | // .setSliceSize(5) 77 | .setQueryUrl("dbdemo/_search"); 78 | //添加额外变量参数,可以在查询dsl中使用 79 | importBuilder.addParam("fullImport",false); 80 | // //添加dsl中需要用到的参数及参数值 81 | // .addParam("var1","v1") 82 | // .addParam("var2","v2") 83 | // .addParam("var3","v3") 84 | 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 | }); 110 | // //设置任务执行拦截器结束,可以添加多个 111 | //增量配置开始 112 | importBuilder.setLastValueColumn("collecttime");//手动指定日期增量查询字段变量名称 113 | importBuilder.setFromFirst(true);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据, 114 | //setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据 115 | importBuilder.setLastValueStorePath("es2log_import");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样 116 | // importBuilder.setLastValueStoreTableName("logs");//记录上次采集的增量字段值的表,可以不指定,采用默认表名increament_tab 117 | importBuilder.setLastValueType(ImportIncreamentConfig.TIMESTAMP_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型 118 | importBuilder.setIncreamentEndOffset(5); 119 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型 120 | // importBuilder.setLastValue(new Date()); 121 | //增量配置结束 122 | 123 | //映射和转换配置开始 124 | // /** 125 | // * db-es mapping 表字段名称到es 文档字段的映射:比如document_id -> docId 126 | // * 可以配置mapping,也可以不配置,默认基于java 驼峰规则进行db field-es field的映射和转换 127 | // */ 128 | // importBuilder.addFieldMapping("document_id","docId") 129 | // .addFieldMapping("docwtime","docwTime") 130 | // .addIgnoreFieldMapping("channel_id");//添加忽略字段 131 | // 132 | // 133 | // /** 134 | // * 为每条记录添加额外的字段和值 135 | // * 可以为基本数据类型,也可以是复杂的对象 136 | // */ 137 | // importBuilder.addFieldValue("testF1","f1value"); 138 | // importBuilder.addFieldValue("testInt",0); 139 | // importBuilder.addFieldValue("testDate",new Date()); 140 | // importBuilder.addFieldValue("testFormateDate","yyyy-MM-dd HH",new Date()); 141 | // TestObject testObject = new TestObject(); 142 | // testObject.setId("testid"); 143 | // testObject.setName("jackson"); 144 | // importBuilder.addFieldValue("testObject",testObject); 145 | importBuilder.addFieldValue("author","张无忌"); 146 | // importBuilder.addFieldMapping("operModule","OPER_MODULE"); 147 | // importBuilder.addFieldMapping("logContent","LOG_CONTENT"); 148 | // importBuilder.addFieldMapping("logOperuser","LOG_OPERUSER"); 149 | 150 | 151 | /** 152 | * 重新设置es数据结构 153 | */ 154 | importBuilder.setDataRefactor(new DataRefactor() { 155 | public void refactor(Context context) throws Exception { 156 | //可以根据条件定义是否丢弃当前记录 157 | //context.setDrop(true);return; 158 | // if(s.incrementAndGet() % 2 == 0) { 159 | // context.setDrop(true); 160 | // return; 161 | // } 162 | 163 | 164 | context.addFieldValue("author","duoduo");//将会覆盖全局设置的author变量 165 | context.addFieldValue("title","解放"); 166 | context.addFieldValue("subtitle","小康"); 167 | 168 | // context.addIgnoreFieldMapping("title"); 169 | //上述三个属性已经放置到docInfo中,如果无需再放置到索引文档中,可以忽略掉这些属性 170 | // context.addIgnoreFieldMapping("author"); 171 | 172 | // //修改字段名称title为新名称newTitle,并且修改字段的值 173 | // context.newName2ndData("title","newTitle",(String)context.getValue("title")+" append new Value"); 174 | context.addIgnoreFieldMapping("subtitle"); 175 | /** 176 | * 获取ip对应的运营商和区域信息 177 | */ 178 | Map ipInfo = (Map)context.getValue("ipInfo"); 179 | if(ipInfo != null) 180 | context.addFieldValue("ipinfo", SimpleStringUtil.object2json(ipInfo)); 181 | else{ 182 | context.addFieldValue("ipinfo", ""); 183 | } 184 | DateFormat dateFormat = SerialUtil.getDateFormateMeta().toDateFormat(); 185 | Date optime = context.getDateValue("LOG_OPERTIME",dateFormat); 186 | context.addFieldValue("logOpertime",optime); 187 | 188 | /** 189 | //关联查询数据,单值查询 190 | Map headdata = SQLExecutor.queryObjectWithDBName(Map.class,"test", 191 | "select * from head where billid = ? and othercondition= ?", 192 | context.getIntegerValue("billid"),"otherconditionvalue");//多个条件用逗号分隔追加 193 | //将headdata中的数据,调用addFieldValue方法将数据加入当前es文档,具体如何构建文档数据结构根据需求定 194 | context.addFieldValue("headdata",headdata); 195 | //关联查询数据,多值查询 196 | List facedatas = SQLExecutor.queryListWithDBName(Map.class,"test", 197 | "select * from facedata where billid = ?", 198 | context.getIntegerValue("billid")); 199 | //将facedatas中的数据,调用addFieldValue方法将数据加入当前es文档,具体如何构建文档数据结构根据需求定 200 | context.addFieldValue("facedatas",facedatas); 201 | */ 202 | } 203 | }); 204 | //映射和转换配置结束 205 | 206 | /** 207 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池 208 | */ 209 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行 210 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度 211 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量 212 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行 213 | importBuilder.setPrintTaskLog(true); 214 | 215 | 216 | /** 217 | * 执行es数据导入数据库表操作 218 | */ 219 | DataStream dataStream = importBuilder.builder(); 220 | dataStream.execute();//执行导入操作 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /src/main/java/org/frameworkset/elasticsearch/imp/http/ES2HttpDemo.java: -------------------------------------------------------------------------------- 1 | package org.frameworkset.elasticsearch.imp.http; 2 | /** 3 | * Copyright 2022 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.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.es.input.ElasticsearchInputConfig; 25 | import org.frameworkset.tran.plugin.http.output.HttpOutputConfig; 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:

37 | *

38 | *

Copyright (c) 2020

39 | * @Date 2022/7/1 40 | * @author biaoping.yin 41 | * @version 1.0 42 | */ 43 | public class ES2HttpDemo { 44 | private static Logger logger = LoggerFactory.getLogger(ES2HttpDemo.class); 45 | public static void main(String[] args){ 46 | 47 | 48 | ImportBuilder importBuilder = new ImportBuilder() ; 49 | importBuilder.setFetchSize(50).setBatchSize(10); 50 | ElasticsearchInputConfig elasticsearchInputConfig = new ElasticsearchInputConfig(); 51 | elasticsearchInputConfig.setDslFile("dsl2ndSqlFile.xml")//配置dsl和sql语句的配置文件 52 | .setDslName("scrollQuery") //指定从es查询索引文档数据的dsl语句名称,配置在dsl2ndSqlFile.xml中 53 | .setScrollLiveTime("10m") //scroll查询的scrollid有效期 54 | 55 | // .setSliceQuery(true) 56 | // .setSliceSize(5) 57 | .setQueryUrl("https2es/_search") ;//查询索引表demo中的文档数据 58 | 59 | // //添加dsl中需要用到的参数及参数值 60 | // exportBuilder.addParam("var1","v1") 61 | // .addParam("var2","v2") 62 | // .addParam("var3","v3"); 63 | 64 | importBuilder.setInputConfig(elasticsearchInputConfig); 65 | HttpOutputConfig httpOutputConfig = new HttpOutputConfig(); 66 | //指定导入数据的dsl语句,必填项,可以设置自己的提取逻辑, 67 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如: 68 | 69 | 70 | httpOutputConfig 71 | .setServiceUrl("/httpservice/sendData.api") 72 | .setHttpMethod("post") 73 | .addTargetHttpPoolName("http.poolNames","datatran") 74 | .addHttpOutputConfig("datatran.http.health","/health") 75 | .addHttpOutputConfig("datatran.http.hosts","192.168.137.1:808") 76 | .addHttpOutputConfig("datatran.http.timeoutConnection","5000") 77 | .addHttpOutputConfig("datatran.http.timeoutSocket","50000") 78 | .addHttpOutputConfig("datatran.http.connectionRequestTimeout","50000") 79 | .addHttpOutputConfig("datatran.http.maxTotal","200") 80 | .addHttpOutputConfig("datatran.http.defaultMaxPerRoute","100") 81 | .addHttpOutputConfig("datatran.http.failAllContinue","true"); 82 | 83 | importBuilder.setOutputConfig(httpOutputConfig); 84 | 85 | 86 | importBuilder 87 | // 88 | .setUseJavaName(true) //可选项,将数据库字段名称转换为java驼峰规范的名称,true转换,false不转换,默认false,例如:doc_id -> docId 89 | .setUseLowcase(true) //可选项,true 列名称转小写,false列名称不转换小写,默认false,只要在UseJavaName为false的情况下,配置才起作用 90 | .setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false 91 | ; //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理 92 | 93 | //定时任务配置, 94 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明 95 | // .setScheduleDate(date) //指定任务开始执行时间:日期 96 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行 97 | .setPeriod(1000L * 60); //每隔period毫秒执行,如果不设置,只执行一次 98 | //定时任务配置结束 99 | // 100 | // //设置任务执行拦截器,可以添加多个,定时任务每次执行的拦截器 101 | importBuilder.addCallInterceptor(new CallInterceptor() { 102 | @Override 103 | public void preCall(TaskContext taskContext) { 104 | System.out.println("preCall"); 105 | } 106 | 107 | @Override 108 | public void afterCall(TaskContext taskContext) { 109 | System.out.println("afterCall"); 110 | } 111 | 112 | @Override 113 | public void throwException(TaskContext taskContext, Throwable e) { 114 | System.out.println("throwException"); 115 | } 116 | }).addCallInterceptor(new CallInterceptor() { 117 | @Override 118 | public void preCall(TaskContext taskContext) { 119 | System.out.println("preCall 1"); 120 | } 121 | 122 | @Override 123 | public void afterCall(TaskContext taskContext) { 124 | System.out.println("afterCall 1"); 125 | } 126 | 127 | @Override 128 | public void throwException(TaskContext taskContext, Throwable e) { 129 | System.out.println("throwException 1"); 130 | } 131 | }); 132 | // //设置任务执行拦截器结束,可以添加多个 133 | //增量配置开始 134 | // importBuilder.setStatusDbname("test");//设置增量状态数据源名称 135 | importBuilder.setLastValueColumn("collecttime");//手动指定数字增量查询字段,默认采用上面设置的sql语句中的增量变量名称作为增量查询字段的名称,指定以后就用指定的字段 136 | importBuilder.setFromFirst(true);//setFromfirst(false),如果作业停了,作业重启后从上次截止位置开始采集数据, 137 | // setFromfirst(true) 如果作业停了,作业重启后,重新开始采集数据 138 | importBuilder.setStatusDbname("es2http"); 139 | importBuilder.setLastValueStorePath("es2http_import");//记录上次采集的增量字段值的文件路径,作为下次增量(或者重启后)采集数据的起点,不同的任务这个路径要不一样 140 | importBuilder.setLastValueStoreTableName("es2http");//记录上次采集的增量字段值的表,可以不指定,采用默认表名increament_tab 141 | importBuilder.setLastValueType(ImportIncreamentConfig.TIMESTAMP_TYPE);//如果没有指定增量查询字段名称,则需要指定字段类型:ImportIncreamentConfig.NUMBER_TYPE 数字类型 142 | importBuilder.setIncreamentEndOffset(60*1);//单位:秒 143 | // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 144 | // try { 145 | // Date date = format.parse("2000-01-01"); 146 | // importBuilder.setLastValue(date);//增量起始值配置 147 | // } 148 | // catch (Exception e){ 149 | // e.printStackTrace(); 150 | // } 151 | // 或者ImportIncreamentConfig.TIMESTAMP_TYPE 日期类型 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 | // long logTime = context.getLongValue("logTime"); 183 | // context.addFieldValue("logTime",new Date(logTime)); 184 | // long oldLogTime = context.getLongValue("oldLogTime"); 185 | // context.addFieldValue("oldLogTime",new Date(oldLogTime)); 186 | // long oldLogTimeEndTime = context.getLongValue("oldLogTimeEndTime"); 187 | // context.addFieldValue("oldLogTimeEndTime",new Date(oldLogTimeEndTime)); 188 | // Date date = context.getDateValue("LOG_OPERTIME"); 189 | 190 | 191 | context.addFieldValue("newCollecttime",new Date());//添加采集时间 192 | 193 | } 194 | }); 195 | //映射和转换配置结束 196 | 197 | /** 198 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池 199 | */ 200 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行 201 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度 202 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量 203 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行 204 | 205 | importBuilder.setExportResultHandler(new ExportResultHandler() { 206 | @Override 207 | public void success(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 error(TaskCommand taskCommand, String result) { 215 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics(); 216 | logger.info(taskMetrics.toString()); 217 | logger.debug(result); 218 | } 219 | 220 | @Override 221 | public void exception(TaskCommand taskCommand, Throwable exception) { 222 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics(); 223 | logger.debug(taskMetrics.toString()); 224 | } 225 | 226 | }); 227 | /** 228 | * 执行数据库表数据导入es操作 229 | */ 230 | DataStream dataStream = importBuilder.builder(); 231 | dataStream.execute();//执行导入操作 232 | } 233 | } 234 | -------------------------------------------------------------------------------- /src/main/java/org/frameworkset/elasticsearch/imp/http/ParrelHttp2ESDemo.java: -------------------------------------------------------------------------------- 1 | package org.frameworkset.elasticsearch.imp.http; 2 | /** 3 | * Copyright 2022 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.apache.http.HttpResponse; 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.es.output.ElasticsearchOutputConfig; 26 | import org.frameworkset.tran.plugin.http.input.HttpInputConfig; 27 | import org.frameworkset.tran.plugin.http.input.HttpRecord; 28 | import org.frameworkset.tran.schedule.CallInterceptor; 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 | import java.util.Date; 35 | 36 | /** 37 | *

Description: http并行查询同步数据到Elasticsearch案例 38 | * 并行查询不支持增量同步处理 39 | *

40 | *

41 | *

Copyright (c) 2020

42 | * @Date 2022/7/1 43 | * @author biaoping.yin 44 | * @version 1.0 45 | */ 46 | public class ParrelHttp2ESDemo { 47 | private static Logger logger = LoggerFactory.getLogger(ParrelHttp2ESDemo.class); 48 | public static void main(String[] args){ 49 | 50 | 51 | ImportBuilder importBuilder = new ImportBuilder() ; 52 | importBuilder.setFetchSize(50).setBatchSize(10); 53 | HttpInputConfig httpInputConfig = new HttpInputConfig(); 54 | //指定导入数据的dsl语句,必填项,可以设置自己的提取逻辑, 55 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如: 56 | 57 | 58 | httpInputConfig.setDslFile("httpdsl.xml") 59 | .setQueryDslName("parrelqueryDsl") 60 | .setQueryUrl("/httpservice/getData.api") 61 | .setShowDsl(true) 62 | .addSourceHttpPoolName("http.poolNames","datatran") 63 | .addHttpInputConfig("datatran.http.health","/health") 64 | .addHttpInputConfig("datatran.http.hosts","192.168.137.1:808") 65 | .addHttpInputConfig("datatran.http.timeoutConnection","5000") 66 | .addHttpInputConfig("datatran.http.timeoutSocket","50000") 67 | .addHttpInputConfig("datatran.http.connectionRequestTimeout","50000") 68 | .addHttpInputConfig("datatran.http.maxTotal","200") 69 | .addHttpInputConfig("datatran.http.defaultMaxPerRoute","100") 70 | .addHttpInputConfig("datatran.http.failAllContinue","true"); 71 | 72 | importBuilder.setInputConfig(httpInputConfig); 73 | //设置并行查询线程数和等待队长度、结果异步处理队列长度 74 | httpInputConfig.setQueryThread(10); 75 | httpInputConfig.setQueryThreadQueue(20); 76 | httpInputConfig.setQueryResultQueue(20); 77 | //添加并行查询参数组 78 | importBuilder.addJobInputParam("otherParam","陈雨菲2:0战胜戴资颖"); 79 | importBuilder.makeParamGroup(); 80 | importBuilder.addJobInputParam("otherParam","安塞龙1:2惜败黄智勇"); 81 | importBuilder.makeParamGroup(); 82 | importBuilder.addJobInputParam("otherParam","桃田0:2惨败昆拉武特"); 83 | importBuilder.makeParamGroup(); 84 | importBuilder.addJobInputParam("otherParam","石宇奇2:1胜黄智勇"); 85 | importBuilder.makeParamGroup(); 86 | importBuilder.addJobInputParam("otherParam","翁弘扬2:0横扫乔纳坦"); 87 | importBuilder.makeParamGroup(); 88 | 89 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig(); 90 | elasticsearchOutputConfig.setTargetElasticsearch("default") 91 | .setIndex("parrelhttps2es") 92 | .setEsIdField("log_id")//设置文档主键,不设置,则自动产生文档id 93 | .setDebugResponse(false)//设置是否将每次处理的reponse打印到日志文件中,默认false 94 | .setDiscardBulkResponse(false);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false 95 | /** 96 | elasticsearchOutputConfig.setEsIdGenerator(new EsIdGenerator() { 97 | //如果指定EsIdGenerator,则根据下面的方法生成文档id, 98 | // 否则根据setEsIdField方法设置的字段值作为文档id, 99 | // 如果默认没有配置EsIdField和如果指定EsIdGenerator,则由es自动生成文档id 100 | 101 | @Override 102 | public Object genId(Context context) throws Exception { 103 | return SimpleStringUtil.getUUID();//返回null,则由es自动生成文档id 104 | } 105 | }); 106 | */ 107 | // .setIndexType("dbdemo") ;//es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType; 108 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新 109 | /** 110 | * es相关配置 111 | */ 112 | // elasticsearchOutputConfig.setTargetElasticsearch("default,test");//同步数据到两个es集群 113 | 114 | importBuilder.setOutputConfig(elasticsearchOutputConfig); 115 | importBuilder.setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false 116 | ; //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理 117 | 118 | //定时任务配置, 119 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明 120 | // .setScheduleDate(date) //指定任务开始执行时间:日期 121 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行 122 | .setPeriod(1000L * 60); //每隔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 | System.out.println("afterCall 1"); 150 | } 151 | 152 | @Override 153 | public void throwException(TaskContext taskContext, Throwable e) { 154 | System.out.println("throwException 1"); 155 | } 156 | }); 157 | // //设置任务执行拦截器结束,可以添加多个 158 | 159 | /** 160 | * 重新设置es数据结构 161 | */ 162 | importBuilder.setDataRefactor(new DataRefactor() { 163 | public void refactor(Context context) throws Exception { 164 | long logTime = context.getLongValue("logTime"); 165 | context.addFieldValue("logTime",new Date(logTime)); 166 | long oldLogTime = context.getLongValue("oldLogTime"); 167 | context.addFieldValue("oldLogTime",new Date(oldLogTime)); 168 | long oldLogTimeEndTime = context.getLongValue("oldLogTimeEndTime"); 169 | context.addFieldValue("oldLogTimeEndTime",new Date(oldLogTimeEndTime)); 170 | // Date date = context.getDateValue("LOG_OPERTIME"); 171 | 172 | HttpRecord record = (HttpRecord) context.getCurrentRecord().getRecord(); 173 | HttpResponse response = record.getResponse();//可以从httpresponse中获取head之类的信息 174 | context.addFieldValue("collecttime",new Date());//添加采集时间 175 | 176 | } 177 | }); 178 | //映射和转换配置结束 179 | 180 | /** 181 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池 182 | */ 183 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行 184 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度 185 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量 186 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行 187 | 188 | importBuilder.setExportResultHandler(new ExportResultHandler() { 189 | @Override 190 | public void success(TaskCommand taskCommand, String result) { 191 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics(); 192 | logger.info(taskMetrics.toString()); 193 | logger.debug(result); 194 | } 195 | 196 | @Override 197 | public void error(TaskCommand taskCommand, String result) { 198 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics(); 199 | logger.info(taskMetrics.toString()); 200 | logger.debug(result); 201 | } 202 | 203 | @Override 204 | public void exception(TaskCommand taskCommand, Throwable exception) { 205 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics(); 206 | logger.debug(taskMetrics.toString()); 207 | } 208 | 209 | }); 210 | /** 211 | * 执行http服务数据导入es作业 212 | */ 213 | DataStream dataStream = importBuilder.builder(); 214 | dataStream.execute();//执行导入操作 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /src/main/java/org/frameworkset/elasticsearch/imp/http/ParrelHttpPagine2ESDemo.java: -------------------------------------------------------------------------------- 1 | package org.frameworkset.elasticsearch.imp.http; 2 | /** 3 | * Copyright 2022 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.apache.http.HttpResponse; 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.es.output.ElasticsearchOutputConfig; 26 | import org.frameworkset.tran.plugin.http.input.HttpInputConfig; 27 | import org.frameworkset.tran.plugin.http.input.HttpRecord; 28 | import org.frameworkset.tran.schedule.CallInterceptor; 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 | import java.util.Date; 35 | 36 | /** 37 | *

Description: http并行查询同步数据到Elasticsearch案例 38 | * 并行查询不支持增量同步处理 39 | *

40 | *

41 | *

Copyright (c) 2020

42 | * @Date 2022/7/1 43 | * @author biaoping.yin 44 | * @version 1.0 45 | */ 46 | public class ParrelHttpPagine2ESDemo { 47 | private static Logger logger = LoggerFactory.getLogger(ParrelHttpPagine2ESDemo.class); 48 | public static void main(String[] args){ 49 | 50 | 51 | ImportBuilder importBuilder = new ImportBuilder() ; 52 | importBuilder.setFetchSize(50).setBatchSize(10); 53 | HttpInputConfig httpInputConfig = new HttpInputConfig(); 54 | //指定导入数据的dsl语句,必填项,可以设置自己的提取逻辑, 55 | // 设置增量变量log_id,增量变量名称#[log_id]可以多次出现在sql语句的不同位置中,例如: 56 | 57 | 58 | httpInputConfig.setDslFile("httpdsl.xml") 59 | .setQueryDslName("parrelPagineQueryDsl") 60 | .setQueryUrl("/httpservice/getPagineData.api") 61 | .setShowDsl(true) 62 | .setPagine(true) 63 | .addSourceHttpPoolName("http.poolNames","datatran") 64 | .addHttpInputConfig("datatran.http.health","/health") 65 | .addHttpInputConfig("datatran.http.hosts","192.168.137.1:808") 66 | .addHttpInputConfig("datatran.http.timeoutConnection","5000") 67 | .addHttpInputConfig("datatran.http.timeoutSocket","50000") 68 | .addHttpInputConfig("datatran.http.connectionRequestTimeout","50000") 69 | .addHttpInputConfig("datatran.http.maxTotal","200") 70 | .addHttpInputConfig("datatran.http.defaultMaxPerRoute","100") 71 | .addHttpInputConfig("datatran.http.failAllContinue","true"); 72 | 73 | importBuilder.setInputConfig(httpInputConfig); 74 | //添加并行查询参数组 75 | importBuilder.addJobInputParam("otherParam","陈雨菲2:0战胜戴资颖"); 76 | importBuilder.makeParamGroup(); 77 | importBuilder.addJobInputParam("otherParam","安塞龙1:2惜败黄智勇"); 78 | importBuilder.makeParamGroup(); 79 | importBuilder.addJobInputParam("otherParam","桃田0:2惨败昆拉武特"); 80 | importBuilder.makeParamGroup(); 81 | importBuilder.addJobInputParam("otherParam","石宇奇2:1胜黄智勇"); 82 | importBuilder.makeParamGroup(); 83 | importBuilder.addJobInputParam("otherParam","翁弘扬2:0横扫乔纳坦"); 84 | importBuilder.makeParamGroup(); 85 | 86 | ElasticsearchOutputConfig elasticsearchOutputConfig = new ElasticsearchOutputConfig(); 87 | elasticsearchOutputConfig.setTargetElasticsearch("default") 88 | .setIndex("parrelpaginehttps2es") 89 | .setEsIdField("log_id")//设置文档主键,不设置,则自动产生文档id 90 | .setDebugResponse(false)//设置是否将每次处理的reponse打印到日志文件中,默认false 91 | .setDiscardBulkResponse(false);//设置是否需要批量处理的响应报文,不需要设置为false,true为需要,默认false 92 | /** 93 | elasticsearchOutputConfig.setEsIdGenerator(new EsIdGenerator() { 94 | //如果指定EsIdGenerator,则根据下面的方法生成文档id, 95 | // 否则根据setEsIdField方法设置的字段值作为文档id, 96 | // 如果默认没有配置EsIdField和如果指定EsIdGenerator,则由es自动生成文档id 97 | 98 | @Override 99 | public Object genId(Context context) throws Exception { 100 | return SimpleStringUtil.getUUID();//返回null,则由es自动生成文档id 101 | } 102 | }); 103 | */ 104 | // .setIndexType("dbdemo") ;//es 7以后的版本不需要设置indexType,es7以前的版本必需设置indexType; 105 | // .setRefreshOption("refresh")//可选项,null表示不实时刷新,importBuilder.setRefreshOption("refresh");表示实时刷新 106 | /** 107 | * es相关配置 108 | */ 109 | // elasticsearchOutputConfig.setTargetElasticsearch("default,test");//同步数据到两个es集群 110 | 111 | importBuilder.setOutputConfig(elasticsearchOutputConfig); 112 | importBuilder.setPrintTaskLog(true) //可选项,true 打印任务执行日志(耗时,处理记录数) false 不打印,默认值false 113 | ; //可选项,批量导入es的记录数,默认为-1,逐条处理,> 0时批量处理 114 | 115 | //定时任务配置, 116 | importBuilder.setFixedRate(false)//参考jdk timer task文档对fixedRate的说明 117 | // .setScheduleDate(date) //指定任务开始执行时间:日期 118 | .setDeyLay(1000L) // 任务延迟执行deylay毫秒后执行 119 | .setPeriod(1000L * 60); //每隔period毫秒执行,如果不设置,只执行一次 120 | //定时任务配置结束 121 | // 122 | // //设置任务执行拦截器,可以添加多个,定时任务每次执行的拦截器 123 | importBuilder.addCallInterceptor(new CallInterceptor() { 124 | @Override 125 | public void preCall(TaskContext taskContext) { 126 | logger.info("preCall"); 127 | } 128 | 129 | @Override 130 | public void afterCall(TaskContext taskContext) { 131 | logger.info("afterCall"); 132 | } 133 | 134 | @Override 135 | public void throwException(TaskContext taskContext, Throwable e) { 136 | logger.error("throwException",e); 137 | } 138 | }); 139 | // //设置任务执行拦截器结束,可以添加多个 140 | 141 | /** 142 | * 重新设置es数据结构 143 | */ 144 | importBuilder.setDataRefactor(new DataRefactor() { 145 | public void refactor(Context context) throws Exception { 146 | long logTime = context.getLongValue("logTime"); 147 | context.addFieldValue("logTime",new Date(logTime)); 148 | long oldLogTime = context.getLongValue("oldLogTime"); 149 | context.addFieldValue("oldLogTime",new Date(oldLogTime)); 150 | long oldLogTimeEndTime = context.getLongValue("oldLogTimeEndTime"); 151 | context.addFieldValue("oldLogTimeEndTime",new Date(oldLogTimeEndTime)); 152 | // Date date = context.getDateValue("LOG_OPERTIME"); 153 | 154 | HttpRecord record = (HttpRecord) context.getCurrentRecord().getRecord(); 155 | HttpResponse response = record.getResponse();//可以从httpresponse中获取head之类的信息 156 | context.addFieldValue("collecttime",new Date());//添加采集时间 157 | 158 | } 159 | }); 160 | //映射和转换配置结束 161 | 162 | /** 163 | * 内置线程池配置,实现多线程并行数据导入功能,作业完成退出时自动关闭该线程池 164 | */ 165 | importBuilder.setParallel(true);//设置为多线程并行批量导入,false串行 166 | importBuilder.setQueue(10);//设置批量导入线程池等待队列长度 167 | importBuilder.setThreadCount(50);//设置批量导入线程池工作线程数量 168 | importBuilder.setContinueOnError(true);//任务出现异常,是否继续执行作业:true(默认值)继续执行 false 中断作业执行 169 | 170 | importBuilder.setExportResultHandler(new ExportResultHandler() { 171 | @Override 172 | public void success(TaskCommand taskCommand, String result) { 173 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics(); 174 | logger.info(taskMetrics.toString()); 175 | logger.debug(result); 176 | } 177 | 178 | @Override 179 | public void error(TaskCommand taskCommand, String result) { 180 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics(); 181 | logger.info(taskMetrics.toString()); 182 | logger.debug(result); 183 | } 184 | 185 | @Override 186 | public void exception(TaskCommand taskCommand, Throwable exception) { 187 | TaskMetrics taskMetrics = taskCommand.getTaskMetrics(); 188 | logger.debug(taskMetrics.toString()); 189 | } 190 | 191 | 192 | }); 193 | /** 194 | * 执行http服务数据导入es作业 195 | */ 196 | DataStream dataStream = importBuilder.builder(); 197 | dataStream.execute();//执行导入操作 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /src/main/java/org/frameworkset/elasticsearch/imp/quartz/Bootstrap.java: -------------------------------------------------------------------------------- 1 | package org.frameworkset.elasticsearch.imp.quartz; 2 | 3 | import org.quartz.*; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | import static org.quartz.CronScheduleBuilder.cronSchedule; 8 | import static org.quartz.TriggerBuilder.newTrigger; 9 | 10 | 11 | /** 12 | * @author MECHREV 13 | */ 14 | public class Bootstrap { 15 | private static Logger logger = LoggerFactory.getLogger(Bootstrap.class); 16 | //2020.8.19.controller里面每一次来新的任务表,都直接调用了quartz.传入参数通过 17 | // @RequestMapping(value = "quartz") 18 | // public void sql2sql() throws SchedulerException, InterruptedException { 19 | // bootstrap.quartz(scheduler); 20 | // } 21 | public void quartz(Scheduler scheduler) throws InterruptedException, SchedulerException { 22 | //2020.8.19在这定义加载的任务类,然后业务逻辑是每一次都在 23 | JobDetail jobDetail = JobBuilder.newJob(ImportDataJob.class) 24 | 25 | .withIdentity("test1", "group1").build(); 26 | // 3、构建Trigger实例,每隔1s执行一次 27 | Trigger trigger = newTrigger() 28 | .withIdentity("test1", "group1") 29 | .withSchedule(cronSchedule("0/30 * * * * ? ")) 30 | .forJob("test1", "group1") 31 | .build(); 32 | //4、执行 33 | scheduler.scheduleJob(jobDetail, trigger); 34 | System.out.println("--------scheduler start ! ------------"); 35 | scheduler.start(); 36 | //睡眠 37 | // TimeUnit.MINUTES.sleep(2); 38 | // scheduler.shutdown(); 39 | System.out.println("--------scheduler shutdown ! ------------"); 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #工具主程序 2 | mainclass=org.frameworkset.elasticsearch.imp.http.Http2ESDemo 3 | # 参数获取方法:CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值 4 | dropIndice=false 5 | queueSize=50 6 | workThreads=20 7 | batchSize=20 8 | 9 | ##多集群配置样例,如果需要做多集群配置,请将参照本文内容修改application.properties文件内容 10 | elasticsearch.serverNames = default,test 11 | 12 | ##x-pack或者searchguard账号和口令 13 | elasticUser=elastic 14 | elasticPassword=changeme 15 | 16 | #elasticsearch.rest.hostNames=10.1.236.88:9200 17 | #elasticsearch.rest.hostNames=127.0.0.1:9200 18 | #elasticsearch.rest.hostNames=10.21.20.168:9200 19 | elasticsearch.rest.hostNames=192.168.137.1:9200 20 | #elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282 21 | 22 | #在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别 23 | elasticsearch.showTemplate=false 24 | elasticsearch.discoverHost=false 25 | 26 | ##default连接池配置 27 | http.timeoutConnection = 5000 28 | http.timeoutSocket = 50000 29 | http.connectionRequestTimeout=5000 30 | http.retryTime = 1 31 | http.maxLineLength = -1 32 | http.maxHeaderCount = 200 33 | http.maxTotal = 200 34 | http.defaultMaxPerRoute = 100 35 | http.soReuseAddress = false 36 | http.soKeepAlive = false 37 | http.timeToLive = 3600000 38 | http.keepAlive = 3600000 39 | http.keystore = 40 | http.keyPassword = 41 | # ssl 主机名称校验,是否采用default配置, 42 | # 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER 43 | http.hostnameVerifier = 44 | 45 | ##x-pack或者searchguard账号和口令 46 | test.elasticUser=elastic 47 | test.elasticPassword=changeme 48 | 49 | 50 | test.elasticsearch.rest.hostNames=192.168.137.1:9200 51 | 52 | test.elasticsearch.dateFormat=yyyy.MM.dd 53 | test.elasticsearch.timeZone=Asia/Shanghai 54 | 55 | #在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别 56 | test.elasticsearch.showTemplate=true 57 | test.elasticsearch.discoverHost=false 58 | 59 | ##default连接池配置 60 | test.http.timeoutConnection = 5000 61 | test.http.timeoutSocket = 50000 62 | test.http.connectionRequestTimeout=10000 63 | test.http.retryTime = 1 64 | test.http.maxLineLength = -1 65 | test.http.maxHeaderCount = 200 66 | test.http.maxTotal = 200 67 | test.http.defaultMaxPerRoute = 100 68 | test.http.soReuseAddress = false 69 | test.http.soKeepAlive = false 70 | test.http.timeToLive = 3600000 71 | test.http.keepAlive = 3600000 72 | test.http.keystore = 73 | test.http.keyPassword = 74 | # ssl 主机名称校验,是否采用default配置, 75 | # 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER 76 | test.http.hostnameVerifier = 77 | # dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制 78 | dslfile.refreshInterval = 3000 79 | 80 | 81 | ## 演示数据库数据导入elasticsearch源配置 82 | db.name = test 83 | db.user = root 84 | db.password = 123456 85 | db.driver = com.mysql.cj.jdbc.Driver 86 | #db.url = jdbc:mysql://192.168.137.1:3306/bboss?useCursorFetch=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true 87 | ## mysql插入批处理慢,可以尝试在链接后面加上参数:rewriteBatchedStatements=true 88 | db.url = jdbc:mysql://192.168.137.1:3306/bboss?useUnicode=true&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true 89 | db.usePool = true 90 | 91 | db.initSize=5 92 | db.minIdleSize=5 93 | db.maxSize=5 94 | 95 | 96 | db.validateSQL = select 1 97 | #db.jdbcFetchSize = 10000 98 | db.jdbcFetchSize = -2147483648 99 | db.showsql = true 100 | #db.dbtype = mysql -2147483648 101 | #db.dbAdaptor = org.frameworkset.elasticsearch.imp.TestMysqlAdaptor 102 | 103 | # 演示数据库数据导入elasticsearch源配置 104 | 105 | 106 | # 国产数据库达梦数据源配置,展示额外定制的达梦dbAdaptor, 107 | # 通过定制自己的dbAdaptor可以非常方便地实现bboss本身不支持的数据库的数据同步工作 108 | # /** 109 | # * dbtype专用于设置不支持的数据库类型名称和数据库适配器,方便用户扩展不支持的数据库的数据导入 110 | # * 可选字段,设置了dbAdaptor可以不设置dbtype,默认为数据库driver类路径 111 | # */ 112 | # private String dbtype ; 113 | # /** 114 | # * dbAdaptor专用于设置不支持的数据库类型名称和数据库适配器,方便用户扩展不支持的数据库的数据导入 115 | # * dbAdaptor必须继承自com.frameworkset.orm.adapter.DB或者其继承DB的类 116 | # 5.7.0后的版本达梦数据库已经内置到bboss中支持 117 | # db.dbtype = dm 118 | # db.dbAdaptor = org.frameworkset.elasticsearch.imp.DMAdaptor 119 | # */ 120 | #db.name = test 121 | #db.user = username 122 | #db.password = password 123 | #db.driver = dm.jdbc.driver.DmDriver 124 | #db.url = jdbc:dm://localhost:12345/dbname 125 | #db.usePool = true 126 | #db.validateSQL = select 1 127 | #db.jdbcFetchSize = 10000 128 | #db.showsql = true 129 | 130 | 131 | 132 | # 增量导入状态存储数据源配置,默认采用sqlite,增量导入装存储到本地的sqlite数据库中,采用分布式的外部定时任务引擎时, 133 | # 就不能将状态存储到本地,需要采用外部的数据库(mysql,oracle等)来存储增量导入状态。 134 | # 如果做了config.db配置,则采用配置的的数据源,必须指定创建statusTableName的建表语句,每种数据库对应的语法做适当调整 135 | # create table $statusTableName (ID number(2),lasttime number(10),lastvalue number(10),lastvaluetype number(1),PRIMARY KEY (ID)) 136 | # 137 | # 一般情况下不需要使用外部状态数据源,除非采用分布式的外部定时任务引擎, 138 | # 外部状态数据源可以直接使用上面的dbname为test的数据源: 139 | #config.db.name=test 140 | 141 | # 如果不引用别的数据源,可以参考如下配置,重新配置一个完整的外部数据源,dbname为testconfig,注意不能和上面的dbname重名 142 | #config.db.name = testconfig 143 | #config.db.user = root 144 | #config.db.password = 123456 145 | #config.db.driver = com.mysql.cj.jdbc.Driver 146 | #config.db.url = jdbc:mysql://192.168.137.1:3306/bboss?useUnicode=true&characterEncoding=utf-8&useSSL=false 147 | #config.db.usePool = true 148 | #config.db.validateSQL = select 1 149 | #config.db.jdbcFetchSize = 10000 150 | #config.db.showsql = true 151 | ### mysql 152 | #config.db.statusTableDML = CREATE TABLE $statusTableName ( ID bigint(10) NOT NULL AUTO_INCREMENT, lasttime bigint(10) NOT NULL, lastvalue bigint(10) NOT NULL, lastvaluetype int(1) NOT NULL, PRIMARY KEY(ID)) ENGINE=InnoDB 153 | 154 | quartzImportTask.crontime = */20 * * * * ? 155 | 156 | ip.serviceUrl = http://ip.taobao.com/service/getIpInfo.php 157 | ip.cachesize = 2000 158 | # 库下载地址https://dev.maxmind.com/geoip/geoip2/geolite2/ 159 | ip.database = d:/geolite2/GeoLite2-City.mmdb 160 | ip.asnDatabase = d:/geolite2/GeoLite2-ASN.mmdb 161 | 162 | ## 在数据导入过程可能需要使用的其他数据名称,需要在配置文件中定义相关名称的db配置 163 | thirdDatasources = postgres 164 | 165 | postgres.db.user = postgres 166 | postgres.db.password = 123456 167 | postgres.db.driver = org.postgresql.Driver 168 | #db.url = jdbc:mysql://192.168.137.1:3306/bboss?useUnicode=true&characterEncoding=utf-8&useSSL=false 169 | #postgres.db.url = jdbc:postgresql://192.168.137.1:5432/bboss?defaultRowFetchSize=1000 170 | postgres.db.url = jdbc:postgresql://192.168.137.1:5432/bboss 171 | postgres.db.usePool = true 172 | 173 | postgres.db.initSize=5 174 | postgres.db.minIdleSize=5 175 | postgres.db.maxSize=5 176 | 177 | 178 | postgres.db.validateSQL = select 1 179 | postgres.db.showsql = true 180 | postgres.db.jdbcFetchSize = 2000 181 | #db1.db.user = root 182 | #db1.db.password = 123456 183 | #db1.db.driver = com.mysql.cj.jdbc.Driver 184 | ##db.url = jdbc:mysql://192.168.137.1:3306/bboss?useUnicode=true&characterEncoding=utf-8&useSSL=false 185 | #db1.db.url = jdbc:mysql://192.168.137.1:3306/bboss?useUnicode=true&characterEncoding=utf-8&useSSL=false 186 | #db1.db.usePool = true 187 | #db1.db.validateSQL = select 1 188 | #db1.db.showsql = true 189 | ##db1.db.dbtype = mysql -2147483648 190 | ##db1.db.dbAdaptor = org.frameworkset.elasticsearch.imp.TestMysqlAdaptor 191 | 192 | #db2.db.user = root 193 | #db2.db.password = 123456 194 | #db2.db.driver = com.mysql.cj.jdbc.Driver 195 | ##db.url = jdbc:mysql://192.168.137.1:3306/bboss?useUnicode=true&characterEncoding=utf-8&useSSL=false 196 | #db2.db.url = jdbc:mysql://192.168.137.1:3306/bboss?useUnicode=true&characterEncoding=utf-8&useSSL=false 197 | #db2.db.usePool = true 198 | #db2.db.validateSQL = select 1 199 | ##db.jdbcFetchSize = 10000 200 | #db2.db.jdbcFetchSize = -2147483648 201 | #db2.db.showsql = true 202 | ##db2.db.dbtype = mysql -2147483648 203 | ##db2.db.dbAdaptor = org.frameworkset.elasticsearch.imp.TestMysqlAdaptor -------------------------------------------------------------------------------- /src/main/resources/dsl2ndSqlFile.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 11 | 12 | 58 | 59 | 62 | 63 | 75 | 76 | 79 | 80 | 86 | 87 | 90 | 91 | 101 | 102 | 103 | 106 | 107 | 108 | 109 | 110 | 113 | 114 | 125 | 126 | 127 | 128 | 129 | 141 | 142 | 143 | 156 | 157 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /src/main/resources/httpdsl.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 18 | 19 | 59 | 60 | 68 | 69 | 70 | 76 | 77 | 78 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/resources/org/frameworkset/task/quarts-task.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 27 | 51 | 52 | 58 | 64 | 70 | 71 | 72 | 73 | 74 | 75 | 79 | 83 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /src/main/resources/sql.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | #[log_id]]]> 9 | 10 | 11 | 14 | 15 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/test/java/org/frameworkset/elasticsearch/imp/CMSDocumentImportTest.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/10/16 19:05 23 | * @author biaoping.yin 24 | * @version 1.0 25 | */ 26 | public class CMSDocumentImportTest { 27 | public static void main(String[] args){ 28 | CMSDocumentImport cmsDocumentImport = new CMSDocumentImport(); 29 | cmsDocumentImport.scheduleImportData(true); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/org/frameworkset/elasticsearch/imp/DBTest.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.common.poolman.PreparedDBUtil; 19 | import com.frameworkset.common.poolman.util.SQLUtil; 20 | import org.frameworkset.spi.assemble.PropertiesContainer; 21 | 22 | import java.sql.SQLException; 23 | 24 | /** 25 | *

Description:

26 | *

27 | *

Copyright (c) 2018

28 | * @Date 2019/3/20 22:36 29 | * @author biaoping.yin 30 | * @version 1.0 31 | */ 32 | public class DBTest { 33 | public static void main(String[] args) throws SQLException { 34 | PropertiesContainer propertiesContainer = new PropertiesContainer(); 35 | propertiesContainer.addConfigPropertiesFile("application.properties"); 36 | String dbName = propertiesContainer.getProperty("db.name"); 37 | String dbUser = propertiesContainer.getProperty("db.user"); 38 | String dbPassword = propertiesContainer.getProperty("db.password"); 39 | String dbDriver = propertiesContainer.getProperty("db.driver"); 40 | String dbUrl = propertiesContainer.getProperty("db.url"); 41 | 42 | 43 | String validateSQL = propertiesContainer.getProperty("db.validateSQL"); 44 | 45 | 46 | String _jdbcFetchSize = propertiesContainer.getProperty("db.jdbcFetchSize"); 47 | Integer jdbcFetchSize = null; 48 | if(_jdbcFetchSize != null && !_jdbcFetchSize.equals("")) 49 | jdbcFetchSize = Integer.parseInt(_jdbcFetchSize); 50 | 51 | //启动数据源 52 | SQLUtil.startPool(dbName,//数据源名称 53 | dbDriver,//mysql驱动 54 | dbUrl,//mysql链接串 55 | dbUser,dbPassword,//数据库账号和口令 56 | validateSQL, //数据库连接校验sql 57 | jdbcFetchSize // jdbcFetchSize 58 | ); 59 | // List datas = SQLExecutor.queryList(Map.class,"select * from td_cms_document id = ?",0); 60 | // System.out.println(); 61 | // initData(); 62 | patchData(); 63 | } 64 | 65 | public static void patchData() throws SQLException { 66 | String sql = "INSERT INTO bigtable (col1, col2, col3) VALUES (?, ?, ?) "; 67 | // SQLExecutor.deleteWithDBName("test","delete from bigtable"); 68 | PreparedDBUtil dbUtil = new PreparedDBUtil(); 69 | dbUtil.preparedInsert("test", sql); 70 | StringBuilder builder = new StringBuilder(); 71 | for(long j = 99995001; j < 100000000l; j ++) { 72 | 73 | 74 | dbUtil.setString(1, builder.append(j).toString()); 75 | builder.setLength(0); 76 | dbUtil.setString(2, builder.append("sss").append(j).toString()); 77 | builder.setLength(0); 78 | dbUtil.setString(3, builder.append("ddd").append(j).toString()); 79 | builder.setLength(0); 80 | dbUtil.addPreparedBatch(); 81 | 82 | } 83 | dbUtil.executePreparedBatch(); 84 | 85 | } 86 | public static void initData() throws SQLException { 87 | String sql = "INSERT INTO bigtable (col1, col2, col3) VALUES (?, ?, ?) "; 88 | // SQLExecutor.deleteWithDBName("test","delete from bigtable"); 89 | PreparedDBUtil dbUtil = new PreparedDBUtil(); 90 | dbUtil.preparedInsert("test", sql); 91 | StringBuilder builder = new StringBuilder(); 92 | for(long j = 0; j < 100000000l; j ++) { 93 | 94 | 95 | dbUtil.setString(1, builder.append(j).toString()); 96 | builder.setLength(0); 97 | dbUtil.setString(2, builder.append("sss").append(j).toString()); 98 | builder.setLength(0); 99 | dbUtil.setString(3, builder.append("ddd").append(j).toString()); 100 | builder.setLength(0); 101 | dbUtil.addPreparedBatch(); 102 | if(j > 0 && j % 5000 == 0) { 103 | dbUtil.executePreparedBatch(); 104 | dbUtil = new PreparedDBUtil(); 105 | dbUtil.preparedInsert("test", sql); 106 | } 107 | } 108 | dbUtil.executePreparedBatch(); 109 | 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/test/java/org/frameworkset/elasticsearch/imp/DbdemoFromSQLFileTest.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/27 20:38 23 | * @author biaoping.yin 24 | * @version 1.0 25 | */ 26 | public class DbdemoFromSQLFileTest { 27 | public static void main(String args[]){ 28 | DbdemoFromSQLFile dbdemo = new DbdemoFromSQLFile(); 29 | boolean dropIndice = false;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值 30 | dbdemo.scheduleImportData( dropIndice); 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/org/frameworkset/elasticsearch/imp/DbdemoTest.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/27 20:38 23 | * @author biaoping.yin 24 | * @version 1.0 25 | */ 26 | public class DbdemoTest { 27 | public static void main(String args[]){ 28 | 29 | long t = System.currentTimeMillis(); 30 | Dbdemo dbdemo = new Dbdemo(); 31 | boolean dropIndice = true;//CommonLauncher.getBooleanAttribute("dropIndice",false);//同时指定了默认值 32 | // dbdemo.scheduleImportData( dropIndice);//定时增量导入 33 | // dbdemo.externalScheduleImportData(dropIndice);//外部定时器定时增量导入 34 | // dbdemo.externalFullScheduleImportData(dropIndice);//外部定时器定时增量导入 35 | // dbdemo.scheduleFullImportData(dropIndice);//定时全量导入 36 | dbdemo.scheduleTimestampImportData(dropIndice);//一次性全量导入 37 | 38 | // dbdemo.scheduleRefactorImportData(dropIndice);//定时全量导入,在context中排除remark1字段 39 | // dbdemo.fullAutoUUIDImportData(dropIndice); 40 | // dbdemo.scheduleFullAutoUUIDImportData(dropIndice);//定时全量导入,自动生成UUID 41 | // dbdemo.scheduleDatePatternImportData(dropIndice);//定时增量导入,按日期分表yyyy.MM.dd 42 | 43 | 44 | 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/org/frameworkset/elasticsearch/imp/ESDemoTest.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.client.ClientInterface; 20 | 21 | /** 22 | *

Description: 从es中查询数据导入数据库案例

23 | *

24 | *

Copyright (c) 2018

25 | * @Date 2019/1/11 14:39 26 | * @author biaoping.yin 27 | * @version 1.0 28 | */ 29 | public class ESDemoTest { 30 | public static void main(String[] args){ 31 | 32 | // 33 | // ES2DBDemo esDemo = new ES2DBDemo(); 34 | // // esDemo.directExport(); 35 | //// esDemo.exportData(); 36 | //// esDemo.exportSliceData(); 37 | //// esDemo.exportSliceDataWithInnerhit(); 38 | // esDemo.exportParallelData(); 39 | // //结束所以后台程序,退出,测试时打开 40 | // //BaseApplicationContext.shutdown(); 41 | 42 | ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil(); 43 | System.out.println(clientInterface.existIndice("不管")); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/org/frameworkset/elasticsearch/imp/QuartzTest.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.task.SchedulejobInfo; 19 | import org.frameworkset.task.TaskService; 20 | 21 | /** 22 | *

Description:

23 | *

24 | *

Copyright (c) 2018

25 | * @Date 2019/11/5 12:46 26 | * @author biaoping.yin 27 | * @version 1.0 28 | */ 29 | public class QuartzTest { 30 | public static void main(String[] args){ 31 | TaskService taskService = TaskService.getTaskService(); 32 | taskService.startService(); 33 | 34 | SchedulejobInfo jobinfo = new SchedulejobInfo(); 35 | jobinfo.setName("QuartzImportTask"); 36 | jobinfo.setId("QuartzImportTask"); 37 | 38 | jobinfo.setUsed(true); 39 | // jobinfo.setBeanName(jobPro.getStringExtendAttribute("bean-name")); 40 | QuartzImportTask quartzImportTask = new org.frameworkset.elasticsearch.imp.QuartzImportTask(); 41 | quartzImportTask.init(); 42 | jobinfo.setBean(quartzImportTask); 43 | jobinfo.setMethod("execute"); 44 | // jobinfo.setMethodConstruction(jobPro.getConstruction()); 45 | jobinfo.setShouldRecover(false); 46 | jobinfo.setCronb_time("*/20 * * * * ?"); 47 | //添加作业参数 48 | jobinfo.addParameter("jobid", "xxxxxxx"); 49 | taskService.startExecuteJob(jobinfo); 50 | // setParameters(jobPro, jobinfo); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/org/frameworkset/elasticsearch/imp/SqliteUpdate.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 com.frameworkset.common.poolman.SQLExecutor; 19 | import com.frameworkset.common.poolman.util.SQLUtil; 20 | 21 | import java.sql.SQLException; 22 | 23 | /** 24 | *

Description:

25 | *

26 | *

Copyright (c) 2020

27 | * @Date 2021/8/27 16:37 28 | * @author biaoping.yin 29 | * @version 1.0 30 | */ 31 | public class SqliteUpdate { 32 | public static void main(String[] args) throws SQLException { 33 | SQLUtil.startPool("test",//数据源名称 34 | "org.sqlite.JDBC",//oracle驱动 35 | "jdbc:sqlite:E:\\gatewayoom\\filelog\\filelog_importbak" ,//mysql链接串 36 | "root","123456",//数据库账号和口令 37 | "select 1 " //数据库连接校验sql 38 | ); 39 | SQLExecutor.updateWithDBName("test","update increament_tab set status = 0"); 40 | } 41 | } 42 | --------------------------------------------------------------------------------