메뉴 건너뛰기

Cloudera, BigData, Semantic IoT, Hadoop, NoSQL

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


* _uri및 ct에 like검색을 수행하여 리턴되는 결과값 중에서 con의 값을 string->interger로 casting한후 원래의 _id를 key로 하여

update하는 java소스 코드이다.(주의할점은 아래의 예제는 makeStringMap를 이용하여 모든 값을 String으로 변경되므로 숫자등의 속성이 유지 되어야 하는 값은 적절하게 변환하여 주어야 한다)


// MongoDB연결
		try {
			mongoClient = new MongoClient(new ServerAddress(db_server, Integer.parseInt(db_port)));
			db = mongoClient.getDB(db_name);
			table = db.getCollection(collection_name);
		} catch (Exception ex) {
			log.debug("MongoDB connection error : "+ex.getMessage());
			if(db != null) {
				db.cleanCursors(true);
				db = null;				
			}
			if(table != null) {table = null;}
			if(mongoClient != null ) {
				mongoClient.close();
			}
			throw ex;
		} 

// con값에 대한 형변환(String -> Integer)
		// 형변환(shell 코드)
	/*		db.resource.find (
				    {"_uri": /TicketCount/status/CONTENT_INST/, "ct": /20161213/}
				)
				    .forEach(function(x) {
				    x.con = new NumberInt(x.con);  
				      db.resource.save(x)  })
	*/		
	   DBObject searchCastQuery = new BasicDBObject();  //"$match", new BasicDBObject("ct", new BasicDBObject("$gte", "20161213T160000")));
	   searchCastQuery.put("_uri", new BasicDBObject("$regex", "TicketCount/status/CONTENT_INST"));
	   //searchCastQuery.put("ct", new BasicDBObject("$regex", "20161213"));
	   searchCastQuery.put("ct", new BasicDBObject("$regex", Utils.sysdateFormat.format(new Date())));
		
		DBCursor cursor = table.find(searchCastQuery);
		while (cursor.hasNext()) {
			DBObject oldObj = cursor.next();
			
			@SuppressWarnings("unchecked")
			Map<String, String> map = makeStringMap(oldObj.toMap());
			//map.put("_id", new ObjectId(map.get("_id")));
			
			ObjectId id = new ObjectId(map.get("_id"));
			BasicDBObject newObj = new BasicDBObject(map);
			newObj.append("_id", id);
                        newObj.append("con", Integer.parseInt(map.get("con")));
			newObj.append("ty", Integer.parseInt(map.get("ty")));
			newObj.append("st", Integer.parseInt(map.get("st")));
			newObj.append("cs", Integer.parseInt(map.get("cs")));
			
			String lbl_tmp = map.get("lbl");
			Gson gson = new Gson();
			String[] lbl_json = gson.fromJson(lbl_tmp ,String[].class);
			
			newObj.append("lbl", lbl_json);

			BasicDBObject updateObj = new BasicDBObject();
			updateObj.put("$set", newObj);

			table.update(oldObj, updateObj);
		}


makeStringMap함수

public Map<String,String> makeStringMap(Map<String, String> map) {
		Map<String, String> newMap = new HashMap<String, String>();
		
    	Set<String> entry = map.keySet();
    	Iterator<String> itr = entry.iterator();
    	
    	while(itr.hasNext()) {
    		String key = String.valueOf(itr.next());
    		//System.out.println("key : "+key);
    		String value = String.valueOf(map.get(key));
    		//System.out.println("value : "+value);
    		
    		newMap.put(key, value);
    	}
    	
	    return newMap;
	}



번호 제목 날짜 조회 수
421 Cannot create /var/run/oozie/oozie.pid: Directory nonexistent오류 2014.06.03 537
420 여러 홈페이지를 운영하거나 혹은 서버에 가입한 사용자들에게 홈페이지 계정을 나누어 줄수 있도록 설정/계정 생성방법 2018.01.23 536
419 github에 있는 프로젝트와 로컬에서 작업한 프로젝트 합치기 2016.11.22 536
418 halyard의 console스크립트에서 생성한 repository는 RDF4J Web Applications에서 공유가 되지 않는다. 2017.07.05 533
417 new Gson().toJson(new ObjectId())을 사용하면 값이 다르게 나오는 경우가 있음 2016.12.23 531
416 python실행시 ValueError: zero length field name in format오류 해결방법 2016.05.27 528
415 Could not configure server becase SASL configuration did not allow the Zookeeper server to authenticate itself properly: javax.security.auth.login.LoginException: Checksum failed 2019.05.18 525
414 hadoop 어플리케이션을 사용하는 사용자 변경시 바꿔줘야 하는 부분 2016.09.23 524
413 Error: E0501 : E0501: Could not perform authorization operation, User: hadoop is not allowed to impersonate hadoop 해결하는 방법 2015.06.07 521
412 AIX 7.1에서 hive실행시 "hive: line 86: readlink: command not found" 오류가 발생시 임시 조치사항 2016.09.25 520
411 Caused by: java.lang.ClassNotFoundException: org.apache.spark.Logging 발생시 조치사항 2017.04.19 519
410 [HIVESERVER2]프로세스의 thread및 stack trace를 덤프하는 방법(pstack, jstack) 2022.05.11 518
409 우분투 서버에 GUI로 접속하기 file 2018.05.27 518
408 fuseki에서 제공하는 script중 s-post를 사용하는 예문 2017.09.15 517
407 streaming작업시 입력된 값에 대한 사본을 만들게 되는데 이것이 실패했을때 발생하는 경고메세지 2017.04.03 517
406 서버중 slave,worker,regionserver만 재기동해야 할때 필요한 기동스크립트및 사용방법 2017.02.03 516
405 Soft memory limit exceeded (at 100.05% of capacity) 오류 조치 2022.01.17 515
404 우분투 16.04LTS에 Jupyter설치 2018.04.17 515
403 Windows7 64bit 환경에서 Apache Hadoop 2.7.1설치하기 2017.07.26 513
402 mongodb에서 큰데이타 sort시 오류발생에 대한 해결방법 2015.12.22 513
위로