laravel5.1+ElasticSearch+go-mysql-elasticsearch MySQL数据实时导入(mac)
来源:SegmentFault
2023-02-16 15:11:17
0浏览
收藏
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《laravel5.1+ElasticSearch+go-mysql-elasticsearch MySQL数据实时导入(mac)》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~
1. ElasticSearch安装
直接使用brew install elasticsearch 安装最新版本的es,基本没有障碍。
2.laravel5.1框架添加elasticsearch支持
在composer.json文件中添加elasticsearch-php依赖:
{ "require": { "elasticsearch/elasticsearch": "~5.0" } }
执行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 '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数据

理论要掌握,实操不能落!以上关于《laravel5.1+ElasticSearch+go-mysql-elasticsearch MySQL数据实时导入(mac)》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
版本声明
本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除

- 上一篇
- Laravel 5.4 migrate时报错: Specified key was too long error

- 下一篇
- Nodejs Express 连接Mongodb
评论列表
-
- 自信的灯泡
- 很棒,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢师傅分享文章!
- 2023-03-12 09:08:46
-
- 聪明的铃铛
- 这篇技术贴太及时了,太详细了,受益颇多,已加入收藏夹了,关注up主了!希望up主能多写数据库相关的文章。
- 2023-03-12 06:46:49
查看更多
最新文章
-
- 数据库 · MySQL | 5小时前 |
- MySQL数据归档方法与工具详解
- 494浏览 收藏
-
- 数据库 · MySQL | 5小时前 |
- MySQL常用命令20个必备操作指南
- 149浏览 收藏
-
- 数据库 · MySQL | 9小时前 |
- MySQL数据库详解:核心功能与优势全解析
- 236浏览 收藏
-
- 数据库 · MySQL | 14小时前 |
- MySQL基础命令详解,新手必看操作指南
- 436浏览 收藏
-
- 数据库 · MySQL | 16小时前 |
- MySQL中英文界面切换方法详解
- 366浏览 收藏
-
- 数据库 · MySQL | 18小时前 |
- MySQL数据库特点与优势详解
- 194浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL性能监控设置与核心指标解析
- 252浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL中文乱码解决方法汇总
- 119浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL增删改查语法速查表大全
- 434浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- 主键外键关系详解:MySQL关联解析
- 191浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL安装D盘配置教程
- 120浏览 收藏
-
- 数据库 · MySQL | 1天前 |
- MySQL中WHERE与HAVING的区别详解
- 304浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 511次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 498次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- 千音漫语
- 千音漫语,北京熠声科技倾力打造的智能声音创作助手,提供AI配音、音视频翻译、语音识别、声音克隆等强大功能,助力有声书制作、视频创作、教育培训等领域,官网:https://qianyin123.com
- 231次使用
-
- MiniWork
- MiniWork是一款智能高效的AI工具平台,专为提升工作与学习效率而设计。整合文本处理、图像生成、营销策划及运营管理等多元AI工具,提供精准智能解决方案,让复杂工作简单高效。
- 227次使用
-
- NoCode
- NoCode (nocode.cn)是领先的无代码开发平台,通过拖放、AI对话等简单操作,助您快速创建各类应用、网站与管理系统。无需编程知识,轻松实现个人生活、商业经营、企业管理多场景需求,大幅降低开发门槛,高效低成本。
- 226次使用
-
- 达医智影
- 达医智影,阿里巴巴达摩院医疗AI创新力作。全球率先利用平扫CT实现“一扫多筛”,仅一次CT扫描即可高效识别多种癌症、急症及慢病,为疾病早期发现提供智能、精准的AI影像早筛解决方案。
- 231次使用
-
- 智慧芽Eureka
- 智慧芽Eureka,专为技术创新打造的AI Agent平台。深度理解专利、研发、生物医药、材料、科创等复杂场景,通过专家级AI Agent精准执行任务,智能化工作流解放70%生产力,让您专注核心创新。
- 253次使用
查看更多
相关文章
-
- golang MySQL实现对数据库表存储获取操作示例
- 2022-12-22 499浏览
-
- 搞一个自娱自乐的博客(二) 架构搭建
- 2023-02-16 244浏览
-
- B-Tree、B+Tree以及B-link Tree
- 2023-01-19 235浏览
-
- mysql面试题
- 2023-01-17 157浏览
-
- MySQL数据表简单查询
- 2023-01-10 101浏览