Created
December 28, 2022 13:47
-
-
Save shininghyunho/05c810fabcccb9ce6f5ebdeda50fa16a to your computer and use it in GitHub Desktop.
실거래가 저장
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson | |
import org.apache.http.client.config.RequestConfig | |
import org.apache.http.client.methods.HttpPost | |
import org.apache.http.entity.StringEntity | |
import org.apache.http.impl.client.HttpClients | |
import java.io.BufferedReader | |
import java.io.File | |
import java.util.LinkedList | |
// apache http client | |
val httpClient = HttpClients.createDefault() | |
val baseURL="http://ec2-3-37-157-108.ap-northeast-2.compute.amazonaws.com:8080/api/v1" | |
//val baseURL="http://localhost:8080/api/v1" | |
val gson= Gson() | |
data class House_CSV( | |
val 행정코드: Long, | |
val 지번_kakao: String, | |
val 도로명_kakao: String, | |
val 단지명: String, | |
val 단지명_kakao: String, | |
val 거래금액: Long, | |
val 건축년도: String, | |
val 전용면적: Double, | |
val 층: Int, | |
val 거래일자: String, | |
val 위도: Double, | |
val 경도: Double, | |
val 우편번호: Int | |
) | |
data class House_DB( | |
val hangCode: Long, | |
val jibunAddress: String, | |
val roadAddress: String, | |
val danjiName: String, | |
val cost: Long, | |
val latitude: Double, | |
val longitude: Double, | |
val postCode: Int | |
) | |
fun pushToQue(line: String) : LinkedList<String> { | |
val que=LinkedList<String>() | |
val sb=StringBuilder() | |
var isDoubleQuote=false | |
for(c:Char in line){ | |
if(c=='"'){ | |
isDoubleQuote=!isDoubleQuote | |
continue | |
} | |
else if(c==','){ | |
if(!isDoubleQuote){ | |
que.add(sb.toString()) | |
sb.clear() | |
} | |
continue | |
} | |
sb.append(c) | |
} | |
que.add(sb.toString()) | |
return que | |
} | |
fun removeComma(line: String) : String { | |
val sb=StringBuilder() | |
for(c:Char in line){ | |
if(c==','){ | |
continue | |
} | |
sb.append(c) | |
} | |
return sb.toString() | |
} | |
fun saveToDB(houseDb: House_DB){ | |
val url="$baseURL/houses" | |
val json=gson.toJson(houseDb) | |
//println(json) | |
val post=HttpPost(url) | |
post.setHeader("Content-type", "application/json; charset=utf-8") | |
post.entity=StringEntity(json, "UTF-8") | |
// set timeout to 10 seconds | |
val TEN_SEC=10*1000 | |
val config=RequestConfig.custom() | |
.setConnectTimeout(TEN_SEC) | |
.setConnectionRequestTimeout(TEN_SEC) | |
.setSocketTimeout(TEN_SEC) | |
.build() | |
post.config=config | |
try{ | |
httpClient.execute(post) | |
} catch (e: Exception){ | |
} | |
post.releaseConnection() | |
//println("saved to db") | |
} | |
fun removeSpace(line: String) : String { | |
val sb=StringBuilder() | |
for(c:Char in line){ | |
if(c==' ') continue | |
sb.append(c) | |
} | |
return sb.toString() | |
} | |
fun main(args: Array<String>) { | |
// read csv file | |
val csvFile = File("./data.csv") | |
val br = BufferedReader(csvFile.reader()) | |
val header=br.readLine() | |
val columnNum=header.split(",").size | |
var i=0 | |
val error_set=HashSet<Int>() | |
// save to db | |
while(br.ready()){ | |
i++ | |
if(i%3000==0) println(i) | |
val line=br.readLine() | |
val que = pushToQue(line) | |
if(que.size!=columnNum){ | |
error_set.add(i) | |
continue | |
} | |
var house: House_CSV | |
try{ | |
house=House_CSV( | |
행정코드=que.poll().toLong(), | |
지번_kakao=que.poll(), | |
도로명_kakao=que.poll(), | |
단지명=que.poll(), | |
단지명_kakao=que.poll(), | |
거래금액=removeSpace(removeComma(que.poll())).toLong(), | |
건축년도=que.poll(), | |
전용면적=que.poll().toDouble(), | |
층=que.poll().toInt(), | |
거래일자=que.poll(), | |
위도=que.poll().toDouble(), | |
경도=que.poll().toDouble(), | |
우편번호=que.poll().toInt() | |
) | |
} catch (e: Exception){ | |
error_set.add(i) | |
continue | |
} | |
// save to db | |
val houseDb=House_DB( | |
hangCode=house.행정코드, | |
jibunAddress=house.지번_kakao, | |
roadAddress=house.도로명_kakao, | |
danjiName=house.단지명, | |
cost=house.거래금액, | |
latitude=house.위도, | |
longitude=house.경도, | |
postCode=house.우편번호 | |
) | |
//println(houseDb) | |
saveToDB(houseDb) | |
} | |
println("error_cnt: ${error_set.size}") | |
println("total: $i") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment