You are on page 1of 10

ໍ້ ນຈາກ SQL table ໄປ index ES

Logstash ດຶງຂມູ

ສິ່ ງທິ່ ຕໍ້ອງຕຽມ:


ໍ້ ນທິ່ ສໍ້າງແລໍ້ວ: ຊິ່ database , ຊິ່ Table
1. ຖານຂມູ
2. Index ໃນ Elasticsearch ທິ່ ສໍ້າງແລໍ້ວ
3. Mysql-connector-java ໂຫລດຕາມລງໍ້
Maven Repository: mysql » mysql-connector-java » 8.0.30 (mvnrepository.com)

MySQL :: Download Connector/J

ໍ້
ຂັນຕອນການເຮັ
ດ:

• ແຕກໄຟຣ logstash ທິ່ ໂຫລດມາ

ເຂາໍ້ logstash > config


ໍ້ ິ່ ແຕິ່ ຕໍ້ອງ .conf
• ສໍ້າງໄຟຣໃຫມິ່ ຕັງຊ
ໍ້
ເຂາໄປ config ໄຟຣທິ່ ສໍ້າງ

ໍ້
ຈະມ 3 ຂັນຕອນ

Input {} , Filter {} , Output {}

1
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost:3306/my_database"
jdbc_user => "root"
jdbc_password => ""
jdbc_driver_library => "D:\ELASTICSEARCH\logstash-conf\mysql-connector-
java-8.0.30.jar"
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
statement => "SELECT * FROM products"
}
}

ໍ້ າເຊິ່ ອມ database / host:post/your_database


ຕັງຄິ່

User , password

Driver_library ແມິ່ ນ directory file: Mysql-connector-java


ໍ້ ນ
Driver_class ຄິ່ າຕັງຕ ໍ້

Statement: ເລອກ Table

filter {
grok {
match => {

}
}
}
ໍ້ ນ ຫລ ຈັດການ ID ຫ ລາຍລະອຽດ ບິ່ ໃຫໍ້ຊາກັ
ກອງຂມູ ໍ້ ນ ຖໍ້າເຄຍ pipeline ແລໍ້ວ

2
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "testcsv"
user => "elastic"
password => "E2HC=MBvYnpnDe*ZUZTX"
}
stdout { codec => json_lines }
}

Hosts ຈາກ Elasticsearch

Index ທິ່ ໄດໍ້ສາໍ້ ງໄວໍ້ໃນ ES


ໍ້
User: ຄິ່ າພນຖານ elastic

Password: ຂອງ ES

- Config ແລໍ້ວ Save

ໍ້
ຂັນຕອນ run file .conf

** Run Command Prompt: as administrator ເທິ່ ານັນໍ້

ໃສິ່ ຄາສັິ່ງ set ເວລາກິ່ ອນ ຈັິ່ງ run file .conf

set TZ=Asia/Bangkok
D:\ELASTICSEARCH\logstash-8.9.1-windows-x86_64\logstash-8.9.1\bin\logstash -f
D:\ELASTICSEARCH\logstash-8.9.1-windows-x86_64\logstash-8.9.1\config\ຊິ່ ທິ່ ຕັງ.conf
ໍ້

3
Run ທິ່ ຢູິ່ໃນ folder bin > logstash -f

ແລໍ້ວເລອກ ທິ່ ຢູິ່ຂອງ ໄຟຣ.conf

ການດ .conf sync database


ALTER TABLE products
ADD last_modified TIMESTAMP NOT NULL DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

Add ເວລາໃສິ່ table ໃຫໍ້ເຊັກເວລາ


input {
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost:3306/my_database"
jdbc_user => "root"
jdbc_password => ""
jdbc_driver_library => "D:\ELASTICSEARCH\logstash-conf\mysql-connector-
java-8.0.30.jar"
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
statement => "SELECT * FROM products WHERE last_modified >
:sql_last_value"
use_column_value => true
tracking_column => "last_modified"
tracking_column_type => "timestamp"
#schedule => "* * * * *"
}
}
Statement: ດຶງເວລາສະເພາະປິ່ຽນແປງ

Use_column_value: ຕວດສອບການປິ່ຽນແປງ

Tracking_coloumn: ຕດຕາມການປິ່ຽນແປງ

Tracking_column_type: ຕດຕາມ timestamp

Schedule: ຈັດເວລາໃຫໍ້ loop ທຸກໆ 1 ນາທ

Filter{}

4
Output: ແຕິ່ ລະແບບ

output {
elasticsearch {
hosts => ["localhost:9200"]
index => "testcsv"
user => "elastic"
password => "E2HC=MBvYnpnDe*ZUZTX"
document_id => "%{id}" # ใช้ค่า ID จาก SQL เป็นคีย์ของเอกสารใน
Elasticsearch
action => "create" # ใช้การสร้างเอกสารใน Elasticsearch
document_type => "_doc"
}
stdout { codec => json_lines }
}
ແບບກາລດ ID index ໃຫໍ້ ຕງກັບ ID SQL

