メンチカツ

ロースカツが好きです

Amazon SimpleDBにcsvを読み込んでインポートするphp置いておきますね

とくにたいしたこともしていないけど、どこかでだれかの
お役にたてたらいいなくらいのものですよ。

つかいかた

1ぎょうめにアトリビュート名
1れつめにitemNameにするID
[既にあるドメイン名].csv というファイル名

のファイルを、csvってフォルダをつくっていれておきます。

csvフォルダとおなじ場所に、適当なphpファイル名
(ここではcsv_to_sdb.phpにしときます)でファイルを
つくって以下のソースをコピペします。

<?php
require_once '/path/to/aws-sdk-1.5.11/sdk.class.php';

try
{
    $cts = new CsvToSdb();
    $cts->import();
}
catch (Exception $e)
{
    var_dump($e);
}

class CsvToSdb
{
    const MAX_BATCH_PUT = 25;
    protected $sdb = null;
    protected $files = null;

    public function __construct()
    {
        try
        {
            $this->sdb = new \AmazonSDB();
            $this->sdb->set_region('[リージョンを設定してね]');
        }
        catch (\SDBException $e)
        {
            throw new \Database_Exception($e->getMessage(), $e->getCode(), $e);
        }

        try
        {
            if ($handle = opendir(dirname(__FILE__).'/csv')) {

                while (false !== ($file = readdir($handle)))
                {
                    if (preg_match('/(.*?).csv$/', $file, $matches))
                    {
                        $this->files[] = array('name'=>dirname(__FILE__)."/csv/$file", 'domain'=>$matches[1]);
                    }
                }
                closedir($handle);
            }
        }
        catch (Exception $e)
        {
            throw $e;
        }
    }

    public function import()
    {
        if($this->files === null) return;
        setlocale(LC_ALL, 'ja_JP.UTF-8');

        try
        {
            foreach ($this->files as $f)
            {
                $fp = fopen($f['name'],"r");
                $headers = null;
                $items = null;
                $put_count = 0;

                while($data = fgetcsv($fp))
                {
                    if($headers === null)
                    {
                        foreach ($data as $k => $v)
                        {
                            $headers[$k] = $v;
                        }
                    }
                    else
                    {
                        $itemName = null;
                        $attributes = null;
                       
                        foreach ($data as $k => $v)
                        {
                            //itemName is convert to zero padding format.
                            if($itemName === null) $itemName = sprintf("%010s", $v);
                            $attributes[$k] = $v;
                        }

                        foreach ($headers as $k => $v)
                        {
                            $items[$itemName][$v] = $attributes[$k];
                        }

                        if(count($items) === self::MAX_BATCH_PUT)
                        {
                            $result = $this->sdb->batch_put_attributes($f['domain'], $items, true, null);
                            $put_count += count($items);
                            echo "executing 'batch_put_attributes' count at $put_count ...\n";
                            $items = null;
                        }
                    }
                    if ($headers === null) break;
                }
                fclose($fp);

                if(count($items) > 0)
                {
                    $result = $this->sdb->batch_put_attributes($f['domain'], $items, true, null);
                    $put_count += count($items);
                    echo "executing 'batch_put_attributes' count at $put_count ...\n";
                    $items = null;
                }
            }
        }
        catch (Exception $e)
        {
            throw $e;
        }
    }
}

aws-sdkとリージョンの設定もしておいてください。

おわったら、

php csv_to_sdb.php

ってすると、まあおもったとおりのうごきをします。