Skip to content

Commit

Permalink
[FEAT] 버스 관련 정적데이터 가공 (#71)
Browse files Browse the repository at this point in the history
## 버스 관련 정적데이터 가공
- 버스 노선 정보(bus_route.csv)
- 버스 노선-정거장 정보(bus_route_station.csv)
- 버스 정거장 정보(bus_station.csv)

## DB SQL 코드 작성
- Schema.sql 작성
- csv 파일 load data 스크립트 작성
- api_route_id, api_station_id -> route_id, station_id 매칭
- db 초기화 스크립트 작성

---------

Co-authored-by: Photogrammer <[email protected]>
Co-authored-by: Photogrammer <[email protected]>
  • Loading branch information
3 people authored Aug 13, 2024
1 parent f9ad4de commit 32f68a2
Show file tree
Hide file tree
Showing 12 changed files with 186,425 additions and 3 deletions.
30 changes: 30 additions & 0 deletions data/mysql-files/add_constraints.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- bus_location 테이블의 외래 키 추가
ALTER TABLE talkka_db.bus_location
ADD CONSTRAINT FKohe5hrtdo44wqkqfc96kpb30m
FOREIGN KEY (route_id) REFERENCES talkka_db.bus_route (route_id);

-- bus_route_station 테이블의 외래 키 추가
ALTER TABLE talkka_db.bus_route_station
ADD CONSTRAINT FKf66j4cjj3igamxvlxvbvm3shp
FOREIGN KEY (route_id) REFERENCES talkka_db.bus_route (route_id),
ADD CONSTRAINT FKh3jd7m358dvb09j9qx8xxtn2m
FOREIGN KEY (station_id) REFERENCES talkka_db.bus_station (station_id);

-- subway_confusion 테이블의 외래 키 추가
ALTER TABLE talkka_db.subway_confusion
ADD CONSTRAINT FK8m94gtsyhkedmu7vbk0ky3i8l
FOREIGN KEY (station_id) REFERENCES talkka_db.subway_station (station_id);

-- subway_timetable 테이블의 외래 키 추가
ALTER TABLE talkka_db.subway_timetable
ADD CONSTRAINT FK3tuvka1c7yr4535ac60blmamh
FOREIGN KEY (station_id) REFERENCES talkka_db.subway_station (station_id);

-- bus_review 테이블의 외래 키 추가
ALTER TABLE talkka_db.bus_review
ADD CONSTRAINT FK23ju1igohysqlytq7p8wa6tax
FOREIGN KEY (bus_route_station_id) REFERENCES talkka_db.bus_route_station (bus_route_station_id),
ADD CONSTRAINT FKmh0qi58cvoq8ivcrmstjoauw5
FOREIGN KEY (route_id) REFERENCES talkka_db.bus_route (route_id),
ADD CONSTRAINT FKojkl7pwovnccu0txrbvxk97jd
FOREIGN KEY (user_id) REFERENCES talkka_db.users (user_id);
38 changes: 38 additions & 0 deletions data/mysql-files/bulk_insert_route_stations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- 1. 임시 테이블 생성
DROP TABLE IF EXISTS temp_bus_route_station;
CREATE TABLE temp_bus_route_station
(
route_id BIGINT,
station_id BIGINT,
station_seq SMALLINT,
station_name VARCHAR(100),
mobile_no VARCHAR(50), -- 필요에 따라 추가
region_name VARCHAR(100), -- 필요에 따라 추가
district_cd VARCHAR(10), -- 필요에 따라 추가
center_yn CHAR(1), -- 필요에 따라 추가
turn_yn CHAR(1), -- 필요에 따라 추가
x DECIMAL(11, 8), -- 필요에 따라 추가
y DECIMAL(11, 8), -- 필요에 따라 추가
created_at DATETIME DEFAULT CURRENT_TIMESTAMP -- 생성일자 추가
);

-- 2. CSV 데이터 로드 (열 순서를 명시적으로 지정)
LOAD DATA INFILE '/var/lib/mysql-files/bus_route_station.csv'
INTO TABLE temp_bus_route_station
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(route_id, station_id, station_seq, station_name, mobile_no, region_name, district_cd, center_yn, turn_yn, x, y);
-- 열 순서를 명시적으로 지정합니다.

-- 3. bus_route_station 테이블에 데이터 삽입
INSERT INTO bus_route_station (route_id, station_id, station_seq, station_name, created_at)
SELECT b.route_id, s.station_id AS station_id, t.station_seq, t.station_name, NOW()
FROM temp_bus_route_station t
JOIN bus_route b ON t.route_id = b.api_route_id
JOIN bus_station s ON t.station_id = s.api_station_id;
-- station_id를 매핑합니다.

-- 4. 임시 테이블 삭제
DROP TABLE temp_bus_route_station;
70 changes: 70 additions & 0 deletions data/mysql-files/bulk_insert_routes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
-- 1. 임시 테이블 생성
DROP TABLE IF EXISTS temp_bus_route;
CREATE TABLE temp_bus_route
(
route_id BIGINT,
route_name VARCHAR(100),
route_type_cd VARCHAR(10),
route_type_name VARCHAR(100),
start_station_id BIGINT,
start_station_name VARCHAR(100),
start_mobile_no VARCHAR(50),
end_station_id BIGINT,
end_station_name VARCHAR(100),
end_mobile_no VARCHAR(50),
region_name VARCHAR(100),
district_cd VARCHAR(10),
up_first_time VARCHAR(20),
up_last_time VARCHAR(20),
down_first_time VARCHAR(20),
down_last_time VARCHAR(20),
peek_alloc INT,
n_peek_alloc INT,
company_id BIGINT,
company_name VARCHAR(100),
company_tel VARCHAR(50)
);

-- 2. CSV 데이터 로드 (임시 테이블에)
LOAD DATA INFILE '/var/lib/mysql-files/bus_route.csv'
INTO TABLE temp_bus_route
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(route_id, route_name, route_type_cd, route_type_name, start_station_id, start_station_name, start_mobile_no,
end_station_id, end_station_name, end_mobile_no, region_name, district_cd, up_first_time, up_last_time,
down_first_time, down_last_time, peek_alloc, n_peek_alloc, company_id, company_name, company_tel);

-- 3. 중복 제거 후 최종 테이블에 삽입
INSERT INTO bus_route (api_route_id, route_name, route_type_cd, route_type_name, start_station_id, start_station_name,
start_mobile_no, end_station_id, end_station_name, end_mobile_no, region_name, district_cd,
up_first_time, up_last_time, down_first_time, down_last_time, peek_alloc, n_peek_alloc,
company_id, company_name, company_tel, created_at, updated_at)
SELECT DISTINCT route_id,
route_name,
route_type_cd,
route_type_name,
start_station_id,
start_station_name,
start_mobile_no,
end_station_id,
end_station_name,
end_mobile_no,
region_name,
district_cd,
up_first_time,
up_last_time,
down_first_time,
down_last_time,
peek_alloc,
n_peek_alloc,
company_id,
company_name,
company_tel,
NOW(),
NOW()
FROM temp_bus_route;

-- 4. 임시 테이블 삭제
DROP TABLE temp_bus_route;
40 changes: 40 additions & 0 deletions data/mysql-files/bulk_insert_stations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- 1. 임시 테이블 생성
DROP TABLE IF EXISTS temp_bus_station;
CREATE TABLE temp_bus_station
(
station_id BIGINT,
station_name VARCHAR(100),
mobile_no VARCHAR(50),
region_name VARCHAR(100),
district_cd VARCHAR(10),
center_yn CHAR(1),
turn_yn CHAR(1),
x DECIMAL(11, 8),
y DECIMAL(11, 8)
);

-- 2. CSV 데이터 로드 (임시 테이블에)
LOAD DATA INFILE '/var/lib/mysql-files/bus_station.csv'
INTO TABLE temp_bus_station
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(station_id, station_name, mobile_no, region_name, district_cd, center_yn, turn_yn, x, y);

-- 3. 중복 제거 후 최종 테이블에 삽입
INSERT INTO bus_station (api_station_id, station_name, region_name, district_cd, center_yn, turn_yn,
latitude, longitude, created_at)
SELECT DISTINCT station_id,
station_name,
region_name,
district_cd,
center_yn,
turn_yn,
y AS latitude,
x AS longitude,
NOW()
FROM temp_bus_station;

-- 4. 임시 테이블 삭제
DROP TABLE temp_bus_station;
Loading

0 comments on commit 32f68a2

Please sign in to comment.