• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

laravel5+ElasticSearch+go-mysql-elasticsearchMySQL数据实时导入(mac)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1. ElasticSearch安装

直接使用brew install elasticsearch 安装最新版本的es,基本没有障碍。

2.Laravel5 框架添加elasticsearch支持

在composer.json文件中添加elasticsearch-php依赖:

 

 

 执行composer命令更新es代码加载到当前laravel框架中

  composer update

  laravel控制器中使用es

  use Elasticsearch\ClientBuilder;//引入
  $client = ClientBuilder::create()->build();//初始化
  $params = [
  'index' => 'my_index',
  'type' => 'my_type',
  'id' => 'my_id',
  'body' => ['testField' => 'abc']
  ];

  $response = $client->index($params);//添加索引
  print_r($response);

  $params = [
  'index' => 'my_index',
  'type' => 'my_type',
  'id' => 'my_id'
  ];

  $response = $client->get($params);//获取my_id对应的数据
  print_r($response);
实例:

  

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Elasticsearch\ClientBuilder;



class VideoController extends Controller
{
private $client;

public function __construct()
{
$this->client = ClientBuilder::create()->build();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
for ($i=0;$i<5000;$i++) {
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id' . $i,
'body' => ['testField' => $i . '、Kitty,今天气很好!!!']
];
$response = $this->client->index($params);
}

dd($response);
}

/**
* Display the specified resource.
*
* @param string $str
* @return \Illuminate\Http\Response
*/
public function show($str='abc')
{
// $params = [
// 'index' => 'my_index',
// 'type' => 'my_type',
// 'id' => $id
// ];
$params = [
'index' => 'my_index',
'type' => 'my_type',
//"scroll" => "3s",
"from" => 0,
"size" => 100,
'body' => [
'query' => [
'match' => [
'testField' => $str,
]
]
]
];

$response = $this->client->search($params);
dd($response);
}

}

3. 实时导入插件go-mysql-elasticsearch

安装go环境,设置环境变量

  brew install go
  vi ~/.bash_profile 添加以下PATH和GOPATH,并在命令行中执行这两条命令
  export PATH=$PATH:/usr/local/Cellar/go/1.7.4_2/bin/
  export GOPATH=/Users/User/go

获取go-mysql-elasticsearch

  go get github.com/siddontang/go-mysql-elasticsearch

编译go-mysql-elasticsearch

  cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch
  make

配置elasticsearch的etc目录下的river.toml

# MySQL address, user and password
# user must have replication privilege in MySQL.
my_addr = "192.168.0.1:3306"#mysql地址
my_user = "test"#用户名
my_pass = "test123"#密码

# Elasticsearch address
es_addr = "127.0.0.1:9200"#es安装地址

# Path to store data, like master.info, and dump MySQL data
data_dir = "./var"

# Inner Http status address
stat_addr = "127.0.0.1:12800"

# pseudo server id like a slave
server_id = 1001

# mysql or mariadb
flavor = "mysql"#mysql数据库

# mysqldump execution path
# if not set or empty, ignore mysqldump.
mysqldump = "mysqldump"

# MySQL data source
[[source]]
schema = "dev_cms"#数据库名称

# Only below tables will be synced into Elasticsearch.
# "test_river_[0-9]{4}" is a wildcard table format, you can use it if you have many sub tables, like table_0000 - table_1023
# I don't think it is necessary to sync all tables in a database.
tables = ["test_content"]#数据表名称,可以多个

# Below is for special rule mapping
#[[rule]]
#schema = "test"
#table = "test_river"
#index = "river"
#type = "river"
# only MySQL field in filter will be synced
#filter=["title","tags"]
# title is MySQL test_river field name, es_title is the customized name in Elasticsearch
#[rule.field]
# This will map column title to elastic search my_title
#title="es_title"
# This will map column tags to elastic search my_tags and use array type
#tags="my_tags,list"
# This will map column keywords to elastic search keywords and use array type
#keywords=",list"

# wildcard table rule, the wildcard table must be in source tables
[[rule]]
schema = "dev_cms"#数据库名称
table = "test_content"#数据表名称
index = "es_dev_cms"#生成es数据索引名称,对应schema
type = "es_test_content"#生成es数据类型,对应table

# title is MySQL test_river field name, es_title is the customized name in Elasticsearch
[[rule.fields]]
mysql = "title"
elastic = "es_title"

执行命令,导入数据

cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch
./bin/go-mysql-elasticsearch -config=./etc/river.toml &

可以在谷歌浏览器安装sense插件查看es数据

有需要交流的小伙伴可以点击这里加本人QQ:luke


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
GO语言学习(十四)Go 语言数组发布时间:2022-07-10
下一篇:
go语言基础之go猜数字游戏发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap