using logstash to sync data - mysql

I'm trying to use logstash to sync all my data on my MySql server to my Elasticsearch server.
I've aleardy learned the basics of logstash.conf, this is my file:
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost/homestead"
jdbc_user => "homestead"
jdbc_password => "secret"
jdbc_driver_library => "/home/vagrant/Code/mysql-connector-java-5.1.38-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
statement => "SELECT * from volunteer"
}
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost/homestead"
jdbc_user => "homestead"
jdbc_password => "secret"
jdbc_driver_library => "/home/vagrant/Code/mysql-connector-java-5.1.38-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
statement => "SELECT * from contact"
}
}
output {
elasticsearch {
document_id => "%{uid}"
hosts => "localhost"
}
}
My intention is to copy every table into a Type. How do I specify this?
edit: "type" instead of "index"
Thank you!

What you can do is simply to add a field (using add_field) in each input denoting the type name which you want the data to be indexed in and then use that variable as the type name in the elasticsearch output.
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost/homestead"
jdbc_user => "homestead"
jdbc_password => "secret"
jdbc_driver_library => "/home/vagrant/Code/mysql-connector-java-5.1.38-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
statement => "SELECT * from volunteer"
add_field => {"type" => "volunteer"}
}
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost/homestead"
jdbc_user => "homestead"
jdbc_password => "secret"
jdbc_driver_library => "/home/vagrant/Code/mysql-connector-java-5.1.38-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
statement => "SELECT * from contact"
add_field => {"type" => "contact"}
}
}
output {
elasticsearch {
hosts => ["localhost"]
index => "homestead"
document_type => "%{type}" <--- specify the index here
document_id => "%{uid}"
}
}
Be aware though that using the same index to host several different mapping types might lead to type conflicts. The short story is that two different fields with the same name in two different types MUST ALWAYS have the same type definition. Read more about it in this blog article

Related

Logstash settup to Mysql hosted on AWS RDS. Won't connect

