2

我目前在 MySQL 中有一个表,看起来像

id | parameters
1    gender=male;location=london;age=32
2    gender=female;location=manchester
3    speaks=french/english;gender=male

我想将它加载到一个表中,该表有 3 列分隔参数但保留 id,例如任何想法我可以如何做到这一点以及我可以使用哪些方法。请记住,有数百万行,所以不能太慢。谢谢

id | key    | value
1    gender   male
1    location london
1    age      32
2    gender   female
2    location manchester
3    speaks   french/english
3    gender   male
4

1 回答 1

2

您可以编写一个小脚本来创建输入 csv 提要文件。类似这样的东西——

awk '
BEGIN { 
    print "id,key,value"
}
NR>1 {    
    j=1
    split ($2, a, ";")
    for (i=1; i<=length(a); i++) {
        split (a[i], b, "=")
        printf "%s,%s,%s\n",NR-1,b[j],b[j+1]
    }
}' file

测试:

[jaypal:~/Temp] cat file
id | parameters
1    gender=male;location=london;age=32
2    gender=female;location=manchester
3    speaks=french/english;gender=male

[jaypal:~/Temp] awk '
BEGIN { 
    print "id,key,value"
}
NR>1 {    
    j=1
    split ($2, a, ";")
    for (i=1; i<=length(a); i++) {
        split (a[i], b, "=")
        printf "%s,%s,%s\n",NR-1,b[j],b[j+1]
    }
}' file
id,key,value
1,gender,male
1,location,london
1,age,32
2,gender,female
2,location,manchester
3,speaks,french/english
3,gender,male
于 2013-05-27T17:25:00.197 回答