【问题标题】:How can I generate 10 million random alphanumeric records in vertica sql?如何在vertica sql中生成1000万条随机字母数字记录?
【发布时间】:2019-02-23 06:47:39
【问题描述】:

我需要在 vertica 数据库表中生成 1 亿条随机字母数字 12 字符记录,或者以任何其他方式生成 1 亿条随机字母数字 12 字符记录文件。然后我可以在数据库中加载文件。 以下是示例记录

例如:BAN1334HNAD1

 GEN1235NDA12   

【问题讨论】:

标签: java python sql shell vertica


【解决方案1】:

我没有要测试的 Vertica 实例,但这应该会给您一个想法:

with t (l) as (
  select 'A' union all select 'B' union all select 'C' union all 
  select '1' union all select '2' union all select '3'
)
select t1.l || t2.l || t3.l 
from t t1, t t2, t t3 
order by random() 
limit 10

【讨论】:

  • 这是一个在 Vertica 中直接为我运行的简洁答案......但是......如果它扩展到 AZ 和 0-9,有 12 个字符,运行需要很长时间.我刚刚用列出的输出 3 进行了尝试,它花费了 125 毫秒,4 花费了 165,6 1659 毫秒,7 花费了 267 毫秒,8 花费了 365,000 毫秒。我不想等待比这更大的事情。我建议准备 Vertica 外部的数据并使用 COPY 运行它。
【解决方案2】:

这将满足您的需要:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int ii,jj;
    char list[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int len=strlen(list);
    char retStr[14]={0};
    for(ii=0; ii<100*1000*1000; ii++) {
        memset(retStr,0,sizeof(retStr));
        for(jj=0; jj<12; jj++) {
            int sub=random()%len;
            retStr[jj]=list[sub];
        }
        printf("%s\n", retStr);
    }
}

我将它编译成一个名为 rand12 的程序并创建了一个 rand12 表:

create table rand12 (a varchar2(12));

然后运行数据:

./rand12 |
~/opt/vertica/bin/vsql -c  "copy rand12 (a) from local stdin
delimiter '|' abort on error commit;"

这可以很容易地用 Java 或 Python 实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2021-04-05
    相关资源
    最近更新 更多