Cassandra和pdo php。从csv文件导入数据。如何提高性能

时间:2014-06-30 15:21:11

标签: php pdo data-modeling cql3 cassandra-2.0

我开始处理大数据。 我的输入是包含许多列的每日csv文件,我将导入这三列:

  • USER_ID
  • PARENT_ID
  • grandparent_id
  • .....

我的目标是什么?

  • 我必须统计属于两个grandparent_id(12345和67890)的所有唯一user_id值,并按parent_ids对它们进行分组
  • 我必须计算属于两个grandparent_id(12345和67890)的所有无排他唯一user_id值,并按parent_ids对它们进行分组

我设计了这个数据模型有三个表(感谢Rock建议我在之前的帖子中使用计数器)

CREATE TABLE container_u_id (
 parent_id int,
 user_id text,
 grandparent_a boolean,
 grandparent_b boolean,
 PRIMARY KEY (parent_id, user_id)
) 

CREATE TABLE unique_u_id (
  parent_id int,
  counter_uu counter,
  PRIMARY KEY (parent_id)
) 

CREATE TABLE no_exclusive_unique_u_id (
  parent_id int,
  counter_uu counter,
  PRIMARY KEY (parent_id)
)

使用php和Cassandra的pdo驱动程序导入数据 这是我的剧本

$relpath = realpath(dirname(__FILE__)) . '/';
include_once($relpath."config/init.php");


$dsn = "cassandra:host=127.0.0.1;port=9160"; 
$db = new PDO($dsn);
$db->exec("USE phpcassa");


$array_grandparent_id = array(12345, 67890);


$file_csv = new CSVFile("file.csv");



foreach ($file_csv as $row) {


    $user_id = $row['USER_ID'];
    $parent_id = $row['PARENT_ID'];



    if(in_array($row['GRANDPARENT_ID'], $array_grandparent_id)){

        $is_grandparent_a  = ($row['GRANDPARENT_ID']==12345) ? 'TRUE' : 'FALSE';
        $is_grandparent_b = ($row['GRANDPARENT_ID']==67890) ? 'TRUE' : 'FALSE';


        $query= "INSERT INTO container_u_id (user_id, parent_id, grandparent_a, grandparent_b) VALUES('".$user_id."', ".$parent_id.", ".$is_grandparent_a.", ".$is_grandparent_b." ) IF NOT EXISTS;";
        $stmt = $db->prepare($query);
        $status_exec = $stmt->execute();
        $result_insert = $stmt->fetchAll();

        $is_uu = ($result_insert[0]['[applied]']) ? True : False;

        if($is_uu===False){

        $status_grandparent_a = $result_insert[0]['grandparent_a'];
        $status_grandparent_b = $result_insert[0]['grandparent_b'];


            //if user belong to both grandparents then it is already present onto both counter tables 
            if($status_grandparent_a===False || $status_grandparent_b===False){

                if($status_grandparent_a===False && $is_grandparent_a=='TRUE'){

                    $query = "UPDATE container_u_id SET grandparent_a = true WHERE user_id='".$user_id."' AND parent_id = ".$parent_id.";";
                    $stmt = $db->prepare($query);
                    $status_exec = $stmt->execute();

                    $query= "UPDATE no_exclusive_so SET counter_uu = counter_uu + 1 WHERE parent_id = ".$parent_id.";";
                    $stmt = $db->prepare($query);
                    $status_exec = $stmt->execute();


                }elseif($status_grandparent_b===False && $is_grandparent_b=='TRUE'){

                    $query = "UPDATE container_u_id SET grandparent_b = true WHERE user_id='".$user_id."' AND parent_id = ".$parent_id.";";
                    $stmt = $db->prepare($query);
                    $status_exec = $stmt->execute();

                    $query= "UPDATE no_exclusive_so SET counter_uu = counter_uu + 1 WHERE parent_id = ".$parent_id.";";
                    $stmt = $db->prepare($query);
                    $status_exec = $stmt->execute();

                }


            }

        }

        if($is_uu){

            $query= "UPDATE unique_u_id SET counter_uu = counter_uu + 1 WHERE parent_id = ".$parent_id.";";
            $stmt = $db->prepare($query);
            $status_exec = $stmt->execute();
        }            
    }  
}

然后从这个csv示例开始

Yzc4Jknl0-3c, 34, 12345
Yzc4Jknl0-3c, 34, 67890 
Yzc4Jknl0-3c, 34, 12345
01w44Xz0w3c1, 13, 12345
01w44Xz0w3c1, 21, 12345
01w44Xz0w3c1, 21, 12345  
qFxg0023Exy4, 21, 67890

在代码的最后,表格显示了这些结果

SELECT * FROM container_u_id;   
 parent_id | user_id      | grandparent_a | grandparent_b
-----------+--------------+---------------+--------------
     34    | Yzc4Jknl0-3c |     True      |    True
     13    | 01w44Xz0w3c1 |     True      |    False
     21    | 01w44Xz0w3c1 |     True      |    False
     21    | qFxg0023Exy4 |     False     |    True


SELECT * FROM unique_u_id;
 parent_id | counter_uu
-----------+------------
 34        |       1
 13        |       1   
 21        |       2 


SELECT * FROM no_exclusive_unique_u_id;
 parent_id | counter_uu
-----------+------------
 34        |       1

现在,让我们开始说出我的怀疑。 该脚本有效,但速度很慢。对于获取5000万行,脚本花费的时间为8到10个小时。 开发环境是:

  • ubuntu 12.04 64 bit
  • cqlsh 4.1.1
  • Cassandra 2.0.6
  • CQL规范3.1.1
  • Thrift protocol 19.39.0
  • cpu:i3 M 370
  • 存储:旋转磁盘5400rpm
  • 响铃:具有单个节点的群集

因此,我想改善表现......我的问题是:

  1. 我的数据建模有什么问题吗?
  2. 我是否需要添加更多节点并更改硬件(aws ec2)?

0 个答案:

没有答案