30 리눅스의 마리아 디비와 파이썬 연동하기
중요한 데이터는 전부 데이터 베이스에 저장하고 관리합니다.
새롭게 데이터가 들어오면 insert하고 수정이 생기면 SQL로 update를 합니다.
왜 현업에서는 database와 파이썬을 연동해서 사용하는가?
database는 계속 데이터 변동이 실시간으로 일어나고 있습니다. 그 변경된 데이터로 데이터 분석을 하거나 시각화를 해야하는데 만약 연동하지 않는다면 테이블을 csv파일로 os로 내려 이 내린 csv파일을 파이썬의 판다스 데이터 프레임으로 생성해야 합니다. 그런데 회사에 분석해야할 테이블은 아주 많고 그리고 그 양도 상당히 큽니다. 그래서 csv파일로 내리지 않고 바로 연동해서 분석을 하는 것입니다.
예제1. 연동하지 않았을 경우, 마리아 디비의 테이블을 os에 csv file로 내려야하는데 내리는 방법 배우기
1) putty에서 root유저로 접속합니다.
# mysql -u root -p
MariaDB [(none)]> use orcl;
2) 다음의 쿼리를 /tmp 밑에 emp100.csv로 저장하시오.
MariaDB [orcl]> select *
from orcl.emp
into outfile '/tmp/emp100.csv'
fields enclosed by '"'
terminated by ','
lines terminated by '\r\n';
3) 마리아 디비를 빠져 나와서 /tmp에 emp100.csv가 있는지 확인합니다.
MariaDB [orcl]> exit;
# cd /tmp
# ls -l emp100.csv
예제2. 모바텀으로 리눅스의 /tmp 밑에 있는 emp100.csv를 윈도우로 내리세요.
모바텀에서 /tmp 폴더를 검색해 emp100.csv 파일을 찾고, 그 파일을 윈도우로 다운로드하면 됩니다.
문제167. 마리아 디비의 dept테이블을 /tmp 밑에 있는 dept100.csv로 내리시오.
# mysql -u root -p
MariaDB [(none)]> use orcl;
MariaDB [orcl]> select *
from orcl.dept
into outfile '/tmp/dept100.csv'
fields enclosed by '"'
terminated by ','
lines terminated by '\r\n';
MariaDB [orcl]> exit;
# cd /tmp
# ls -l dept100.csv
+) 조건절을 넣어서 파일 내리기 가능함.
MariaDB [orcl]> select e.ename, d.loc
from emp e, dept d
where e.deptno = d.deptno;
MariaDB [orcl]> select e.ename, d.loc
from emp e, dept d
where e.deptno = d.deptno
into outfile '/tmp/result100.csv'
fields enclosed by '"'
terminated by ','
lines terminated by '\r\n';
문제168. (오늘의 마지막 문제) naver2 테이블에서 영화이름이 모가디슈인 모든 행의 데이터를 추출하여 /tmp밑에 result200.csv로 생성하고 cat으로 result200.csv를 연 화면을 캡쳐해서 올리세요.
[root@localhost tmp]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 26
Server version: 10.4.24-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use orcl;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [orcl]> select * from orcl.naver2 where cname = '모가디슈' into outfile '/tmp/result200.csv' fields enclosed by '"' terminated by ',' lines terminated by '\r\n';
Query OK, 3568 rows affected, 1 warning (0.006 sec)
MariaDB [orcl]> exit;
Bye
[root@localhost tmp]# ls -l result200.csv
-rw-r--r--. 1 mysql mysql 453124 3월 16 17:05 result200.csv
select *
from orcl.naver2 -> 현재 데이터베이스가 하나밖에 없어서 naver2라고 해도 가능함
where cname = '모가디슈'
into outfile '/tmp/result200.csv'
fields enclosed by '"'
terminated by ','
lines terminated by '\r\n';
'Study > class note' 카테고리의 다른 글
리눅스 / 마리아디비 파이썬 연동해서 시각화하기1(코로나 데이터) (0) | 2022.03.17 |
---|---|
리눅스 / 리눅스의 마리아 디비와 파이썬 연동하기2 (0) | 2022.03.17 |
리눅스 / 주피터 노트북 설치하기 + 원격으로 주피터노트북 실행하기 (0) | 2022.03.16 |
리눅스 / 아나콘다 설치하기 (0) | 2022.03.16 |
리눅스 / 원격제어를 이용해서 리눅스 서버에 접속하는 방법 (0) | 2022.03.16 |