RElated Topics

Tuesday, August 5, 2008

CSV Utility Class


<?php
/**
* CSV File to Assiciative Array ( Simply Using fgetcsv() )
* &&
* CSV String to Associative Array ( Using tmpfile() )
*/

error_reporting(E_ALL);

echo '<pre>'.var_export( CSV::file2Array('csv.csv') , true ).'</pre>';

echo '<pre>'.var_export( CSV::str2Array( "f1,f2,f3\naa,\"bb,bb\",cc\nxx,yy,zz" ) , true ).'</pre>';

class CSV
{
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//General Utility Functions Start
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/**
* Utility Function to make array from csv file, Array keys are same as csv column name.
*
* Function internaly call fgetcsv to perform it operation
* @access public
* @return Array Return array is associative array , Whose keys are identical to csv column names )
* @param string valid file path
* @param character defalut value ','
* @param character default value '"'
* @param character defalut '\' currently ignored
*/
public static function file2Array( $csv_path, $delimiter=',', $enclosure='"', $escape='\\' )
{
//return associative array from csv file
//assumption $csvstr containcsv data, first line containing column name
// $csvstr = "f1,f2,f3\naa,\"bb,bb\",cc\nxx,yy,zz";
// Array( [0] => Array( [f1] => aa [f2] => bb,bb [f3] => cc )
// [1] => Array( [f1] => xx [f2] => yy [f3] => zz ) )
$handle = @fopen($csv_path, "r");
#var_dump( $csv_path, $handle ); die();
$ret = Array();
if ( false !== $handle)
{
//retrive columns
$cols = @fgetcsv($handle, 1000, $delimiter, $enclosure);
//make all value to lower case
foreach($cols as $k=>$v)
{
$cols[$k]= strtolower(trim($v));
}

while( ($data = @fgetcsv($handle, 1000)) !== FALSE)
{
if ( '' != trim($data[0]) )//for ignoring empty lines
{
//IMP to ignore empty lines
$ret[] = array_combine($cols, $data);
}
}
@fclose($handle);
}
return $ret;
}//END_OF_csvFile2Array

/**
* Utility Function to,Convert csv string to array with keys are same as column name
*
* Function internaly call fgetcsv to perform it operation, <br>
* for this it create temporery file usign tempfile() wich will be deleted on fclose().
* @access public
* @return Array Return array is associative array , Whose keys are identical to csv column names )
* @param string valid file path
* @param character defalut value ','
* @param character default value '"'
* @param character defalut '\' currently ignored
*/
public static function str2Array( $csv_str, $delimiter=',', $enclosure='"', $escape='\\' )
{
//return associative array from csv data string
//assumption $csvstr containcsv data, first line containing column name
// $csvstr = "f1,f2,f3\naa,\"bb,bb\",cc\nxx,yy,zz";
// Array( [0] => Array( [f1] => aa [f2] => bb,bb [f3] => cc )
// [1] => Array( [f1] => xx [f2] => yy [f3] => zz ) )

$handle = tmpfile();
$ret = Array();
if ( false !== $handle)
{
//Write csv data to string
@fwrite($handle, $csv_str);
@fseek($handle, 0);//Move pointer to start

//retrive columns
$cols = @fgetcsv($handle, 1000, $delimiter, $enclosure);
//make all value to lower case
foreach($cols as $k=>$v)
{
$cols[$k]= strtolower(trim($v));
}

while( ($data = @fgetcsv($handle, 1000, $delimiter, $enclosure)) !== FALSE)
{
if ( '' != trim($data[0]) )
{
//IMP to ignore empty lines
$ret[] = array_combine($cols, $data);
}
}
@fclose($handle);
}
return $ret;
}//END_OF_csvStr2Array

public static function array2Str( $arr ){ die('CSV::array2Str() yet not implemented '); }
public static function array2File( $arr , $path){ die('CSV::array2File() yet not implemented '); }


}
?>

Friday, August 1, 2008

mysql replication

What is Mysql Replication

  • Replication enables data from one MySQL database server (called the master) to be replicated to one or more MySQL database servers (slaves).

  • Replication is asynchronous - your replication slaves do not need to be connected permanently to receive updates from the master,which means that updates can occur over long-distance connections and even temporary solutions such as a dial-up service.

  • Depending on the configuration, you can replicate all databases, selected databases and even selected tables within a database.More..




What-is/Where-to use Of Mysql Replication

  • Web apllication having larger(millions of) requests per day need to be served from multiple servers.

  • To run application from Multiple Server we need to access

    • A. One Central Database (Having Very Good HW Support )

    • Or

    • B. Use Mysql Replication ( Replica of mysql database ) To Manage Replica of Master database on All slave we use Mysql Replication as Below Image

      Web Aplication Structure using mysql replication


      Load Balancer OR LVS (Linux Virtual Server) is special machine, which take request from client and forward it to any web client.


      Three Slave db with Web Client(Apache) run One site.

      Each Web client is identical and serv same website.

      All Insert / Update goes to master db server,
      This Updated / Modified data will be Replicatedto all slave db server.

      Each select statement goes to slave DB server.








  • Steps To Set Mysql Replication

  • Set Master to create binary log for all or Selected database.

  • Create User on Mysql having Mysql replication priveleges( Login to master from slaves )

  • Set Salve Configuration ( Select Databae/s to Replicate.List of Table for do-replicate, do-note-replicate )

  • Set Replication ( Set Master-Server User,Psw,Logfile, Position )

  • More Technical Details How to Set Up Replication




Usefull linux commands

  • To start stop restart mysql

    shell_prompt>>/etc/init.d/mysql start

    or
    shell_prompt>>/etc/init.d/mysqld start

    Hint after typing "/etc/init.d/mysq" ( not l is missing) press tab key will display all installed mysql setup



  • Find mysql Data-directory , config-file path, Error-log path


    shell_prompt>>ps -elf | grep mysql
    --datadir=/var/lib/mysql //Mysql Data Dir, here all Binary-Log, Database folders having( *.frm, *.MYD, *.MYI)
    --defaults-file=/etc/my.cnf //Mysql Config file
    --log-error=/var/log/mysqld.log //Mysql Error log file



  • #open my.cnf in vi editor

    shell_prompt>>vi /etc/my.cnf

    in my.cnf file , comments starts with first character as #



Helpful vi commands

  • Pressing Esc key editor gose in Command mode, Diffrent Comands are as follow.

  • Esc i ->Insert Mode

  • Esc u ->Undo

  • Esc :w ->Save

  • Esc :q! ->Quit Without save

  • Esc :wq ->Saveand Quit

  • Esc dd ->Delete Current Line

  • Keybord Shortcuts in Insert Mode

  • Selecting Text with Mouse

  • Ctrl+Insert ->Copy

  • Shift+Insert ->Paset