I have a MySQL DB hosted on AWS RDS. I am running ElasticSearch locally and using Logstash to retrieve data from MYSQL server that is on AWS to then push the data to my ElasticSearch DB.
The problem is that my logstash file isn't settup correctly I guess:
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://aws.ffffffffff.us-east-1.rds.amazonaws.com:3306/dbName?user=userName&password=pword"
jdbc_user => "user"
jdbc_password => "pword"
schedule => "* * * * *"
jdbc_validate_connection => true
jdbc_driver_library => "C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.19.jar"
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
statement => "SELECT * from data-5"
type => "data-5"
tags => ["data-5"]
}
jdbc {
jdbc_connection_string => "jdbc:mysql://aws.ffffffffff.us-east-1.rds.amazonaws.com:3306/dbName?user=userName&password=pword"
jdbc_user => "user"
jdbc_password => "pword"
schedule => "* * * * *"
jdbc_validate_connection => true
jdbc_driver_library => "C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.19.jar"
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
statement => "SELECT * from data-4"
type => "data-4"
tags => ["data-4"]
}
jdbc {
jdbc_connection_string => "jdbc:mysql://aws.ffffffffff.us-east-1.rds.amazonaws.com:3306/dbName?user=userName&password=pword"
jdbc_user => "user"
jdbc_password => "pword"
schedule => "* * * * *"
jdbc_validate_connection => true
jdbc_driver_library => "C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.19.jar"
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
statement => "SELECT * from data-3"
type => "data-3"
tags => ["data-3"]
}
}
output {
stdout { codec => json_lines }
if "data-5" in [tags] {
elasticsearch {
hosts => ["http://127.0.0.1:9200/"]
index => "data-5"
document_type => "data-%{+YYYY.MM.dd}"
}
}
if "data-4" in [tags] {
elasticsearch {
hosts => ["http://127.0.0.1:9200/"]
index => "data-4"
document_type => "data-%{+YYYY.MM.dd}"
}
}
if "data-3" in [tags] {
elasticsearch {
hosts => ["http://127.0.0.1:9200/"]
index => "data-3"
document_type => "data-%{+YYYY.MM.dd}"
}
}
}
This is the fun part of programming right?
Anyway, locally I am on windows as you may be able to tell from the file path to the jdbc driver library. My jdbc connection to the AWS RDS is copied and pasted from AWS Console, so no typos were involved.
I am told that I only need to append jdbc:mysql:// to the url. But is there anything I'm missing to do in the AWS console? Do I need to modify my RDS instance?
The error by the way is:
Unable to connect to database. Tried 1 times
{:error_message=>"Java::ComMysqlCjJdbcExceptions::CommunicationsException:
Communications link failure\n\n
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server."
I had a similar issue, and SSL was not enabled for communication. So, AWS RDS was not allowing to connect.
I specified an additional query param useSSL=false to JDBC connection string, and problem was solved.
So, in your case, the jdbc_connection_string would be as follows:
"jdbc:mysql://aws.ffffffffff.us-east-1.rds.amazonaws.com:3306/dbName?useSSL=false&user=userName&password=pword"

logstash: not able to connect mysql with logstash?

I am trying to make connection with MySQL using logstash and write into elastic search below in my code in conf file
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://192.168.2.24:3306/test"
# The user we wish to execute our statement as
jdbc_user => "uname"
jdbc_password => "pass"
# The path to our downloaded jdbc driver
jdbc_driver_library => "/usr/local/Cellar/logstash/6.2.4/mysql-connector-java-8.0.11.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
# our query
statement => "SELECT * FROM report_table"
}
}
output {
elasticsearch {
action => "index"
hosts => "localhost:9200"
index => "mysqlsample"
document_type => "record"
}
}
on running the above getting the below error :
Error: com.mysql.jdbc.Driver not loaded. Are you sure you've included
the correct jdbc driver in :jdbc_driver_library? Exception:
LogStash::ConfigurationError Stack:
/usr/local/Cellar/logstash/6.2.4/libexec/vendor/bundle/jruby/2.3.0/gems/logstash-input-jdbc-4.3.9/lib/logstash/plugin_mixins/jdbc.rb:162:in
open_jdbc_connection'
/usr/local/Cellar/logstash/6.2.4/libexec/vendor/bundle/jruby/2.3.0/gems/logstash-input-jdbc-4.3.9/lib/logstash/plugin_mixins/jdbc.rb:220:in
execute_statement'
/usr/local/Cellar/logstash/6.2.4/libexec/vendor/bundle/jruby/2.3.0/gems/logstash-input-jdbc-4.3.9/lib/logstash/inputs/jdbc.rb:264:in
execute_query'
/usr/local/Cellar/logstash/6.2.4/libexec/vendor/bundle/jruby/2.3.0/gems/logstash-input-jdbc-4.3.9/lib/logstash/inputs/jdbc.rb:250:in
run'
/usr/local/Cellar/logstash/6.2.4/libexec/logstash-core/lib/logstash/pipeline.rb:514:in
inputworker'
/usr/local/Cellar/logstash/6.2.4/libexec/logstash-core/lib/logstash/pipeline.rb:507:in
block in start_input'
Sounds like it's an issue with jdbc_driver_library => "/usr/local/Cellar/logstash/6.2.4/mysql-connector-java-8.0.11.jar".
Are you sure it's a valid path? Is that the correct connector? Maybe try to use the one that the documentation mentions: mysql-connector-java-5.1.36-bin.jar

Laravel 5.4 odbc connection MS SQL SERVER

