2013-02-11 86 views
0

我從DBpedia中下載的文件,與內容是這樣的:如何從文本文件導入特定數據到mysql?

<http://dbpedia.org/resource/Selective_Draft_Law_Cases> <http://dbpedia.org/ontology/wikiPageExternalLink>  <http://supreme.justia.com/cases/federal/us/245/366/> . 
<http://dbpedia.org/resource/List_of_songs_recorded_by_Shakira> <http://dbpedia.org/ontology/wikiPageExternalLink> <http://www.shakira.com/> . 
<http://dbpedia.org/resource/Bucharest_Symphony_Orchestra> <http://dbpedia.org/ontology/wikiPageExternalLink> <http://www.symphorchestra.ro/> . 
<http://dbpedia.org/resource/Bucharest_Symphony_Orchestra> <http://dbpedia.org/ontology/wikiPageExternalLink> <http://symphorchestra.ro> . 
<http://dbpedia.org/resource/Bucharest_Symphony_Orchestra> <http://dbpedia.org/ontology/wikiPageExternalLink> <http://www.youtube.com/symphorchestra> . 

我需要從每一行(即Selective_draft_Law_Cases在第一線,List_of_songs_etc第二等的第一部分中提取標題)),並且它在一個MySQL表這是在同一行中的第三元素中的URL一起保存,iefor的first linesecond line

我還需要跳過該文件中的第一行,其有不同的,不相關的信息。

在PHP中完成這項工作的最快方法是什麼?

注意:該文件相當大(超過1 GB的大小,超過600萬行)。

在此先感謝!

+6

1. while loop 2. read line。 3.分割線。 4.插入行。 5.重複 – 2013-02-11 14:58:43

回答

1

我相信它可以優化,但它的一個開始。請嘗試:

function insertFileToDb(){ 
    $myFile = "myFile.txt"; //your txt file containing the data 
    $handle = fopen($myFile, 'r'); 

    //Read first line, but do nothing with it 
    $contents = fgets($handle); 

    //now read the rest of the file line by line 
    while(!feof($handle)){ 
     $data = fgets($handle); 

     //remove <> characters 
     $vowels = array("<", ">"); 
     $data = str_replace($vowels, "", $data); 

     //remove spaces to a single space for each line 
     $data = preg_replace('!\s+!', ' ', $data); 

     /* 
     * Get values from array, 1st URL is $dataArr[0] and 2nd URL is $dataArr[2] 
     * Explode on ' ' spaces 
     */ 
     $dataArr = explode(" ", $data); 

     //Get last part of uri from 1st element in array 
     $title = $this->getLastPartOfUrl($dataArr[0]); 

     //Execute your sql query with $title and $dataArr[2] which is the url 
     INSERT INTO `table` ... 
    } 
    fclose($handle); 
} 

function getLastPartOfUrl($url){ 
    $keys = parse_url($url); // parse the url 
    $path = explode("/", $keys['path']); // splitting the path 
    $last = end($path); // get the value of the last element 
    return $last; 
} 
+0

mallix,我試圖測試你的代碼,但遇到了一個障礙。文件中的第一行是「#started 2012-06-04T11:00:11Z」,它會引發錯誤。我如何讓代碼忽略第一行? – Phil 2013-02-12 09:00:12

+0

你沒有提到這一點。您的帖子中沒有「#」。先更新它。 – mallix 2013-02-12 10:44:31

+0

你是對的,已更新。 – Phil 2013-02-12 11:01:39

1

您應該使用正則表達式和使用PHP的preg_match功能,如果文件過大(這似乎是你的情況),您可能需要使用fopen + fgets + fclose以避免加載整個文件記憶和逐行工作。

您可以嘗試測試file_get_contents對文件讀取的性能,但由於需要大量內存,看起來這不會是更快的方式。

相關問題