当前位置:   article > 正文

【小工具-模拟数据】Python实现自动针对Postgres SQL docker容器执行SQL脚本文件_docker进入pgsql并执行sql脚本

docker进入pgsql并执行sql脚本

1.工具简要说明

自动生成SQL脚本文件,自动针对docker运行容器形式的PG数据库进行数据模拟插入。可以以此为基础进行脚本改造,达到模拟大数据的用途。源码的写法比较土,后面有空可以优化一下代码。

2.工具源码

python文件名:init_db.py

  1. #!/usr/bin/python
  2. #create time: 2022/03/12 by epic
  3. import os;
  4. def get_db_container_id():
  5. print('*** get_db_container_id');
  6. docker_ps = os.popen('docker ps|grep postgres').readlines();
  7. container_info = docker_ps[0].split();
  8. return container_info[0];
  9. def generate_sql_script_file(sql_script_file, number):
  10. os.system('rm -f ' + sql_script_file);
  11. os.system('touch ' + sql_script_file);
  12. for i in range(number):
  13. sno = str(i + 1) + '';
  14. sname = 'stu' + sno;
  15. sex = 1;
  16. os.system("echo delete from student where sno=\\'" + sno + "\\'\\; >> " + sql_script_file);
  17. os.system("echo insert into student values\\(\\'" + sno +"\\',\\'"+sname+"\\',"+str(sex)+"\\)\\; >> " + sql_script_file);
  18. return;
  19. def copy_script_file_to_container(src_script_file,
  20. dest_script_file,
  21. container_id):
  22. print('*** copy_script_file_to_container: ' + src_script_file + ',' + dest_script_file + ',' + container_id);
  23. return os.system('docker cp ' + src_script_file + ' ' + container_id + ':' + dest_script_file);
  24. #
  25. #chown postgres:postgres /media/init_db_script.sql
  26. #su - postgres -c "psql -d db1 -f /media/init_db_script.sql"
  27. #
  28. def generate_host_init_db_shell(host_init_db_shell,
  29. container_init_db_shell,
  30. container_init_db_script):
  31. print('*** generate_host_init_db_shell: ' + host_init_db_shell + ',' + container_init_db_shell + ',' + container_init_db_script);
  32. os.system('mv ' + host_init_db_shell + ' ' + host_init_db_shell + '.bak');
  33. os.system('touch ' + host_init_db_shell);
  34. os.system('echo chown postgres:postgres ' + container_init_db_shell + '>> ' + host_init_db_shell);
  35. os.system('echo chown postgres:postgres ' + container_init_db_script + '>> ' + host_init_db_shell);
  36. os.system('echo su - postgres -c \\"psql -d db1 -f ' + container_init_db_script + '\\" >> ' + host_init_db_shell);
  37. return;
  38. #
  39. #docker exec -it 1deab09a9907 /bin/sh -c 'sh /media/init_db.sh'
  40. #
  41. def exec_script_file(container_id,
  42. container_init_db_shell):
  43. print('*** exec_script_file: ' + container_id + ',' + container_init_db_shell);
  44. os.system('docker exec -it ' + container_id + " /bin/sh -c ' sh " + container_init_db_shell + "'");
  45. return;
  46. def main():
  47. print("^*********************--------------------***********************^");
  48. container_id = get_db_container_id();
  49. host_init_db_shell = "/root/init_db.sh";
  50. host_init_db_script = "/root/init_db_script.sql";
  51. container_init_db_shell = "/media/init_db.sh";
  52. container_init_db_script = "/media/init_db_script.sql";
  53. number = 2;
  54. generate_host_init_db_shell(host_init_db_shell, container_init_db_shell, container_init_db_script);
  55. copy_script_file_to_container(host_init_db_shell, container_init_db_shell, container_id);
  56. generate_sql_script_file(host_init_db_script, number);
  57. copy_script_file_to_container(host_init_db_script, container_init_db_script, container_id);
  58. exec_script_file(container_id, container_init_db_shell);
  59. print(">>>>>>insert number: " + str(number));
  60. print("^*********************--------------------***********************^");
  61. return;
  62. main();

运行python脚本文件进行数据模拟,最后打印了模拟了多少条数据:

  1. [root@localhost ~]# ./init_db.py
  2. ^*********************--------------------***********************^
  3. *** get_db_container_id
  4. *** generate_host_init_db_shell: /root/init_db.sh,/media/init_db.sh,/media/init_db_script.sql
  5. *** copy_script_file_to_container: /root/init_db.sh,/media/init_db.sh,6825fcf46076
  6. *** copy_script_file_to_container: /root/init_db_script.sql,/media/init_db_script.sql,6825fcf46076
  7. *** exec_script_file: 6825fcf46076,/media/init_db.sh
  8. DELETE 0
  9. INSERT 0 1
  10. DELETE 0
  11. INSERT 0 1
  12. >>>>>>insert number: 2
  13. ^*********************--------------------***********************^

-----------

运行python脚本后生成SQL脚本文件和shell脚本文件,并且自动拷贝到指定的容器中,接着自动在容器中执行SQL文件。下面2个文件是生成的中间文件:

文件1:生成shell脚本文件init_db.sh内容为:

  1. chown postgres:postgres /media/init_db.sh
  2. chown postgres:postgres /media/init_db_script.sql
  3. su - postgres -c "psql -d db1 -f /media/init_db_script.sql"

文件2:生成的SQL文件init_db_script.sql内容为:

  1. [root@localhost ~]# cat init_db_script.sql
  2. delete from student where sno='1';
  3. insert into student values('1','stu1',1);
  4. delete from student where sno='2';
  5. insert into student values('2','stu2',1);

3. 运行后的效果

  1. [root@localhost ~]# docker exec -it 6825fcf46076 /bin/sh
  2. # su - postgres
  3. postgres@6825fcf46076:~$ psql
  4. psql (10.17 (Debian 10.17-1.pgdg90+1))
  5. Type "help" for help.
  6. postgres=# \c db1
  7. You are now connected to database "db1" as user "postgres".
  8. db1=# truncate student;
  9. TRUNCATE TABLE
  10. db1=# select * from student;
  11. sno | sname | sex
  12. -----+-------+-----
  13. 1 | stu1 | 1
  14. 2 | stu2 | 1
  15. (2 rows)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/401049
推荐阅读
相关标签
  

闽ICP备14008679号