I need to config Laravel 5.4 to use the ODBC PDO Driver for Microsoft SQL Server.I didnt find any useful resource. If anyone previously used odbc in laravel 5.4 please help me out.
Database.php
'odbc' => [
'driver' => 'odbc',
'dsn' => 'Driver={SQL Server};Server=
{serverName};Trusted_Connection=true;Database=meteor;',
'host' => 'DESKTOP-B9M4O7M\SQLEXPRESS',
'database' => 'meteor',
'username' => 'DESKTOP-B9M4O7M\admin',
'password' => '',
'grammar' => [
'query' => Illuminate\Database\Query\Grammars\SqlServerGrammar::class,
'schema' => Illuminate\Database\Schema\Grammars\SqlServerGrammar::class,
],
],
You have missed a couple of settings, and misunderstood a couple of others. Try the following:
'odbc' => [
/* USE sql server as the driver, and set odbc settings (see below) */
'driver' => 'sqlsrv',
/* Tell laravel that you are using odbc */
'odbc' => true,
/* Set the ODBC data source, your previous dns */
'odbc_datasource_name' => '{SQL Server}',
/* There is no dsn config variable that I am aware of */
/*'dsn' => 'Driver={SQL Server};Server={serverName};Trusted_Connection=true;Database=meteor;', */
/* host is the server */
'host' => '{serverName}', /*'DESKTOP-B9M4O7M\SQLEXPRESS',*/
'database' => 'meteor',
'username' => 'DESKTOP-B9M4O7M\admin',
'password' => '',
'grammar' => [
'query' => Illuminate\Database\Query\Grammars\SqlServerGrammar::class,
'schema' => Illuminate\Database\Schema\Grammars\SqlServerGrammar::class,
],
],

Connect with multiple database in CakePHP 3

I want to store some data histories and logs in other database table it may be very large database in terms of records in future. Also cross database SQL joing should be supported. So please help me for solution for this.
Thanks in advance
You have follow below steps fo use multiple datasource within same cakephp application.
Mention multiple database source in Config/app.php
You have to manage multiple data source configurations, in default data source keep main database, and make one more data source we can say it history for second data source. As mentioned below
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '<host name>',
'username' => '<database user>',
'password' => '<database password>',
'database' => '<database name>',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
'history' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '<host name>',
'username' => '<database user>',
'password' => '<database password>',
'database' => '<database name>',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
]
]
Specify data source in Tables Classes
In Src/Model/Table/<AnyOtherSource>Table.php, Add below method, where you want to use history data source. No need to add below method where you need to use default data source, for default data source CakePHP will take care of it.
public static function defaultConnectionName() {
return 'history';
}
Joining and Model association in CakePHP 3
You access other databases on the same MySQL instance by prefixing the
table with the appropriate database name. You have to specify table
name with database_name.table_name, For this you need to add below
code in each table classes.
For Example,
SELECT * FROM this_database.table_1 t1 JOIN that_database.table_2 t2 ON t2.column = t1.column
Add below code in Src/Model/Table/<All>Table.php
class LogsTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table($this->connection()->config()['database'] . "." . $this->table()); // this is very important for joining and associations.
// your other code for initilize method
}
public static function defaultConnectionName() {
return 'history';
}
// other methods and your code should be here
}

Logstash JDBC Input plugin : Migrate data from mysql in batch count

I have a table of 20GB data having 50 million rows. Need to migrate to ElasticSearch using logstash jdbc input plugin. I have tried all basic implementation but need help in migrating data in batch i.e only 10,000 rows at a time. I am not sure how and where to specify this count and how to update it the next time i run logstash. Please help me solve this issue
This is what i have:
input {
jdbc {
jdbc_driver_library => "mysql-connector-java-5.1.12-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://localhost/db"
jdbc_validate_connection => true
jdbc_user => "root"
jdbc_password => "root"
clean_run => true
record_last_run => true
use_column_value => true
jdbc_paging_enabled => true
jdbc_page_size => 5
tracking_column => id
statement => "select * from employee"
}
}
Thanks in advance.
You need to set jdbc_paging_enabled to true in order for pagniation to work.
But you also need to make sure that clean_run is set to false, otherwise pagination won't work.