顯示具有 handle 標籤的文章。 顯示所有文章
顯示具有 handle 標籤的文章。 顯示所有文章

2016年12月29日 星期四

[PHP] 使用 str_getcsv 處理 CSV 格式 (Google Docs export CSV format)

這議題已經很久,之前一直沒踩到什麼,直到處理 Google doc export the CSV format ...

大部分的分析都用:

foreach ( @explode("\n", file_get_content('https://docs.google.com/spreadsheets/d/.../export?format=csv...')) as $line ) {
$field = str_getcsv($line);
}


但是,若有 enclosure 的方式(例如 "...\n....\n") 就會出包啦,這在 http://php.net/manual/en/function.str-getcsv.php 網站上也有人提到,解法就是一樣用 str_getcsv 去切 $row:

$row = str_getcsv( file_get_content('https://docs.google.com/spreadsheets/d/.../export?format=csv...'), "\n");
foreach( $row as $line ) {
$field = str_getcsv($line);
}


但是,在處理 Google Docs 又發現切不對,最後發現改用 "\r\n" 即可 :P

$row = str_getcsv( file_get_content('https://docs.google.com/spreadsheets/d/.../export?format=csv...'), "\r\n");
foreach( $row as $line ) {
$field = str_getcsv($line);
}


Updated @ 2017/05/04
可以改用 tsv 輸出: $csv = explode("\n", @file_get_contents('https://docs.google.com/spreadsheets/d/.../export?format=tsv...'));
array_shift($csv); // remove header


foreach($csv as $line) {
$fields = str_getcsv($line, "\t");
print_r($fields);
}

2014年8月11日 星期一

AWS 筆記 - 透過 Script 半自動化處理 MySQL Replication Error

用了 AWS RDS 一陣子了,之前一直沒搞懂為何 call mysql.rds_skip_repl_error; 一次只能清一條 Error ,直到另一台 MySQL Slave Server 出錯,變成也要手動清除錯誤訊息時,才搞懂原理就是這樣,一次只能清一條。

所以,對於 MySQL Replication Error 不知不覺就生了兩種 script 來應付,一種是 RDS 當 MySQL Replication Slave 用的,另一個則是一般 MySQL Replication Slave Server 用的:

處理 AWS RDS MySQL Replication Slave Servers:

#!/bin/sh
a=0
while [ $a -lt 1000 ]
do
        check=`echo "SHOW SLAVE STATUS \G" | mysql -h YOUR_AWS_RDS_SERVER -u root -ppassword | grep "Last_SQL_Error:" | grep -c "Could not execute"`
        if [ $check -eq 0 ] ; then
                break
        fi
        echo "CALL mysql.rds_skip_repl_error;" | mysql -h YOUR_AWS_RDS_SERVER -u root -ppassword
        a=$(( $a + 1 ))
done


處理 MySQL Replication Slave Servers:

#!/bin/sh

a=0
while [ $a -lt 2000 ]
do
        check=`echo "SHOW SLAVE STATUS \G" | mysql -h YOUR_MYSQL_SLAVE_SERVER -u root -ppassword | grep "Last_SQL_Error:" | grep -c "Error "`
        if [ $check -eq 0 ] ; then
                break
        fi
        check=`echo "STOP SLAVE; SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; START SLAVE;" | mysql -h localhost -u root -ppassword`
        a=$(( $a + 1 ))
        sleep 3
done


以上是用在 MySQL Server 彼此算異質系統,只能先透過這招頂替了。

2014年3月21日 星期五

[PHP] 使用 Memory Cache (php://memory) 加速 File Handle 操作

最近使用 maxmind / MaxMind-DB-Reader-php 時,發現大量處理很慢,研究一下 src / MaxMind / Db / Reader.php 後,發現 DB 沒有一口氣讀進記憶體使用,就找一下如何將檔案搬進 Memory 後,一樣用 File Handle 對 memory 操作,如此可以降低 source code 的更動。

用法(以 src / MaxMind / Db / Reader.php 為例):

$this->fileHandle = @fopen($database, 'rb');
if ($this->fileHandle === false) {
throw new \InvalidArgumentException(
"Error opening \"$database\"."
);
}

$this->fileSize = @filesize($database);
if ($this->fileSize === false) {
throw new \UnexpectedValueException(
"Error determining the size of \"$database\"."
);
}

// Add by changyy
$fp = fopen("php://memory", 'rb+');
if(is_resource($fp)) {
fputs($fp, fread($this->fileHandle, $this->fileSize) );
rewind($fp);
fclose($this->fileHandle);
$this->fileHandle = $fp;
}


使用的結果,在 Linode 2048 的機器上,測試 20k 筆資料,未加時約 65 秒,加了可以進步到 40 秒附近,稍微失落 Orz 此外,可以用 memory_get_peak_usage() 來的知記憶體的增減。