• 利用datafaker批量生成测试数据


    一、安装datafaker模块

    pip install datafaker

    二、安装对应数据库包

    对于不同的数据库需要用到不同的python包,若在执行过程中报包缺失问题。 请pip安装对应包

    数据库

    python包

    备注

    mysql/tidb

    mysql-python/mysqlclient

    windows+python3请使用mysqlclient

    oracle

    cx-Oracle

    同时需要下载orale相关库

    postgresql/redshift

    psycopg2

    根据sqlachemy选择对应包

    sqlserver

    pyodbc

    mssql+pyodbc

    Hbase

    happybase,thrift

    es

    elasticsearch

    hive

    pyhive

    kafka

    kafka-python

    安装过程

    pip install psycopg2 -i https://mirrors.aliyun.com/pypi/simple/

    pip临时加速地址

    国内主要镜像地址如下:

    清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:https://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 华中理工大学:http://pypi.hustunique.com/ 山东理工大学:http://pypi.sdutlinux.org/ 豆瓣:http://pypi.douban.com/simple/

    故而临时加速pip速度的命令为:

    pip install == -i https://mirrors.aliyun.com/pypi/simple/

    永久提速

    Windows系统配置

    1、在 C:\Users\Administrator\pip 建一个文件 pip.ini如果Administrator 中 没有pip文件夹则自己新建一个,然后新建一个 pip.ini 文件

    2、在 pip.ini 文件输入:

    [global] index-url=https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com

    使用记事本默认的ANSI编码格式复制上面的文本粘贴即可。

    Mac/Linux系统配置

    1、打开terminal

    2、输入命令:

    mkdir .pip vim .pip/pip.conf

    这两步是在home目录下新建文件: .pip/pip.conf)

    按 i 键进入输入模式,在这个文件中复制粘贴写入如下内容:

    [global] index-url = https://mirrors.aliyun.com/pypi/simple/ timeout = 1000

    [install] use-mirrors = true mirrors = https://mirrors.aliyun.com//

    实际操作

    1. #step1 查看python的版本信息
    2. $ python3 --version Python 3.7.10
    3. #step2 创建虚拟运行环境
    4. $ python3 -m venv env
    5. $ source env/bin/activate (env
    6. #查看虚拟运行环境中目前已安装的python模块
    7. (env) $ pip3 list 
    8.  #安装datafaker模块
    9. (env) $ pip3 install datafaker
    10. # 安装psycopy模块
    11. (env) $ pip3 install psycopg
    12. # step4 安装mysql-client模块
    13. (env) $ pip3 install mysql-client 
    14. Amazon Linu2部署安装MySQL8
    15. #step1 添加mysql仓库
    16. sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
    17. #step2 安装mysql服务软件
    18. sudo yum -y install mysql-community-server
    19. #step3 启动mysql服务
    20. sudo systemctl enable --now mysqld
    21. #step4 获取mysql临时密码
    22. sudo grep 'temporary password' /var/log/mysqld.log
    23. #step5 修改密码
    24. $sudo mysql_secure_installation -p 【使用获取的临时密码登录】
    25. $ sudo mysql_secure_installation -p
    26. Enter password: Securing the MySQL server deployment.
    27. The existing password for the user account root has expired.
    28. Please set a new password.
    29. New password:
    30. Re-enter new password:
    31. The 'validate_password' component is installed on the server.
    32. The subsequent steps will run with the existing configuration of the component.
    33. Using existing password for root.
    34. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : No
    35. ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them.
    36. This is intended only for testing, and to make the installation go a bit smoother.
    37. You should remove them before moving into a production environment.
    38. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
    39. Success. Normally, root should only be allowed to connect from 'localhost'.
    40. This ensures that someone cannot guess at the root password from the network.
    41. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
    42. Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
    43. - Dropping test database...
    44. Success. - Removing privileges on test database...
    45. Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
    46. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
    47. Success. All done!
    48. #step6 验证使用修改之后的密码是否可以正常登录操作mysql数据库
    49. $ mysql -uroot -p
    50. mysql>
    51. #Step7 创建数据库demo
    52. mysql>create database demo;
    53. #step8 切换到demo数据库
    54. mysql> use demo;
    55. #Step9 创建表
    56. create table stu ( id int unsigned auto_increment primary key COMMENT '自增id', name varchar(20) not null comment '学生名字', school varchar(20) not null comment '学校名字', nickname varchar(20) not null comment '学生小名', age int not null comment '学生年龄', class_num int not null comment '班级人数', score decimal(4,2) not null comment '成绩', phone bigint not null comment '电话号码', email varchar(64) comment '家庭网络邮箱', ip varchar(32) comment 'IP地址', address text comment '家庭地址' ) engine=InnoDB default charset=utf8;
    57. #Step10 编写刚创建好的stu表的元数据
    58. $ vi meta.txt (env)
    59. $ cat meta.txt
    60. id||int||自增id[:inc(id,1)]
    61. name||varchar(20)||学生名字
    62. school||varchar(20)||学校名字[:enum(file://names.txt)]
    63. nickname||varchar(20)||学生小名[:enum(鬼泣, 高小王子, 歌神, 逗比)]
    64. age||int||学生年龄[:age]
    65. class_num||int||班级人数[:int(10, 100)]
    66. score||decimal(4,2)||成绩[:decimal(4,2,1)]
    67. phone||bigint||电话号码[:phone_number]
    68. email||varchar(64)||家庭网络邮箱[:email]
    69. ip||varchar(32)||IP地址[:ipv4]
    70. address||text||家庭地址[:address]
    71. #step11 设置学校可获取的值信息names.txt文件
    72. $ cat names.txt 清华中学 人和中心 广东中学 猪场 旧大院
    73. #step12 安装pymysql模块
    74. $ pip3 install pymysql
    75. Collecting pymysql Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.8/43.8 kB 8.6 MB/s eta 0:00:00 Installing collected packages: pymysql Successfully installed pymysql-1.0.2

    元数据列说明

    meta.txt文件中每行数据为元数据的一个字段描述,以||分割为三列,若以#开头,则忽略该行。

    • 第一列:字段名
    • 第二列:表字段类型
    • 第三列:字段注释,其中包含构造规则标识

    name不加标记则会随机产生20字符内的字符串,可以加上改为:学生名字[:name]

    其中学校名字[:enum(file://names.txt)]表示从本地文件names.txt中读取枚举数据,表示学校名称只能从下面这5所学校中随机产生。names.txt内容如下:

    清华中学

    人和中心

    广东中学

    猪场

    旧大院

    从本地meta.txt中读取元数据,以,,分隔符构造10条数据,打印到屏幕上

    $ datafaker rdb mysql+pymysql://root:Aa123456#@localhost:3306/test?charset=utf8 stu 10 --outprint --meta meta.txt --outspliter ,, 

     命令说明: datafaker连接mysql使用的数据库类型为rdb或者mysql。

    mysql+pymysql为其使用的包

    root为mysql数据库连接的用户名

    Aa123456#为数据库连接使用的密码

    3306为mysql的端口号

    将生成的数据直接写入mysql中

    #step1 清空mysql表数据

    delete from stu;

    或者使用

    truncte table stu;

    #Step2 将生成的数据加载到mysql数据库的表中去

    $ datafaker mysql mysql+pymysql://root:Aa123456#@localhost:3306/demo?charset=utf8 stu 10 --meta meta.txt insert 10 records time used: 0.657 s 

    注意事项

    若要再次运行,需要修改meta.txt文件中为

    id[:inc(id,11)]起始值为11或更大值,不然数据库会报主键重复错误。

    验证数据是否成功插入到MySQL中去

    参考资料

    datafaker/README.md at master · gangly/datafaker · GitHub

    datafaker --- 测试数据生成工具-阿里云开发者社区

    Installation - psycopg 3.1.dev0 documentation

     datafaker测试数据生成工具_嘻嘻嘻277的博客-CSDN博客

  • 相关阅读:
    m多载波MC-CDMA系统单用户检测方法的研究,对比EGC,MRC,ORC以及MMSE
    电脑系统还原怎么操作?
    一种高效的同态加密方案及其应用-解读
    王道数据结构——栈在括号匹配中的应用
    计算机网络工程毕业设计题目选题大全
    SpringBoot基础入门
    条件判断指令分析 || JVM类加载与字节码技术
    用antdPro表单时,defaultValue设置了默认值,ModalForm的onFinish获取不到该默认值
    bugku-web-社工-初步收集
    构建出自己的archetype-java脚手架
  • 原文地址:https://blog.csdn.net/qq_42048263/article/details/126371689