///////
Search

이아진

Java로 대용량 데이터 분석하기 프로젝트

<PopulationStatistics>

파일의 한글자씩 읽어오기
// 한글자씩 public void readByChar(String filename) throws IOException { // 파일 위치 FileReader fileReader = new FileReader(filename); // 파일 읽지 않는다. String fileContents =""; while(fileContents.length() < 1_000_000) { // read 사용했을 때 읽음 char c = (char)fileReader.read(); // 한글자만 읽어오는 것 fileContents += c; // 1바이트씩 쌓기위해 } System.out.println("filename" + fileContents); }
Java
복사
한글자씩 읽는 기능(이전 시간의 문자열 알파벳 한 글자씩 읽어오기와 동일)
파일을 한줄씩 읽어오기
public List<PopulationMove> readByLine(String filename) throws IOException { //읽어온 데이터를 List에 담아 줄 것 List<PopulationMove> pml = new ArrayList<>(); BufferedReader reader = new BufferedReader( new FileReader(filename) // 파일name에 해당하는 file의 경로로 ); String str =""; while((str = reader.readLine()) != null) { System.out.println(str); PopulationMove pm = parse(str); // 한 줄씩 들어오는 str parsing pml.add(pm); } reader.close(); return pml; }
Java
복사
읽어온 파일 콘솔에 찍어주기
public void readByLine2(String filename) throws IOException { try(BufferedReader br = Files.newBufferedReader( Paths.get(filename), StandardCharsets.UTF_8)) { String line; while((line = br.readLine()) != null) { // 읽어올 파일 없을때까지 System.out.println(line); } } catch (IOException e) { throw new RuntimeException(e); } }
Java
복사
data를 ,로 분리 후 cityArr배열에 담아서 배열의 Index 0번과 6번만 return
public PopulationMove parse(String data) { String[] cityArr = data.split(","); int inCity = Integer.parseInt(cityArr[0]); int toCity = Integer.parseInt(cityArr[6]); // 생성자 오버로딩 return new PopulationMove(inCity,toCity); }
Java
복사
대용량 파일은 메모리에 무리가 가기 때문에 파일에서 필요한 부분만 뽑아서 저장
새로운 File 생성하기
public void createFile(String filename){ File file = new File(filename); try{ System.out.println("파일 생성성"); file.createNewFile(); // 이 함수까지 실행해줘야 파일이 생긴다 }catch (IOException e){ System.out.println("파일 생성 못함"); throw new RuntimeException(); } }
Java
복사
file.createNewFile() 함수 생성 꼭 해주기
List<String>을 받아서 filename에 쓴다.
public void write(List<String> strs, String filename) throws IOException{ // 1.파일 객체 생성 File file = new File(filename); // 2. 파일 존재여부 체크 및 생성 if(!file.exists()){ file.createNewFile(); } // 3. Buffer를 사용해서 File에 write할 수 있는 BufferedWrite 생성 FileWriter fw = new FileWriter(file); BufferedWriter writer = new BufferedWriter(fw); // 4. 파일에 쓰기 for (String str : strs) { writer.write(str); } // 5. BufferedWrite close writer.close(); }
Java
복사
// PopulationMove -> "11,11" // from, to public String fromToString(PopulationMove populationMove){ return populationMove.getFromSido() + "," + populationMove.getToSido() + "\n"; } public Map<String, Integer> getMoveCntMap(List<PopulationMove> pml){ Map<String, Integer> moveCntMap = new HashMap<>(); for (PopulationMove pm : pml) { String key = pm.getFromSido() + "," + pm.getToSido(); if(moveCntMap.get(key) == null){ moveCntMap.put(key, 1); } moveCntMap.put(key, moveCntMap.get(key) + 1); } return moveCntMap; }
Java
복사
public static void main(String[] args) throws IOException { String address = "D:\\Users\\LEEAJIN\\Desktop\\2021_인구관련연간자료_20220927_66125.csv"; PopulationStatistics ps = new PopulationStatistics(); List<PopulationMove> pml = ps.readByLine(address); // populationStatistics.readByChar(address); // populationStatistics.readByLine(address); // List<String> strings = new ArrayList<>(); // // for (PopulationMove pm : pml) { // System.out.printf("전입:%s, 전출:%s\n", pm.getFromSido(), pm.getToSido()); Map<String ,Integer> map = ps.getMoveCntMap(pml); Map<String, Integer> heatMapIdxMap = ps.heatmapIdxMap(); String targetFilename = "for_heatmap.txt"; ps.createFile(targetFilename); List<String> cntResult = new ArrayList<>(); for (String key :map.keySet() ){ String[] fromto = key.split(","); // 매핑을 해서 저장 String s = String.format("{%s, %s, %d},\n", heatMapIdxMap.get(fromto0,heatMapIdxMap)); cntResult.add(s); } ps.write(cntResult, targetFilename); }
Java
복사

PopulationMove 클래스

// constructor추가 fromSido, toSido를 받아서 멤버변수에 넣는 기능 public class PopulationMove { private int fromSido; private int toSido; private String fromSidoKorean; private String toSidoKorean; private Map<Integer, String> sidoMap = new HashMap<>(); // data(String)으로 넘어온 데이터를 parsing 생성자 public PopulationMove(int fromSido, int toSido) { this.fromSido = fromSido; this.toSido = toSido; this.setSidoMap(); this.fromSidoKorean = this.sidoMap.get(fromSido); this.toSidoKorean = this.sidoMap.get(toSido); } public int getFromSido() { return fromSido; } public int getToSido() { return toSido; } public void setSidoMap() { /* 11 : 서울, 21 : 부산, 22 : 대구 * 23 : 인천, 24: 광주, 25: 대전, 26: 울산 * 29 : 세종, 31: 경기도 32: 강원도, 33: 충청북도 * 34 : 충청남도, 35: 전라북도, 36 :전라남도 * 37 : 경상북도, 38: 경상남도, 39:제주도 * */ sidoMap.put(11, "서울"); sidoMap.put(26, "부산"); sidoMap.put(27, "대구"); sidoMap.put(28, "인천"); sidoMap.put(29, "광주"); sidoMap.put(30, "대전"); sidoMap.put(31, "울산"); sidoMap.put(36, "세종"); sidoMap.put(41, "경기도"); sidoMap.put(42, "강원도"); sidoMap.put(43, "충청북도"); sidoMap.put(44, "충청남도"); sidoMap.put(45, "전라북도"); sidoMap.put(46, "전라남도"); sidoMap.put(47, "경상북도"); sidoMap.put(48, "경상남도"); sidoMap.put(50, "제주도"); } public String getFromSidoKorean() { return fromSidoKorean; } public String getToSidoKorean() { return toSidoKorean; } }
Java
복사