安装需要可软件

  1. sudo apt-get install python-pip
  2. sudo pip install elasticsearch;
  3. sudo apt-get install python-dev
  4. sudo pip install MySQL-python

导入脚本import.sh

  1. #!/bin/bash
  2. set -e
  3. bin=/usr/local/elasticsearch-jdbc-1.5.2.0/bin
  4. lib=/usr/local/elasticsearch-jdbc-1.5.2.0/lib
  5. echo '{
  6. "type" : "jdbc",
  7. "jdbc" : {
  8. "url" : "jdbc:mysql://192.168.10.29:3306/db_1",
  9. "user" : "root",
  10. "password" : "root",
  11. "sql" : "select * from '${1}' where dtTime>\"'${2}'\" ",
  12. "index": "db_1",
  13. "type": "'${1}'"
  14. }
  15. }' | java \
  16. -cp "${lib}/*" \
  17. -Dlog4j.configurationFile=${bin}/log4j2.xml \
  18. org.xbib.tools.Runner \
  19. org.xbib.tools.JDBCImporter
  20. if [ $? != 0 ];then
  21. exit -1
  22. fi

python调用import.sh实现增量添加:

  1. #!/usr/bin/env python
  2. from datetime import datetime
  3. from elasticsearch import Elasticsearch
  4. import MySQLdb
  5. import time
  6. import os
  7. import subprocess
  8. es=Elasticsearch("192.168.10.29")
  9. def now():
  10. return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
  11. def getLastTime(tableName):
  12. global es
  13. q={
  14. "aggs":
  15. {
  16. "max":{
  17. "max":{"field":"dtTime"}
  18. }
  19. }
  20. }
  21. dt=es.search(index="db_1",doc_type=tableName,body=q)['aggregations']['max']['value']
  22. if dt is None:
  23. return '2015-01-01 00:00:00'
  24. return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(dt/1000))
  25. def insert(tableName,dtLastTime):
  26. global es
  27. print tableName+" startTime:"+str(dtLastTime)
  28. print '/usr/local/elasticsearch-jdbc-1.5.2.0/bin/import.sh %s "%s"'%(tableName,str(dtLastTime))
  29. retCode = subprocess.call('/usr/local/elasticsearch-jdbc-1.5.2.0/bin/import.sh %s "%s"'%(tableName,str(dtLastTime)),shell=True)
  30. if retCode!=0:
  31. print "Import failed"
  32. return
  33. print "%s Import finished"%(now())
  34. es.indices.refresh(index="db_1")
  35. def increment():
  36. conn=MySQLdb.connect(host='192.168.10.29',port=3306,user='root',passwd='root',db ='db_1',)
  37. cur=conn.cursor()
  38. ret=cur.execute('select vTableName,dtLastTime from importinfo')
  39. ret=cur.fetchall()
  40. for line in ret:
  41. tableName=line[0]
  42. fileName=line[1].strftime("%Y-%m-%d-%H-%M-%S")
  43. dtLastTime=getLastTime(tableName)
  44. insert(tableName,dtLastTime)
  45. cur.close()
  46. conn.close()
  47. if __name__=="__main__":
  48. increment()
  49. #getLastTime("achi")

