es导出mysql_es 数据 导出 到 MySQL

本文介绍了如何使用elasticdump工具将Elasticsearch的数据导出到JSON文件,然后通过脚本程序导入到MySQL数据库。详细步骤包括elasticdump的安装、使用方法以及数据迁移过程。
摘要由CSDN通过智能技术生成

暂时没有找到直接 导出到 mysql 数据库的工具 或者项目

目前实现思路: 使用 elasticdump  工具 实现 从 es 数据 导出到 json 文件 ,然后 使用 脚本程序 操作 改 json 文件 实现 导入到MySQL 数据

具体内容如下:

安装elasticdump

1 npm install elasticdump -g elasticdump 全局安装

2 从 github 下载 对应的版本  比如 2.4.*

具体参考 https://www.npmjs.com/package/elasticdump

github 地址 https://github.com/taskrabbit/elasticsearch-dump

安装完成后,就可以使用了,下面举一个例子(将索引a中的数据导入到索引b中):

elasticdump  用法详解

./elasticdump --input=http://127.0.0.1:9200/domain6 --output=query.json --debug=true --limit=1 --offset=0 --searchBody=‘{"query":{"range":{"id":{"lte":"100"}}}}‘ --sourceOnly=true

// 获取 所需的字段值

./elasticdump --input=http://127.0.0.1:9200/domain6 --output=query2.json --limit=100 --offset=0 --searchBody=‘{"query":{"range":{"id":{"lte":"80"}}},"_source":["id","cname"]}‘ --sourceOnly=true

//问题 类似 es  size 的功能 暂时还没找到方案

./elasticdump --input=http://127.0.0.1:9200/domain6 --output=query3.json --limit=100 --offset=0 --searchBody=‘{"query":{"range":{"id":{"lte":"80"}}},"_source":["id","cname"]}‘ --sourceOnly=true

--input 原数据源

--output 导出数据地址

--limit (不是导出总条数 和 es 有区别) 每次执行 导入的数据条数

--offset  相当于 es 里面 的from

--searchBody  查询语句

--sourceOnly  导出数据格式 默认会把 _type _id _source _index 都导出 默认 false

elasticdump --input=http://localhost:9200/a --output=http://localhost:9200/b --type=data

elasticdump --input=http://localhost:9200/domain_v12 --output=D:/ziliao/elasticsearch-2.3.3/dd --type=data

elasticdump --input=http://localhost:9200/domain_v12 --limit=10 --output=D:/ziliao/elasticsearch-2.3.3/dd4.json  --type=data

执行数据迁移

导出Mapping信息

elasticdump --ignore-errors=true  --scrollTime=120m  --bulk=true --input=http://10.10.20.164:9200/xmonitor-2015.04.29   --output=http://192.168.100.72:9200/xmonitor-prd-2015.04.29  --type=mapping

导出数据

elasticdump --ignore-errors=true  --scrollTime=120m  --bulk=true --input=http://10.10.20.164:9200/xmonitor-2015.04.28   --output=/usr/local/esdump/node-v0.12.2-linux-x64/data/xmonitor-prd-2015.04.28.json --type=data

导出数据到本地集群

elasticdump --ignore-errors=true  --scrollTime=120m  --bulk=true --input=http://10.10.20.164:9200/xmonitor-2015.04.29   --output=http://192.168.100.72:9200/xmonitor-prd-2015.04.29 --type=data

var defaults = {

limit:              10,

offset:             0,

debug:              false,

type:               ‘data‘,

delete:             false,

maxSockets:         null,

input:              null,

‘input-index‘:      null,

output:             null,

‘output-index‘:     null,

inputTransport:     null,

outputTransport:    null,

searchBody:         null,

sourceOnly:         false,

jsonLines:          false,

format:             ‘‘,

‘ignore-errors‘:    false,

scrollTime:         ‘10m‘,

timeout:            null,

toLog:              null,

quiet:              false,

awsAccessKeyId:     null,

awsSecretAccessKey: null,

};

npm install elasticdump2.1.0 -g

elasticdump

npm install elasticdump@2.*

从json文件  导入 MySQL

$json_data = file_get_contents(‘D:/ziliao/node_modules/elasticdump/bin/query1223.json‘);(json 文件 所有路径)

$len = strlen($json_data);

$begin = 0;

$end = 0;

$data = [];

for ($i=0; $i < $len; $i++) {

if($json_data[$i]=="}")

{

$end = $i;

$lens = $end-$begin+1;

$data[] = json_decode(substr($json_data,$begin ,$lens),true);

$begin = $end+1;

}

}

// print_r($data);

// 链接 MySQL 数据库 配置

$con = mysql_connect("localhost","root","");

if (!$con)

{

die(‘Could not connect: ‘ . mysql_error());

}

mysql_select_db("adbug", $con);

foreach ($data as $key => $value) {

$sql = "INSERT INTO test2 (name) VALUES (‘".$value[‘host‘]."‘)";

mysql_query($sql);

}

mysql_close($con);

?>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ELK是指Elasticsearch、Logstash和Kibana三个开源软件的组合,可以用来处理和可视化大规模数据。在ELK中,Logstash用于数据收集、处理和转换,Elasticsearch用于数据存储和检索,Kibana用于数据可视化。 以下是将MySQL数据导出Elasticsearch的大致步骤: 1. 安装Elasticsearch、Logstash和Kibana。 2. 创建一个Logstash配置文件,指定MySQL作为数据源,并将数据导入到Elasticsearch中。例如,以下是一个简单的Logstash配置文件示例: ``` input { jdbc { jdbc_connection_string => "jdbc:mysql://localhost:3306/mydatabase" jdbc_user => "myuser" jdbc_password => "mypassword" jdbc_driver_library => "/path/to/mysql-connector-java.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" statement => "SELECT * from mytable" } } output { elasticsearch { hosts => ["localhost:9200"] index => "myindex" document_type => "_doc" } } ``` 在这个配置文件中,我们使用jdbc插件来读取MySQL中的数据,然后使用elasticsearch插件将数据写入到Elasticsearch中。 3. 运行Logstash,让它开始读取MySQL中的数据并将数据导入到Elasticsearch中。你可以通过以下命令来启动Logstash: ``` bin/logstash -f /path/to/config/file.conf ``` 这里的`/path/to/config/file.conf`是你的Logstash配置文件的路径。 4. 检查Elasticsearch中是否已经成功导入了MySQL中的数据。你可以使用Kibana等工具来浏览和查询Elasticsearch中的数据。 需要注意的是,这只是一个简单的示例,你需要根据自己的具体情况来设置Logstash的配置文件。同时,也需要确保MySQLElasticsearch都已经正确地安装和配置好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值