output {
elasticsearch {
hosts => ["localhost:9200"]
index => "testcsv"
user => "elastic"
password => "E2HC=MBvYnpnDe*ZUZTX"
document_id => "%{id}" # ใช้ค่า ID จาก SQL เป็นคีย์ของเอกสารใน
Elasticsearch
action => "create" # ใช้การสร้างเอกสารใหม่ใน Elasticsearch
document_id => "%{id}" # ใช้ค่า ID จาก SQL เป็นคีย์ของเอกสารใน
Elasticsearch
}
stdout { codec => json_lines }
}
ແບບ index ສໍ້າງ ID ໃຫໍ້ 20 ໂຕ

5
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "testcsv"
user => "elastic"
password => "E2HC=MBvYnpnDe*ZUZTX"
document_id => "%{id}" # ใช้ค่า ID จาก SQL เป็นคีย์ของเอกสารใน Elasticsearch
action => "update" # ใช้การอัปเดตเอกสารใน Elasticsearch
doc_as_upsert => true # ให้เพิ่มเอกสารใหม่หากไม่พบเอกสารที่ตรงกับ document_id
}
stdout { codec => json_lines }
}
ໍ້ ນໃນ SQL ແລໍ້ວ run .conf ຂມູ
ຖໍ້າ update ຂມູ ໍ້ ນໃນ index ກຈະປິ່ຽນແຕິ່ ID ເກິ່ າ

** ກລະນ pipeline error ໃຫໍ້ check ແຕິ່ ລະສິ່ ວນ

ສິ່ ວນ input {}

ສິ່ ວນ filter {}

ສິ່ ວນ output {}

ຫ check timezone TZ

** Load java ບິ່ ຮູ ວ


ໍ້ ິ່ າກິ່ ຽວກັນບິ່

jdk-20_windows-x64_bin.exe

jre-8u381-windows-x64.exe

6
logstash.yml

# This defaults to the number of the host's CPU cores.


#
pipeline.workers: 2
#
# How many events to retrieve from inputs before sending to
filters+workers
#
pipeline.batch.size: 125
#
# How long to wait in milliseconds while polling for the next
event
# before dispatching an undersized batch to filters+outputs
#
pipeline.batch.delay: 50

# "false" disables any extra processing necessary for


preserving ordering.
#
pipeline.ordered: auto

# unless configured otherwise.


#
api.http.host: 127.0.0.1
#
# The HTTP API web server will listen on an available port from
the given range.
# Values can be specified as a single port (e.g., `9600`), or
an inclusive range
# of ports (e.g., `9600-9700`).
#
api.http.port: 9600-9700

7
api.auth.basic.username: "logstash-user"
api.auth.basic.password: "s3cUreP4$$w0rD"

logstash.yml | Logstash Reference [8.9] | Elastic

8
กําหนดค่าอินสแตนซ์ Logstash ของคุณเพื่อใช้ปลั๊กอินอินพุต Beats
beats {
port => "5044"
}
คุณจะกําหนดค่า Logstash ให้เขียนไปยัง Elasticsearch ในภายหลัง สําหรับ
ตอนนี้คุณสามารถเพิ่มบรรทัดต่อไปนี้ ไปยังส่วนเพื่อให้ผลลัพธ์ถูกพิมพ์ไปยัง stdout เมื่อ
คุณเรียกใช้ Logstash:output
stdout { codec => rubydebug }
เมื่อคุณทําเสร็จแล้วเนื้อหาของควรมีลักษณะดังนี้:first-pipeline.conf
input {
beats {
port => "5044"
}
}
# The filter part of this file is commented out to indicate that it is
# optional.
# filter {
#
# }
output {
stdout { codec => rubydebug }
}

Run

D:\ELASTICSEARCH\logstash-8.9.1-windows-
x86_64\logstash-8.9.1\bin\logstash -f
D:\ELASTICSEARCH\logstash-8.9.1-windows-

9
x86_64\logstash-8.9.1\bin\first-pipeline.conf --
config.test_and_exit

ตัวเลือกจะแยกวิเคราะห์ไฟล์การกําหนดค่าของคุณและรายงานข้อผิดพลาดใดๆ--
config.test_and_exit

D:\ELASTICSEARCH\logstash-8.9.1-windows-
x86_64\logstash-8.9.1\bin\logstash -f
D:\ELASTICSEARCH\logstash-8.9.1-windows-
x86_64\logstash-8.9.1\bin\first-pipeline.conf --
config.reload.automatic
ตัวเลือกเปิดใช้งานการโหลด config อัตโนมัติเพื่อที่คุณจะได้ไม่ต้องหยุดและรีสตาร์ท
Logstash ทุกครั้งที่คุณแก้ไขไฟล์การกําหนดค่า--config.reload.automatic

10

You might also like