[MongoDB] shell 내에서 커서를 이용하여 연속으로 출력시키는 방법.

var myCursor = db.inventory.find().addOption(DBQuery.Option.noTimeout);

var myCursor = db.inventory.find();

var myFirstDocument = myCursor.hasNext() ? myCursor.next() : null;

myCursor.objsLeftInBatch();




많고 많은 라이브러리 중 하나.
보통은 json-simple 을 사용하는 것으로 알고 있다.

Json 오브젝트에서 Key의 메타 데이터를 가져올 수 있는 라이브러리가 있는지 확인하는 과정에서 나온 라이브러리. 원하는 건 못찾았다.


          try {
               JSONObject jObject = new JSONObject(jString);

               // menu jsonobject 생성
               JSONObject responseObject = jObject.getJSONObject("response");

              
               JSONObject header = responseObject.getJSONObject("header");
               System.out.println(header.toString());
              
               String resultMsg = header.getString("resultMsg");
               System.out.println(resultMsg);
               String resultCode = header.getString("resultCode");
               System.out.println(resultCode);

              
               JSONObject body = jObject.getJSONObject("response").getJSONObject("body");

               JSONArray item = body.getJSONObject("items").getJSONArray("item");
              
               for(int i=0; i<item.length(); i++){
                    JSONObject item_o =item.getJSONObject(i);
                    System.out.println(item_o.toString());
                   
                    int rnum = item_o.getInt("rnum");
                    System.out.println(rnum);
                    int code = item_o.getInt("code");
                    System.out.println(code);
                    String name = item_o.getString("name");
                    System.out.println(name);
                   
                    /*String rnum = item.getJSONObject(i).getString("rnum");
                    System.out.println(rnum);
                    String code = item.getJSONObject(i).getString("code");
                    System.out.println(code);
                    String name = item.getJSONObject(i).getString("name");
                    System.out.println(name);*/
               }
               int numOfRows = body.getInt("numOfRows");
               System.out.println(numOfRows);
               int pageNo = body.getInt("pageNo");
               System.out.println(pageNo);
               int totalCount = body.getInt("totalCount");
               System.out.println(totalCount);
              
    

          } catch (JSONException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }


JNI 패키지 컴파일 예제



 본 예제는 JNI 를 사용하였을 때 패키지에서의 컴파일 방법을 설명한 예제이다.

참조 : http://lng1982.tistory.com/153 패키지 컴파일 방법.
참조 : http://buedt.tistory.com/50  패키지 안에서 JNI 활용하기 (일반 방법하고 다르다.)

  * 소스 작성 : ~/simple-test/HelloJni

com.HelloWorld.java   -> com 폴더 안에 HelloWorld.java  JNI 모듈이다.
package com;

public class HelloWorld {
public HelloWorld (){
System.loadLibrary("HelloWorld");
}
public native void printHelloWorld();
}
  
com.main.java   -> com 폴더 안에 main.java    JNI 모듈을 사용할 메인 함수이다.
package com;

public class main {
public static void main(String[] args) {
HelloWorld hw = new HelloWorld();
hw.printHelloWorld();
}
}

  
  

  JNI 모듈을 헤더파일로 변경

javac ./com/HelloWorld.java
javah -cp ./ com.HelloWorld



  * javah 로 작성된 com_HelloWorld.h

com_HelloWorld.h    -> 요거는 com 폴더 안에 있는 것이 아니라 컴파일 루트에 있다.
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_HelloWorld */

#ifndef _Included_com_HelloWorld
#define _Included_com_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_HelloWorld
* Method: printHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_HelloWorld_printHelloWorld
(JNIEnv *, jobject);


#ifdef __cplusplus
}
#endif
#endif

  * com_HelloWorld.h 에 맞춰서 작성한 .cpp 클래스

HelloWorld.cpp  -> vi HelloWorld.cpp 로 com 폴더 밖에서 작성.
#include <stdio.h>
#include "com_HelloWorld.h"

JNIEXPORT void JNICALL Java_com_HelloWorld_printHelloWorld
(JNIEnv *env, jobject obj){
printf("hi World \n");
return;
}

  동적 라이브러리 생성
gcc -fPIC -g -c -Wall -I /usr/lib/jvm/java-7-openjdk-amd64/include HelloWorld.cpp
gcc -shared -o libHelloWorld.so.1.0.1 HelloWorld.o -lc
ln -s libHelloWorld.so.1.0.1 libHelloWorld.so

  자바 컴파일 및 실행
javac -d . com/*.java
java -cp . com.main


  실행 모습









* 메모리 누수 관련 내용 추가


.cpp 파일에서 jstring param 의 데이터를 쓰고 난 뒤에는

반드시 Release를 해주어야 한다.

이는 jstring 객체 뿐만 아니라 다른  jobject 들도 마찬가지이다.

안 그러면 메모리 누수가 발생한다.



아래는 올바른 예제. 붉은색 글씨가 Get 하고 Release 하는 부분이다.


#include <jni.h>

#include "MessageType.h"

#include <stdio.h>

JNIEXPORT jstring JNICALL Java_MessageType_printMessage(JNIEnv* env, jobject obj, jstring msg) {

   char buf[128];

   const char *str = (*env)->GetStringUTFChars(env, msg, 0);

   printf("%s", str);

   (*env)->ReleaseStringUTFChars(env, msg, str);

   scanf("%s", buf);

   return (*env)->NewStringUTF(env, buf);

} 



출처 : https://rerethink.tistory.com/entry/jni%EB%AC%B8%EC%9E%90%EC%97%B4-%EC%A0%84%EB%8B%AC-%EC%9D%B8%EC%9E%90



'프로그래밍 > JAVA' 카테고리의 다른 글

JDK 버전 충돌이 날 경우 해결 방법  (1) 2016.02.12
Json Parser 라이브러리  (0) 2016.02.12

+ Recent posts