1. ElasticSearch安裝
直接使用brew install elasticsearch 安裝最新版本的es,基本沒有障礙。2.laravel5.1框架添加elasticsearch支持
在composer.json文件中添加elasticsearch-php依賴:
{ "require": { "elasticsearch/elasticsearch": "~5.0" } }
執(zhí)行composer命令更新es代碼加載到當(dāng)前l(fā)aravel框架中
composer update
laravel控制器中使用es
use ElasticsearchClientBuilder;//引入 $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對(duì)應(yīng)的數(shù)據(jù) print_r($response); ……
實(shí)例:
client = ClientBuilder::create()->build(); } /** * Display a listing of the resource. * * @return IlluminateHttpResponse */ 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 IlluminateHttpResponse */ 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. 實(shí)時(shí)導(dǎo)入插件go-mysql-elasticsearch
安裝go環(huán)境,設(shè)置環(huán)境變量
brew install go vi ~/.bash_profile 添加以下PATH和GOPATH,并在命令行中執(zhí)行這兩條命令 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數(shù)據(jù)庫 # mysqldump execution path # if not set or empty, ignore mysqldump. mysqldump = "mysqldump" # MySQL data source [[source]] schema = "dev_cms"#數(shù)據(jù)庫名稱 # 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"]#數(shù)據(jù)表名稱,可以多個(gè) # 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"#數(shù)據(jù)庫名稱 table = "test_content"#數(shù)據(jù)表名稱 index = "es_dev_cms"#生成es數(shù)據(jù)索引名稱,對(duì)應(yīng)schema type = "es_test_content"#生成es數(shù)據(jù)類型,對(duì)應(yīng)table # title is MySQL test_river field name, es_title is the customized name in Elasticsearch [[rule.fields]] mysql = "title" elastic = "es_title"
執(zhí)行命令,導(dǎo)入數(shù)據(jù)
cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch ./bin/go-mysql-elasticsearch -config=./etc/river.toml &
可以在谷歌瀏覽器安裝sense插件查看es數(shù)據(jù)
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/22380.html
摘要:配置文件其中有兩個(gè)關(guān)鍵的配置和。啟動(dòng)如上即為正常運(yùn)行。因?yàn)槲以趩?dòng)后一直報(bào)錯(cuò),,各種嘗試最后報(bào)錯(cuò)依然存在,只好換用部署了。安裝部署安裝和插件獲取驅(qū)動(dòng)下載配置配置使用時(shí)自行把下面注釋去掉。Author : Ali0th Date : 20190514 最近用go語言寫了個(gè)爬蟲,爬了幾百萬條數(shù)據(jù),存在 mysql 里,數(shù)據(jù)量較大,一個(gè)表就一兩G的程度(mysql表一般不要超過2G)。 sho...
閱讀 3694·2021-11-22 15:24
閱讀 1599·2021-09-26 09:46
閱讀 1912·2021-09-14 18:01
閱讀 2610·2019-08-30 15:45
閱讀 3528·2019-08-30 14:23
閱讀 1874·2019-08-30 12:43
閱讀 2917·2019-08-30 10:56
閱讀 803·2019-08-29 12:20