我目前在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
答案 0 :(得分: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