es增量自定义更新的脚本的更多相关文章

  1. 【Quick 3.3】资源脚本加密及热更新(一)脚本加密

    [Quick 3.3]资源脚本加密及热更新(一)脚本加密 注:本文基于Quick-cocos2dx-3.3版本编写 一.脚本加密 quick框架已经封装好加密模块,与加密有关的文件在引擎目录/quic ...

  2. CodePush自定义更新弹框及下载进度条

    CodePush 热更新之自定义更新弹框及下载进度 先来几张弹框效果图 非强制更新场景 image 强制更新场景 image 更新包下载进度效果 image 核心代码 这里的热更新Modal框,是封装 ...

  3. 自定义nagios监控脚本---磁盘检测

    自定义nagios监控脚本---磁盘检测 1. 在客户端上创建脚本/usr/local/nagios/libexec/check_disk.shvim /usr/local/nagios/libexe ...

  4. 实现将机器A上的程序包复制到机器B并更新的脚本

    一.前言 之前有写过如何在单台服务器上执行脚本自动更新程序包,但平时测试过程中相信大部分公司都是需要测试人员在服务器A上进行功能测试,测试通过后再将程序包更新到服务器B上进行安全测试或者性能测试:今天 ...

  5. Sphinx 增量索引更新

    是基于PHP API调用,而不是基于sphinxSE.现在看来sphinxSE比API调用更简单的多,因为之前没有想过sphinxSE,现在先把API的弄明白.涉及到的:sphinx 数据源的设置,简 ...

  6. Elasticsearch mysql 增量同步 三表联合 脚本

    在上一篇中简略的说了一下es同步数据脚本的大致情况,但是实际情况里肯定不会像上一篇里面的脚本那么简单.比如目前我就有三张表,两张实体表,一张关联表.大致实现如下: bin目录建立一个statefile ...

  7. Zabbix日志监视的汇总报警(更新发送邮件脚本)

    Zabbix的用户一定会碰到这种情况: 日志报警一般设置的是multiple模式,有错误大量写入的时候,每写入一行就会触发一次action,导致出现大量的报警邮件. 特别是ora的报警,经常一出就是上 ...

  8. Unity3D热更新全书-脚本(五) NGUI

    让我们实际的研究一下如何将NGUI和C#LightEvil结合起来. 这里使用NGUI2.7,因为他是一个开源的版本,NGUI最新的版本未经作者的许可,是不可以带入我们的开源项目使用的. 这个例子完成 ...

  9. Unity3D热更新全书-脚本(一) 初识脚本

    开篇之前还是要先说明,这是一份给经验并不丰富的程序员阅读的文字. 有需求.有疑惑,往下看. 第一个问题什么是脚本?程序和脚本如何区分?我们给Unity编写的组件是程序还是脚本? 这些问题本文无意去解答 ...

随机推荐

  1. react相关知识点

    1.react内联样式写法: <div style={{width:'200px',height:'100px',border:'1px solid red'}}> </div> ...

  2. Jfinal基础学习(一)

    我负责的项目都是Jfinal的了,有必要写一些学习知识,记录下来. 1.PropKit.use("config.txt", "UTF-8"); PropKit ...

  3. servlet & filter & listener & interceptor

    web.xml 的加载顺序是:context- param -> listener -> filter -> servlet * Servlet 对URL生效,用户处理用户的URL请 ...

  4. WCF学习心得------(三)配置服务

    配置服务 配置服务概述 在设计和实现服务协定后,便可以进行服务的配置.在其中可以定义和自定义如何向客户段公开服务,包括指定可以找到服务的地址,服务用于发送和接受消息的传输和消息编码,以及服务需要的安全 ...

  5. centos 安装sphinx

    官网下载 :http://sphinxsearch.com/downloads/sphinx-2.2.10-release.tar.gz/thankyou.html 安装sphinx 解压 tar z ...

  6. matlab 画三维图函数

    matlab三维绘图 http://blog.sina.com.cn/s/blog_6d5ffd0d0100lyah.html Matlab绘图系列之高级绘图 http://blog.163.com/ ...

  7. LeetCode "Count of Smaller Number After Self"

    Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...

  8. Redis容灾部署(哨兵Sentinel)

    Redis容灾部署(哨兵Sentinel) 哨兵的作用 1. 监控:监控主从是否正常2. 通知:出现问题时,可以通知相关人员3. 故障迁移:自动主从切换4. 统一的配置管理:连接者询问sentinel ...

  9. 51nod 1056

    n<=10000000000 然后欧拉函数的前缀和可以用莫比乌斯函数的前缀和快速求,注意各种取模 #include<cstdio> typedef long long i64; ,P ...

  10. php接二进制文件

    PHP默认只识别application/x-www.form-urlencoded标准的数据类型. 因此,对型如text/xml 或者 soap 或者 application/octet-stream ...