걸어서 개발 속으로

API json parsing

손건호 2023. 2. 27. 01:22

https://github.com/geonho1943/LFG

 

GitHub - geonho1943/LFG: "Looking For Group" service

"Looking For Group" service. Contribute to geonho1943/LFG development by creating an account on GitHub.

github.com

LFG서비스는 스팀 게임 플렛폼의 게임을 같이 할사람을 구하는 서비스다

사람들 구하는 글을 쓸때 같이할 게임의 이미지를 포함 한다면

효과적으로 참여율을 높힐수 있을것이라 예상하여

스팀 api를 통해 스팀의 게임 정보를 가지고와서

서비스에서 모집글 정보와 이미지를 같이 보일수 있도록 했다

 

 

구현

 

스팀API  url을 통해 appID, name 을 DB 에 저장

https://api.steampowered.com/ISteamApps/GetAppList/v2/

 

LFG웹 에서 스팀 게임 타이틀 검색시 appID 를 통해 게임의 정보,이미지를 출력

 

LFG웹 에서 모집글 작성시, 선택한 게임의 베너이미지를 게시글에 같이 출력

http://cdn.akamai.steamstatic.com/steam/apps/{appid}/header.jpg

 

 

 

 

 

스팀API를 사용할때 스팀의 의도를 벗어나고 싶지않아서 공식문서를 통해 주의사항을 확인했다

https://partner.steamgames.com/doc/webapi_overview

 

Web API Overview (Steamworks Documentation)

Documentation Resources News & Updates Support

partner.steamgames.com

 

 

내가 크게 신경쓸부분은

api 요청 10만회 제한으로 사용한다면 크게 지장 없을것이라 판단했고

web api key를 발급 받았지만 public api만 사용했다

public api 만 사용한 이유는

혹시 모를 조건을 어겼을시 스팀에 주는 피해를 미연에 방지하고자 했다

https://steamcommunity.com/dev/apiterms

 

Steam Community :: Steam Web API Terms of Use

1. Steam Web APIs. The Steam Web APIs are Valve's service allowing licensees to retrieve certain data regarding Steam and games available via Steam ("Steam Data"). The Steam Web APIs consist of multiple application program interfaces for retrieving various

steamcommunity.com

 

구현

 

크게 파싱과 저장 으로 나눴다

 

 

https://api.steampowered.com/ISteamApps/GetAppList/v2/

 

 

appid, name 한쌍의 app1 을

apps라는 리스트에 담아 save() 메서드에 넘겨주는 방식으로

파싱을 마무리 했다

 

 

 

 

가공한 데이터를 내 데이터베이스에 저장하는save()는

 이전 파싱구현부에서 넘겨받은 apps리스트 파라미터를

INSERT 쿼리로 하나씩 저장 하는 구조로 구현 했다

 

JDBC 에서 지원 하는 메서드 addBatch() 를 새로 알게되었다

다수의 쿼리를 일괄처리 할때 쿼리를 추가해주는 메서드다

 

addBatch() 오라클 공식문서

https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#addBatch-java.lang.String-

 

Statement (Java Platform SE 8 )

Specifies that this Statement will be closed when all its dependent result sets are closed. If execution of the Statement does not produce any result sets, this method has no effect. Note: Multiple calls to closeOnCompletion do not toggle the effect on thi

docs.oracle.com

Adds the given SQL command to the current list of commands for this Statement object. The commands in this list can be executed as a batch by calling the method executeBatch.

문서에는 

이 문 개체에 대한 현재 명령 목록에 지정된 SQL 명령을 추가합니다. 이 목록의 명령은 executeBatch 메서드를 호출하여 일괄 실행할 수 있습니다.

라고 설명 되어있다

 

 

insert쿼리 일괄처리를 시도할때 문제가 있었다

lfg_app_list테이블 에는 일부의 데이터만 저장되어 있었고

이 원인은

 

  • 일시적인 네트워크 오류
  • 무료 클라우드 서비스의 제한된 네트워크 환경
  • 무료 클라우드 서비스의 제한된 컴퓨터 리소스(메모리 부족)
  • 데이터베이스의 커넥션 유지 시간을 넘어선 쿼리 요청
  • excuteBatch() 메서드의 최대 쿼리요청수 제한

제한된 자원을 늘리는것이 아니라

주워진 자원으로 이 문제를 해결하려했고

excuteBatch() 메서드의 요청 한도 같은경우는

공식문서에서 명시되어있지않았다

 

그외에 다양한 문제가 있을수 있기때문에

addBatch() 로 분활하여 1만개씩 요청하는방법으로 구현했다

1만개를 카운트후 분활 요청한다

1만 * N개 의 쿼리가 요청되고 나면

마지막의 1만개 미만의 남은쿼리를 요청하는방식으로

유실되는 데이터가 없도록 구현 했다

 

 

분활요청 방식으로 구현 했을경우

 

  • 스팀API에서 받아오는데이터 수의 변화에 대한 걱정이 없다

2만개의 게임이 추가 되어도

excuteBatch()가 2회 더 실행되기때문이다

 

 

  • 데이터베이스의 설정을 수정할 필요가 없다

각각 excuteBatch() 메서드의 처리시간이 비슷하기때문에

데이터비이스의 커넥션 지연시간을 늘이지않아도 된다

 

 

  • 같은 이유로 클라우드컴퓨터의 자원을 확장하지않아도 처리할수 있는 구조이여

확인하지못한excuteBatch()의 최대 요청 한도의 유무

경우도 더이상 문제삼지않아도 된다

 

 

 

 

이번에 JDBC 드라이버라는 라이브러리를 사용했다

데이터베이스에 데이터를 전달할때 생길수 있는

다양한 상황에 대응할때 문제가 있었지만

JDBC가 지원하는 메서드로 충분히 구현할수 있었다

 

JDBC의 기능에 익숙해진다면

지금의 성능보다 더 개선할수 있을거라고 생각한다

 

 

 

 

 

 

jackson의 json node,

RestTemplate

ResponseEntity

예외처리의 printStackTrace

생소한 메서드, 처리방식, 자료형 등등이 있어서

이번에 사용한 해당 소스의 내용에 나오는 것들을

다시 정리해봤다

 

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

 

RestTemplate (Spring Framework 6.0.5 API)

postForLocation Create a new resource by POSTing the given object to the URI template, and returns the value of the Location header. This header typically indicates where the new resource is stored. URI Template variables are expanded using the given URI v

docs.spring.io

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

 

ResponseEntity (Spring Framework 6.0.5 API)

Create a ResponseEntity with a body, headers, and a raw status code.

docs.spring.io

 

 

jackson의 공식문서는 없고 왜 github링크만 나오는지...

https://github.com/FasterXML/jackson

 

GitHub - FasterXML/jackson: Main Portal page for the Jackson project

Main Portal page for the Jackson project. Contribute to FasterXML/jackson development by creating an account on GitHub.

github.com

 

예외처리

 

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Throwable.html

 

Throwable (Java SE 17 & JDK 17)

All Implemented Interfaces: Serializable Direct Known Subclasses: Error, Exception The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown

docs.oracle.com

 

+ 추가

공식문서가 github page인 이유는

jackson은 Java에서 사용하는 json을 오브젝트로 변환할수있는 라이브러리로

github를 사용한 오픈소스 이기때문이다

https://github.com/FasterXML/jackson-docs

 

GitHub - FasterXML/jackson-docs: Documentation for the Jackson JSON processor.

Documentation for the Jackson JSON processor. Contribute to FasterXML/jackson-docs development by creating an account on GitHub.

github.com