安装elastic & LogStash & kibana:
https://cloud.tencent.com/developer/article/1900966
其中 LogStash 创建的配置文件最好按以下格式:
input { jdbc { # mysql 数据库链接,test为数据库名 jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/abc" # 用户名和密码 jdbc_user => "root" jdbc_password => "123456" # 驱动 jdbc_driver_library => "/usr/share/logstash/logstash-core/lib/jars/mysql-connector-java-5.1.38/mysql-connector-java-5.1.38-bin.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" # 是否分页 jdbc_paging_enabled => "true" #每页获取数量,可以根据性能按需修改 jdbc_page_size => "20000" # 直接执行sql语句 statement => "SELECT id,realname,nickname,pinyinName,tag,english_name from * where id <= 17" # 是否将字段名转换为小写,默认true(如果有数据序列化、反序列化需求,建议改为false); lowercase_column_names => false # 是否记录上次执行结果,true表示会将上次执行结果的tracking_column字段的值保存到last_run_metadata_path指定的文件中 record_last_run => true # 需要记录查询结果某字段的值时,此字段为true,否则默认tracking_column为timestamp的值 use_column_value => true # 查询结果某字段的数据类型,仅包括numeric和timestamp,默认为numeric tracking_column => "id" # 记录上次执行结果数据的存放位置 last_run_metadata_path => "./last_id.txt" # 是否清除last_run_metadata_path的记录,需要增量同步时此字段必须为false clean_run => false # 要执行的sql文件 # statement_filepath => "/softwares/logstash-7.12.1/config/export-data.sql" # 定时配置 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新 #schedule => "* * * * *" } } output { elasticsearch { # es的ip和端口 hosts => ["127.0.0.1:9200"] # ES索引库名称 index => "test" # 文档类型 # document_type => "_doc" # 设置数据的id为数据库中的字段 document_id => "%{id}" } stdout { #日志输出 codec => json_lines } }
这块不如直接用 代码进行数据迁移至 Es。
function elasticAdd() { $data = $this->lk_common->getTableValue('table',"id>0",'column'); $index = "test";//集合 $type = "doc";//表 $base_url = "http://localhost:9200"; // Elasticsearch base URL foreach ($data as $v){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "$base_url/$index/$type/{$v['id']}"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($v)); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen(json_encode($v)) )); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); if ($response === false) { echo "cURL Error: " . curl_error($ch); } else { echo "Document added successfully: $response"; } curl_close($ch); } }
按需。
kibana:
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.16-linux-x86_64.tar.gz tar -zxvf kibana-5.6.16-linux-x86_64.tar.gz -C /usr/local/ cd /usr/local/kibana-5.6.16-linux-x86_64/config/ #修改es连接信息等。默认可不改 vim kibana.yml # 默认值5601,没有需要可以不修改 server.port: 5601 # 允许远程访问,也可以直接设置为“0.0.0.0” server.host: "0.0.0.0" # 默认值http://localhost:9200 elasticsearch.url: "http://127.0.0.1:9200" cd .. ./bin/kibana
如果是在Linux-nginx,
IP+端口 打不开时;可用域名配置反向代理调试。
分词:
https://developer.aliyun.com/article/848626
原创文章,转载请标明出处!