【问题标题】:Generate unique hash for amazon purchase order report为亚马逊采购订单报告生成唯一哈希
【发布时间】:2020-03-07 10:36:24
【问题描述】:

我的问题是我想不出一种方法来为亚马逊采购订单报告生成唯一哈希,因此我可以在我的 MySQL 表中导入它们而不会重复。

我现在的做法是:

  1. 我将 CSV 文件(示例 => https://gofile.io/?c=A8McUw)加载到关联数组中

  2. 我使用 md5 生成唯一密钥,如下所示:

$hash = md5($row['order_id'] . $row['asin'] . $row['product_category'] . $row['seller_name'] . $row['order_quantity'] . $row['item_quantity'])

  1. 我检查 $hash 键是否存在,如果存在,我更新条目,如果不存在,它将插入一个新条目

这已经工作了几次,但现在我不断收到重复的条目,我猜这是因为每次添加新订单时行的顺序都会不断变化。我查看了报告,这些列是唯一不变的列,可用于唯一标识行。

【问题讨论】:

  • 使用带有微秒的时间戳并使用 Sha 对其进行哈希处理。 md5不够好,可能有重复
  • 在生成唯一标识符时需要牢记几件事。你能用我在“Unique Random Identifiers”中给出的六个问题的答案来编辑你的问题吗?
  • “唯一哈希”是矛盾的。

标签: php


【解决方案1】:

我找到了一个简单的解决方案,该解决方案使用他们从亚马逊生成的 CSV 中输入的项目顺序来工作。唯一的缺点是亚马逊突然决定更改订单。

class Foo
{
    private $itemIndex = [];

    private function generateUniqueIdentifierForRow(array $row, $i = 1)
    {
        $str = $row['order_id'] . $row['asin'] . $i;

        return hash('sha256', $str);
    }

    public function uniqueRows($rows)
    {
        $uniqueRows = [];

        foreach ($rows as $row) {
            $hash = $this->generateUniqueIdentifierForRow($row);

            if (isset($this->hashDuplicatesIndexes[$hash]) || array_key_exists($hash, $this->itemIndex)) {
                $this->itemIndex[$hash]++;
                $hash = $this->generateUniqueIdentifierForRow($row, $this->itemIndex[$hash]);
            } else {
                $this->itemIndex[$hash] = 1;
            }

            $row['hash'] = $hash;
            $uniqueRows[] = $row;
        }

        return $uniqueRows;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多