mongo import 和 mutip_btc_tx报错总结

2020-07-13  本文已影响0人  程序里的小仙女

1.在利用脚本导入的时候报错

Failed: error processing document #2: invalid character ',' looking for beginning of value

分析:https://segmentfault.com/a/1190000014796995?utm_source=tag-newest


解決:
代码定位:



第二种办法就是写脚本去除json文件每行最后的" ,"

2.报错mongo库新建db无权限


3.转化效率:

screen -S btc 
python3 +文件名.py  运行脚本
screen -r btc

json文件大小 14.5MB 25050条数据 转化用时5.17秒
json文件大小 3.85GB 条数据 转化用时314.5秒
25分钟 处理json文件 21GB
一个小时处理 了 44GB文件
两个小时处理 了 86GB文件
共356 GB 共319个json文件 预计处理时间 8.091小时
查看文件夹的大小

cd  /backup/btc/btc_outputList_tx
du -sh  

btc_new_tx数据格式:



  1. 运行一半的时候报错:
 File "btc_new_tx_read_save.py", line 78, in get_outputList
    "address": vo1["address"][0],


5.运行mutip_btc_tx.py脚本的时候,报错:
87行

string index out of range 

原因;
数据结构发生变化:



对比浏览器:


解决:
在遍历vout的"address"的值得时候,进行数据类型判断,如果vout的address 是list,就切片取值,如果是"",则忽略!

6.报错:

Unterminated string starting at: line 1 column 488 (char 487)

7.成功的格式:



8.处理后格式检验:



两个vin 一样的地址:



9.导入mongo库的时候报错:


再多个文件夹中,查找一个内容: 862e0306cf2d3f4d9b90d7a2c95582171d125516bcc64f96b0d327547c18ba3b
grep -n "862e0306cf2d3f4d9b90d7a2c95582171d125516bcc64f96b0d327547c18ba3b" -r ./

得到结果:



原始数据导出:



说明导出文件中只有一条没有问题,
查数据库:

说明数据库也只有一条,
说明数据重复!!!

最後重新用多进程将原数据(再查数据库确认数据无重复,正确的时候)转换清洗一遍保存,再导入mongo数据库就不报数据重复(due _id,duplicated key erro colleection)
重新清洗的数据大小为523GB



入库重复时的数据为:524GB


1.redis任务队列的个数和原文件的个数的对应(也就是要清理redis的db) redis.expire("json_path", 0),
2.一定要注意保存的路径的干净(将保存路径删除干净后再运行清洗程序)

3.分析:
解决mongodb id 重复问题
mongodb在插入数据时出现id重复错误,

百度了一下,发现很多人都出现了类似问题,有人说手动删除自生成的id就能解决, 有人说清空集合就好了。
结果是然并卵,数据库的id貌似是根据 时间戳+主机+进程号+序列生成的。重复的原因个人猜测可能有两种:
内部原因: 同时插入两条数据,导致数据库生成了同一个id值。
外部原因:每次使用同一个变量存储不同的数据,导致数据库认为每次存储的是同一条数据,最终生成同一个id值。

以上都有个共同点,就是让数据库自己生成id值。
后来找到一篇stockoverflow中帖子,给了我启示.

 If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error.

You can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.
Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value.

大概意思是: 由于独特的约束,MongoDB只会允许一个文件,缺少索引字段。如果有一个以上的文档没有索引字段的值或缺少索引字段就会报错

于是自己手动添加id值,当插入的数据带有_id的字段时,mongodb就不再自动生成id。
至此, mongodb重复id的问题就解决了。当然,肯定还有更好的解决办法,后续想到了再补充。

原文链接:https://blog.csdn.net/Xiongtm/article/details/77650448

上一篇下一篇

猜你喜欢

热点阅读