function testgrp() { // connect $m = new MongoClient(); // select a database $db = $m->selectDB('trend'); // select a collection (analogous to a relational database's table) $colnames = ['housesale', 'aptsale', 'flatsale', 'houserent', 'aptrent', 'flatrent']; foreach ($colnames as $colname) { $col2name = $colname . "_agg"; $col2 = new MongoCollection($db, $col2name); // Let's remove all first $col2->drop(); // add agg information mkgrp($db, $colname); } }
function readCSV($dir, $csvFile, $tableName) { // connect $m = new MongoClient(); // select a database $db = $m->trend; $collection = $db->{$tableName}; // Get yeat and month from csvFile list($year, $month, $rest) = explode("_", $csvFile, 3); $year = intval($year); $month = intval($month); assert($year != null && $month != null); $row = 0; if (($handle = fopen("{$dir}/{$csvFile}", "r")) == FALSE) { echo "{$dir} . {$csvFile} not found!"; return; } while (($data = fgetcsv($handle, 10000000, ",")) !== FALSE) { // table head field if ($row++ == 0) { $thdata = array_values($data); $fields = getFields($data); $types = getTypes($fields); // Num of Fields $numFields = count($fields); // Add meta fields $fields[] = "year"; $fields[] = "month"; $fields[] = "state"; $fields[] = "city"; $fields[] = "county"; $fields[] = "region"; //$fields[] = "xlsrow"; // types $types[] = "i"; $types[] = "i"; $types[] = "s"; $types[] = "s"; $types[] = "s"; $types[] = "s"; //$types[] = "i"; print_r($fields); //make a unique/index index //makeDBIndex($db, $collection, $fields); continue; } // Another table head? $diff = array_diff($thdata, $data); // all same? if (count($diff) == 0) { echo "Skip another table head"; continue; } $num = count($data); if ($num != $numFields) { echo "<!> {$num} fields in line {$row}!\n"; print_r($data); continue; } assert($fields); // add year and month $data[] = $year; $data[] = $month; list($state, $city, $county, $region) = explode(" ", trim($data[0]), 4); // data 0 should be the full loc $data[] = $state; $data[] = $city; $data[] = $county; $data[] = $region; $data[] = $row; // echo "$data[0] $data[1]"; // Let's insert insertDB($db, $collection, $types, $fields, $data); } fclose($handle); // mk grpo echo "<!> Inserted {$row} rows!\n"; echo "<!> making agg for {$year}/{$month}...\n"; mkgrp($db, $tableName, $year, $month); }