天天看點

Mysql Perl unload表資料

#!/usr/bin/perl 
use DBI;
$dbUser='test';
$user="test";
$passwd="123456";
$dbh="";
$dbh = DBI->connect("dbi:mysql:database=plmsdb;host=10.10.17.5;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
$UNLOAD_SRC_DBCONN = DBI->connect("dbi:mysql:database=plmsdb;host=10.10.17.5;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
my $table_name= "$ARGV[0]"; 
my $hostSql = qq{SELECT column_name  from information_schema.columns where table_schema='test' and  table_name='$table_name'};
my $DW_DATA_DT ="";
my $datafile="$table_name.TXT";
use HTTP::Date qw(time2iso str2time time2iso time2isoz);


my @lstRlst1;
my @lstRlst;
my ($COLUMN_NAME);
my $selStmt = $dbh->prepare($hostSql);
$selStmt->execute();
$selStmt->bind_col(1, $COLUMN_NAME);;
$selStmt->execute();
while( $selStmt->fetch() ){
  print "$COLUMN_NAME
";
push  (@lstRlst1 ,$COLUMN_NAME);
  }
  $selStmt->finish;
  $dbh->disconnect;
my @lstRlst =  (@lstRlst1);
##########################################
#=================全局變量區==========================#
if ($#ARGV <0){
        print "請輸入一個表名參數";
        exit(-1);
    }
sub printlog
{
  my ($LogInfo)= @_;
  my $CurrTime = time2iso(time());                   # 目前時間
  if(!defined($LogInfo) ){$LogInfo="";}
  my $StrLog="【${CurrTime}】 	 ${LogInfo} 
"; 
  
  print $StrLog;
  #print LOGFILE $StrLog;
  }
  
my $exportOracleSql="SELECT ";  #資料導出的sql
for (my $m=0;$m<@lstRlst + 0 ;$m++){
 if  ($m != @lstRlst + 0 - 1){
  $exportOracleSql = "$exportOracleSql trim($lstRlst[$m])".", "
}
else{
$exportOracleSql = "$exportOracleSql trim($lstRlst[$m])"}
print "$exportOracleSql
";
}
my $exportOracleSql="$exportOracleSql from $dbUser.$table_name";
print "$exportOracleSql
";

sub Exportdata{
	    
	    printlog "開始導出資料!";
	    my $exportsql=$exportOracleSql;
	    if($exportsql eq "error"){
	    	return -1;
	    	}
	     $stmt=$UNLOAD_SRC_DBCONN->prepare($exportsql);
	    unless ($stmt){
			printlog "
執行prepare SQL語句出錯:
";
			printlog $DBI::errstr;
			return -1;
		}
	       $stmt->execute;
		if ($UNLOAD_SRC_DBCONN->err) {
			printlog "
執行SQL語句出錯:
"; 
			printlog $DBI::errstr;
			
			return -1;
		}
	     my $row=0;
	     my $size=0;
	     my $curtime;

	     
	     
	     my $writeflagsql;
	     my $tmpstr="";
	     $row=0;
	     my $m=0;              
 open(DATAFILE,">", $datafile) || die (print "Open DATA file failed!!!
");
	     while(my $Rows = $stmt->fetchrow_arrayref){
	     	$m=0;
	     	$tmpstr="";
	     	foreach(@$Rows){
	     		$tmpstr=$tmpstr.$Rows->[$m]."|";
	     		$m++;
	     	}
	     	print DATAFILE $tmpstr.$DW_DATA_DT."
";
	     	$row++;
	     	if(($row%10000) == 0){
	     		printlog "已導出資料$row條!";
	     	}	     	     	
	    }
     	
        	$stmt->finish;
        #	print FLAGFILE $datafile,"
";
        #	print FLAGFILE $row,"
";
        	close(DATAFILE);
        #  close(FLAGFILE);

        	$curtime=time2iso(time());
          printlog "資料已成功導出!";
          printlog "一共導出資料${row}條";
                
          return 1;	
	
	}
Exportdata