{
  "id": 5606,
  "title": "SQL Script to load data into PostgreSQL DB",
  "url": "/competitions/facebook-recruiting-iii-keyword-extraction/discussion/5606",
  "author_name": "Sashikanth Dareddy",
  "post_date": "2013-08-31T21:30:56.190000",
  "votes": 17,
  "comment_count": 14,
  "views": 7559,
  "content": "<p>I find PostgreSQL to be extremely versatile to manipulate data ( albeit at a slower speed) so, I've loaded this data using the following script without any issues. I've also posted summary stats here:&nbsp;http://www.kaggle.com/c/facebook-recruiting-iii-keyword-extraction/forums/t/5605/extended-summary-stats-for-the-data-provided</p>\n<p><code>CREATE TABLE RAW_TRAIN(<br>ID BIGINT PRIMARY KEY<br>,TITLE TEXT<br>,BODY TEXT<br>,TAGS TEXT<br>);<br>copy RAW_TRAIN from 'D:\\\\FacebookIII\\\\Data\\\\Train.csv' csv header;<br>/*Query returned successfully: 6034195 rows affected, 1072534 ms execution time.*/</code></p>\n<p>&nbsp;</p>\n<p><code>CREATE TABLE RAW_TEST(<br>ID BIGINT PRIMARY KEY<br>,TITLE TEXT<br>,BODY TEXT<br>);<br>copy RAW_TEST from 'D:\\\\FacebookIII\\\\Data\\\\Test.csv' csv header;<br>/*Query returned successfully: 2013337 rows affected, 116390 ms execution time.*/</code></p>",
  "messages": [
    {
      "id": 29868,
      "postDate": "2013-08-31T21:30:56.190Z",
      "content": "<p>I find PostgreSQL to be extremely versatile to manipulate data ( albeit at a slower speed) so, I've loaded this data using the following script without any issues. I've also posted summary stats here:&nbsp;http://www.kaggle.com/c/facebook-recruiting-iii-keyword-extraction/forums/t/5605/extended-summary-stats-for-the-data-provided</p>\n<p><code>CREATE TABLE RAW_TRAIN(<br>ID BIGINT PRIMARY KEY<br>,TITLE TEXT<br>,BODY TEXT<br>,TAGS TEXT<br>);<br>copy RAW_TRAIN from 'D:\\\\FacebookIII\\\\Data\\\\Train.csv' csv header;<br>/*Query returned successfully: 6034195 rows affected, 1072534 ms execution time.*/</code></p>\n<p>&nbsp;</p>\n<p><code>CREATE TABLE RAW_TEST(<br>ID BIGINT PRIMARY KEY<br>,TITLE TEXT<br>,BODY TEXT<br>);<br>copy RAW_TEST from 'D:\\\\FacebookIII\\\\Data\\\\Test.csv' csv header;<br>/*Query returned successfully: 2013337 rows affected, 116390 ms execution time.*/</code></p>",
      "votes": 17
    },
    {
      "id": 35266,
      "postDate": "2013-11-26T12:58:05.867Z",
      "content": "<p>select sum(array_length(regexp_split_to_array(TAGS, '\\s'),1)) from TRAINDATA;</p>",
      "votes": 3
    },
    {
      "id": 36244,
      "postDate": "2013-12-17T09:36:47.803Z",
      "content": "<p>I am late to the party. Hope the dataset is alive even after the contest ends. Following is my java code for parsing if somebody is trying in java (now!!).</p>\n<p>-------------------------------------------------</p>\n<p>public class IssueInstance {</p>\n<p>public int id;<br> public String heading=&quot;&quot;;<br> public String body=&quot;&quot;;<br> public String tags=&quot;&quot;;<br>}</p>\n<p>---------------------------------------------------</p>\n<p>public static String separateInstances() {</p>\n<p>LinkedList&lt;IssueInstance&gt; instances = new LinkedList&lt;IssueInstance&gt;();</p>\n<p>try {<br> // Open the file<br> FileInputStream fstream = new FileInputStream(pathName);<br> BufferedReader br = new BufferedReader(new InputStreamReader(<br> fstream));<br> int count = 0;<br> <br> String strLine;<br> String strLineprev = &quot;&quot;;</p>\n<p>String Body = &quot;&quot;;</p>\n<p>strLine = br.readLine();<br> System.out.println(strLine);// &quot;Id&quot;,&quot;Title&quot;,&quot;Body&quot;,&quot;Tags&quot;</p>\n<p>while ((strLine = br.readLine()) != null) {<br>&nbsp;<br> instances.add(new IssueInstance());<br> if (strLine.startsWith(&quot;\\&quot;&quot;) == true<br> &amp;&amp; strLine.startsWith(&quot;\\&quot;,&quot;) == false<br> &amp;&amp; strLine.startsWith(&quot;\\&quot;\\&quot;&quot;) == false) {</p>\n<p>// System.out.println(strLineprev.split(&quot;\\&quot;&quot;)[strLineprev.split(&quot;\\&quot;&quot;).length-1]);</p>\n<p>if (count &gt; 0) {<br> instances.get(count - 1).tags = strLineprev.split(&quot;\\&quot;&quot;)[strLineprev<br> .split(&quot;\\&quot;&quot;).length - 1];<br> instances.get(count - 1).body = Body<br> .replace(&quot;\\&quot;&quot; + instances.get(count - 1).id, &quot;&quot;)<br> .replace(instances.get(count - 1).heading, &quot;&quot;)<br> .replace(instances.get(count - 1).tags, &quot;&quot;)<br> .replaceAll(&quot;\\&quot;.\\&quot;&quot;, &quot;&quot;); <br> Body = &quot;&quot;;<br> }<br> // System.out.println(strLineprev.split(&quot;\\&quot;&quot;)[strLineprev.split(&quot;\\&quot;&quot;).length-1]);<br> // System.out.println(count);</p>\n<p>instances.get(count).id = count + 1;</p>\n<p>// System.out.println(strLine.split(&quot;\\&quot;,\\&quot;&quot;)[1]);<br> instances.get(count).heading = strLine.split(&quot;\\&quot;,\\&quot;&quot;)[1];<br> count++;</p>\n<p>} else<br> strLineprev = strLine;</p>\n<p><br> {<br> Body = Body + &quot; &quot; + strLine;<br> }<br> <br> }</p>\n<p>&nbsp;<br> // Close the input stream<br> br.close();<br>&nbsp;</p>\n<p>} catch (Exception e) {<br> System.out.println(e.getMessage());<br> }</p>\n<p>return &quot;&quot;;<br> }</p>",
      "votes": 1
    },
    {
      "id": 35265,
      "postDate": "2013-11-26T12:48:34.170Z",
      "content": "<p>Just wondering, how did you calculate the number of unique tags from the RAW_TRAIN table? Each row for TAGS column is a space separated string. Did you create another table to keep each tag separately?&nbsp;</p>",
      "votes": 1
    },
    {
      "id": 35082,
      "postDate": "2013-11-23T10:52:44.520Z",
      "content": "<p>[quote=venkat ram reddy kunta;35071]</p>\n<p><strong>How to skip the header of the csv file ?</strong></p>\n<p><strong>because when we are copying data from csv file to table which has integer values throw error. right?</strong></p>\n<p>&nbsp;</p>\n<p>[/quote]</p>\n<p>Notice the &quot;Header&quot; option in the &quot;Copy from&quot; statement. This is what skips the header.</p>\n<p><code>copy RAW_TRAIN from 'D:\\\\FacebookIII\\\\Data\\\\Train.csv' csv header;</code></p>",
      "votes": 1
    },
    {
      "id": 35335,
      "postDate": "2013-11-27T04:24:57.653Z",
      "content": "<p>You can update your path is Environment Variables if you think the path is the issue.</p>"
    },
    {
      "id": 35324,
      "postDate": "2013-11-27T00:19:44.973Z",
      "content": "<p>I have installed PostgreSQL and PSYCOPG, for connecting PostgreSQL to Python and retrieve data from it. &nbsp;And I am able to retrieve data successfully.</p>\n<p>Then I was using NLTK to perform some operations on the data.</p>\n<p>I got the following error:&nbsp;</p>\n<p>&quot;ImportError: No module named nltk&quot; .</p>\n<p>So I tried to install NLTK again using the URL:&nbsp;http://nltk.org/install.html.&nbsp;</p>\n<p>Output message:</p>\n<p>&quot;Requirement already up-to-date: pyyaml in /Library/Python/2.7/site-packages<br>Requirement already up-to-date: nltk in /Library/Python/2.7/site-packages<br>Cleaning up... &quot;</p>\n<p>this means that <strong>NLTK</strong> is already installed and up to date.</p>\n<p>but i cannot figure out the problem. May be during the installation of PSYCOPG ; PYTHON path has changed?&nbsp;</p>\n<p>Please help me out !</p>"
    },
    {
      "id": 35268,
      "postDate": "2013-11-26T13:14:30.853Z",
      "content": "<p>Thanks! That's very cool. I am becoming a fan of PostgreSQL :D</p>\n<p>&nbsp;</p>\n<p>This gives the number&nbsp;of occurrences of tags. But still pretty cool to get it in one query.</p>"
    },
    {
      "id": 35071,
      "postDate": "2013-11-23T00:20:37.007Z",
      "content": "<p><strong>How to skip the header of the csv file ?</strong></p>\n<p><strong>because when we are copying data from csv file to table which has integer values throw error. right?</strong></p>\n<p>&nbsp;</p>"
    },
    {
      "id": 32009,
      "postDate": "2013-10-04T02:15:20.327Z",
      "content": "<p>Thanks Thats nice .I have just installed postgre sql and started working looks nice that it can handle large data sources</p>"
    },
    {
      "id": 31994,
      "postDate": "2013-10-03T17:43:17.067Z",
      "content": "<p>Hi, you may have overlooked but the execution time (in milliseconds) is right beneath the COPY statement in my original post.</p>"
    },
    {
      "id": 31993,
      "postDate": "2013-10-03T17:35:50.060Z",
      "content": "<p>Hi sashikanth&nbsp;</p>\n<p>&nbsp;</p>\n<p>how log did it take to load the data in postgresql db ?</p>"
    },
    {
      "id": 30090,
      "postDate": "2013-09-03T20:29:06.543Z",
      "content": "<p>If somebody was interested, after reading the raw data with the script above, the folder with the table takes&nbsp;5,85GB.</p>\n<p>Just for comparison</p>\n<p>&nbsp;</p>\n<p>Query returned successfully: 6034195 rows affected, 300542 ms execution time.</p>\n<p>but that is due to the SSD drive I have. It could be faster if I had better faster SATA in my laptop.</p>"
    },
    {
      "id": 29901,
      "postDate": "2013-09-01T08:10:58.013Z",
      "content": "<p>Brilliant, thanks mate! Keeping it simple saved some time.&nbsp;</p>"
    },
    {
      "id": 30550,
      "postDate": "2013-09-09T17:42:00.440Z",
      "rawMarkdown": "",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 35266,
      "author_name": "Abhishek Thakur",
      "author_url": "",
      "post_date": "2013-11-26T12:58:05.867000",
      "content": "<p>select sum(array_length(regexp_split_to_array(TAGS, '\\s'),1)) from TRAINDATA;</p>",
      "votes": 3,
      "replies": []
    },
    {
      "id": 36244,
      "author_name": "Mensch",
      "author_url": "",
      "post_date": "2013-12-17T09:36:47.803000",
      "content": "<p>I am late to the party. Hope the dataset is alive even after the contest ends. Following is my java code for parsing if somebody is trying in java (now!!).</p>\n<p>-------------------------------------------------</p>\n<p>public class IssueInstance {</p>\n<p>public int id;<br> public String heading=&quot;&quot;;<br> public String body=&quot;&quot;;<br> public String tags=&quot;&quot;;<br>}</p>\n<p>---------------------------------------------------</p>\n<p>public static String separateInstances() {</p>\n<p>LinkedList&lt;IssueInstance&gt; instances = new LinkedList&lt;IssueInstance&gt;();</p>\n<p>try {<br> // Open the file<br> FileInputStream fstream = new FileInputStream(pathName);<br> BufferedReader br = new BufferedReader(new InputStreamReader(<br> fstream));<br> int count = 0;<br> <br> String strLine;<br> String strLineprev = &quot;&quot;;</p>\n<p>String Body = &quot;&quot;;</p>\n<p>strLine = br.readLine();<br> System.out.println(strLine);// &quot;Id&quot;,&quot;Title&quot;,&quot;Body&quot;,&quot;Tags&quot;</p>\n<p>while ((strLine = br.readLine()) != null) {<br>&nbsp;<br> instances.add(new IssueInstance());<br> if (strLine.startsWith(&quot;\\&quot;&quot;) == true<br> &amp;&amp; strLine.startsWith(&quot;\\&quot;,&quot;) == false<br> &amp;&amp; strLine.startsWith(&quot;\\&quot;\\&quot;&quot;) == false) {</p>\n<p>// System.out.println(strLineprev.split(&quot;\\&quot;&quot;)[strLineprev.split(&quot;\\&quot;&quot;).length-1]);</p>\n<p>if (count &gt; 0) {<br> instances.get(count - 1).tags = strLineprev.split(&quot;\\&quot;&quot;)[strLineprev<br> .split(&quot;\\&quot;&quot;).length - 1];<br> instances.get(count - 1).body = Body<br> .replace(&quot;\\&quot;&quot; + instances.get(count - 1).id, &quot;&quot;)<br> .replace(instances.get(count - 1).heading, &quot;&quot;)<br> .replace(instances.get(count - 1).tags, &quot;&quot;)<br> .replaceAll(&quot;\\&quot;.\\&quot;&quot;, &quot;&quot;); <br> Body = &quot;&quot;;<br> }<br> // System.out.println(strLineprev.split(&quot;\\&quot;&quot;)[strLineprev.split(&quot;\\&quot;&quot;).length-1]);<br> // System.out.println(count);</p>\n<p>instances.get(count).id = count + 1;</p>\n<p>// System.out.println(strLine.split(&quot;\\&quot;,\\&quot;&quot;)[1]);<br> instances.get(count).heading = strLine.split(&quot;\\&quot;,\\&quot;&quot;)[1];<br> count++;</p>\n<p>} else<br> strLineprev = strLine;</p>\n<p><br> {<br> Body = Body + &quot; &quot; + strLine;<br> }<br> <br> }</p>\n<p>&nbsp;<br> // Close the input stream<br> br.close();<br>&nbsp;</p>\n<p>} catch (Exception e) {<br> System.out.println(e.getMessage());<br> }</p>\n<p>return &quot;&quot;;<br> }</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 35265,
      "author_name": "Rakebul Hasan",
      "author_url": "",
      "post_date": "2013-11-26T12:48:34.170000",
      "content": "<p>Just wondering, how did you calculate the number of unique tags from the RAW_TRAIN table? Each row for TAGS column is a space separated string. Did you create another table to keep each tag separately?&nbsp;</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 35082,
      "author_name": "Sashikanth Dareddy",
      "author_url": "",
      "post_date": "2013-11-23T10:52:44.520000",
      "content": "<p>[quote=venkat ram reddy kunta;35071]</p>\n<p><strong>How to skip the header of the csv file ?</strong></p>\n<p><strong>because when we are copying data from csv file to table which has integer values throw error. right?</strong></p>\n<p>&nbsp;</p>\n<p>[/quote]</p>\n<p>Notice the &quot;Header&quot; option in the &quot;Copy from&quot; statement. This is what skips the header.</p>\n<p><code>copy RAW_TRAIN from 'D:\\\\FacebookIII\\\\Data\\\\Train.csv' csv header;</code></p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 35335,
      "author_name": "Nikhil Agarwal",
      "author_url": "",
      "post_date": "2013-11-27T04:24:57.653000",
      "content": "<p>You can update your path is Environment Variables if you think the path is the issue.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 35324,
      "author_name": "Raghu Nandan Immaneni",
      "author_url": "",
      "post_date": "2013-11-27T00:19:44.973000",
      "content": "<p>I have installed PostgreSQL and PSYCOPG, for connecting PostgreSQL to Python and retrieve data from it. &nbsp;And I am able to retrieve data successfully.</p>\n<p>Then I was using NLTK to perform some operations on the data.</p>\n<p>I got the following error:&nbsp;</p>\n<p>&quot;ImportError: No module named nltk&quot; .</p>\n<p>So I tried to install NLTK again using the URL:&nbsp;http://nltk.org/install.html.&nbsp;</p>\n<p>Output message:</p>\n<p>&quot;Requirement already up-to-date: pyyaml in /Library/Python/2.7/site-packages<br>Requirement already up-to-date: nltk in /Library/Python/2.7/site-packages<br>Cleaning up... &quot;</p>\n<p>this means that <strong>NLTK</strong> is already installed and up to date.</p>\n<p>but i cannot figure out the problem. May be during the installation of PSYCOPG ; PYTHON path has changed?&nbsp;</p>\n<p>Please help me out !</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 35268,
      "author_name": "Rakebul Hasan",
      "author_url": "",
      "post_date": "2013-11-26T13:14:30.853000",
      "content": "<p>Thanks! That's very cool. I am becoming a fan of PostgreSQL :D</p>\n<p>&nbsp;</p>\n<p>This gives the number&nbsp;of occurrences of tags. But still pretty cool to get it in one query.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 35071,
      "author_name": "Raghu Nandan Immaneni",
      "author_url": "",
      "post_date": "2013-11-23T00:20:37.007000",
      "content": "<p><strong>How to skip the header of the csv file ?</strong></p>\n<p><strong>because when we are copying data from csv file to table which has integer values throw error. right?</strong></p>\n<p>&nbsp;</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 32009,
      "author_name": "",
      "author_url": "",
      "post_date": "2013-10-04T02:15:20.327000",
      "content": "<p>Thanks Thats nice .I have just installed postgre sql and started working looks nice that it can handle large data sources</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 31994,
      "author_name": "Sashikanth Dareddy",
      "author_url": "",
      "post_date": "2013-10-03T17:43:17.067000",
      "content": "<p>Hi, you may have overlooked but the execution time (in milliseconds) is right beneath the COPY statement in my original post.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 31993,
      "author_name": "",
      "author_url": "",
      "post_date": "2013-10-03T17:35:50.060000",
      "content": "<p>Hi sashikanth&nbsp;</p>\n<p>&nbsp;</p>\n<p>how log did it take to load the data in postgresql db ?</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 30090,
      "author_name": "Marcin Pękalski",
      "author_url": "",
      "post_date": "2013-09-03T20:29:06.543000",
      "content": "<p>If somebody was interested, after reading the raw data with the script above, the folder with the table takes&nbsp;5,85GB.</p>\n<p>Just for comparison</p>\n<p>&nbsp;</p>\n<p>Query returned successfully: 6034195 rows affected, 300542 ms execution time.</p>\n<p>but that is due to the SSD drive I have. It could be faster if I had better faster SATA in my laptop.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 29901,
      "author_name": "",
      "author_url": "",
      "post_date": "2013-09-01T08:10:58.013000",
      "content": "<p>Brilliant, thanks mate! Keeping it simple saved some time.&nbsp;</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 30550,
      "author_name": "",
      "author_url": "",
      "post_date": "2013-09-09T17:42:00.440000",
      "content": "",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "29868": "",
    "35266": "",
    "36244": "",
    "35265": "",
    "35082": "",
    "35335": "",
    "35324": "",
    "35268": "",
    "35071": "",
    "32009": "",
    "31994": "",
    "31993": "",
    "30090": "",
    "29901": "",
    "30550": ""
  }
}