메뉴 건너뛰기

Cloudera, BigData, Semantic IoT, Hadoop, NoSQL

Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.


Mariadb에 접근하여 쿼리를 수행하고 필요시 정렬하여 List<Map<String, String>>에 담아서 리턴하는 메세드 예제


private final List<Map<String, String>> getResult (String query, String[] idxVals) throws Exception {
		String db_server = ;
		String db_port = ;
		String db_name = ;
		String db_user = ;
		String db_pass = ;

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		
		try {
			Class.forName("org.mariadb.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mariadb://" + db_server + ":" + db_port + "/" + db_name, db_user,  db_pass);
			
			pstmt = conn.prepareStatement(query);
			rs = pstmt.executeQuery();

			int cnt = 0;
	
			ResultSetMetaData md = rs.getMetaData();
			int columns = md.getColumnCount();
			while (rs.next()){
			   HashMap<String,String> row = new HashMap<String, String>(columns);
			   for(int i=1; i<=columns; i++){           
				   row.put(md.getColumnName(i), rs.getObject(i).toString());
			   }
			   //log.debug("row("+(cnt++)+")  ===========>"+row.toString());
			    list.add(row);
			}
			
			// 정렬(test)
	        // Collections.sort(list, new MapStringComparator("rest_type"));
	        //Collections.sort(list, new MapStringComparator("corner_id"));
	        //Collections.sort(list, new MapFloatComparator("cnt"));
	        
			return list;
		} catch (Exception e) {
			throw e;
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException sqle) {
				}
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException sqle) {
				}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException sqle) {
				}
			}
		}
	}


*정렬을 위해서 사용되는 클래스(문자열)

class MapStringComparator implements Comparator<Map<String, String>> {
		private final String key;
		
		public MapStringComparator(String key) {
			this.key = key;
		}
		
		@Override
		public int compare(Map<String, String> first, Map<String, String> second) {
			String firstValue =first.get(key);
	        String secondValue = second.get(key);
	        
	         // 내림차순 정렬
             return firstValue.compareTo(secondValue);
		}
	}


*정렬을 위해서 사용되는 클래스(숫자)

class MapFloatComparator implements Comparator<Map<String, String>> {
		private final String key;
		
		public MapFloatComparator(String key) {
			this.key = key;
		}
		
		@Override
		public int compare(Map<String, String> first, Map<String, String> second) {
			float firstValue = Float.valueOf(first.get(key));
	         float secondValue = Float.valueOf(second.get(key));
	         
			// 내림차순 정렬
	         if (firstValue > secondValue) {
	             return -1;
	         } else if (firstValue < secondValue) {
	             return 1;
	         } else /* if (firstValue == secondValue) */ {
	             return 0;
	         }
		}
	}


번호 제목 날짜 조회 수
350 [kudu]테이블 drop이 안되고 timeout이 걸리는 경우 조치 방법 2020.06.08 4124
349 Class.forName을 이용한 메서드 호출 샘플소스 2016.12.21 4125
348 원격의 origin/master를 기준으로 dev branch를 만들어 작업후 원격의 origin/dev에 push하는 방법 file 2016.11.22 4127
347 MapReduce2.0(YARN)기반의 CDH5 설치시 생성되는 사용자및 권한 부여 2018.05.30 4128
346 source, sink를 직접 구현하여 사용하는 예시 2019.05.30 4129
345 [Sentry]HDFS의 ACL을 Sentry와 연동후 테스트 2020.06.02 4131
344 cloudera에서 spark-shell를 실행했을때 default master는 spark.master=yarn-client임 2018.06.20 4137
343 "You are running Cloudera Manager in non-production mode.." warning메세지가 나타나지 않게 조치하는 방법 2018.05.23 4139
342 oozie 4.1 설치 - maven을 이용한 source compile on hadoop 2.5.2 with postgresql 9.3 2015.04.30 4144
341 spark 2.3.0을 설치하가 위해서 parcel에 다음 url을 입력한다. 2018.07.15 4146
340 ServerInfo객체파일 2016.07.21 4149
339 자주쓰는 유용한 프로그램 2018.03.16 4149
338 SPIN(SPARQL Inference Notation)이란.. file 2016.02.25 4156
337 ubuntu에 maven 3.6.1설치 및 환경변수 설정 2019.06.02 4156
336 Ubuntu 16.04 LTS에 4대에 Hadoop 2.8.0설치 2017.05.01 4158
335 java.lang.OutOfMemoryError: unable to create new native thread오류 발생지 조치사항 2016.10.17 4160
334 hive metastore db중 TBLS, TABLE_PARAMS테이블 설명 2021.10.22 4162
333 centos 5.X에 hadoop 2.0.5 alpha 설치 2013.12.16 4171
332 vuestorefrontui.io를 이용한 front end project 생성하기 2022.02.06 4176
331 DB별 JDBC 드라이버 2015.10.02 4177
위로