#!/usr/bin/perl -w
use strict;
#Aothor@Laomeng
#E-mail:[email protected]
=syn1
背景:批量插入指定的格式的數據
nsert_logfile程序的功能:是數據庫中的mamc_api庫的mamc_report表中插入數據
=cut
use DBI;
use Encode;
use File::Find;
use Data::Dumper;
use POSIX qw(strftime);
my $bakdate = strftime("%Y-%m-%d", localtime(time));#today
#日誌路徑存儲
my $log_file='/root/auto_report/log_2015/';
my @filepaths;
sub wanted
{
push @filepaths,"$File::Find::name" if -f $File::Find::name;
}
find(\&wanted,$log_file);
#數據庫賬戶信息
my %mysql = (
'dbname' => "mamc_api",
'host' => "127.0.0.1",
'port' => 3306,
'user' => 'root',
'pass' => 'abc@198920',
);
#連接數據庫判斷是否連接成功
my $database ="DBI:mysql:$mysql{'dbname'}";
my $dbh = DBI->connect($database,$mysql{'user'},$mysql{'pass'})
or die("Could not make connection to database:$DBI::errstr");
$dbh->do("SET character_set_client='utf8'");
$dbh->do("SET character_set_connection='utf8'");
$dbh->do("SET character_set_results='utf8'");
#讀取日誌文件內容並內容插入數據庫中
open ERRLOG,">>/root/auto_report/log/openerr.log"; #判斷文件是否爲空,如果爲空產生一條記錄
chomp (my $date=`date`);
open(CHF,">>/root/auto_report/log/checkpro.log"); #程序終止日誌
unless(@filepaths)
{
print CHF "$date => Array empty,The specified directory is empty:/root/auto_report/log_2015\n";
exit 0;
}
my $bakdir="/root/auto_report/logbak$bakdate";
if (! -e "$bakdir")
{
mkdir "$bakdir",0755 or warn "Can't make '$bakdir':$!\n";
}
foreach my $value (@filepaths)
{
if( -z $value)
{
print ERRLOG "$date => empty file $value\n";
next;
}
print "$value\n";
my $into_t = $dbh->prepare("INSERT INTO mamc_report(area_name,sys_name,dev_name,ip_addre,wri_date,call_name,api_name,call_count,call_time) VALUES(?,?,?,?,?,?,?,?,?)");
open(FL,'<:encoding(UTF-8)',$value);
while(<FL>)
{
my @v=split (/,/);
$into_t->execute("$v[0]","$v[1]","$v[2]","$v[3]","$v[4]","$v[5]","$v[6]",$v[7],"$v[8]") or die "無法執行SQL語句:$dbh->errstr";
}
open(SUCF,">>/root/auto_report/log/opensucess.log");
print SUCF "$date => successful write file $value\n";
`mv $value $bakdir/`; #歸檔操作的日誌文件
}
close(CHF);
close(ERRLOG);
close(FL);
close(SUCF);
mysql--批量插入數據
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.