메뉴 건너뛰기

Bigdata, Semantic IoT, Hadoop, NoSQL

Bigdata, Hadoop ecosystem, Semantic IoT등의 프로젝트를 진행중에 습득한 내용을 정리하는 곳입니다.
필요한 분을 위해서 공개하고 있습니다. 문의사항은 gooper@gooper.com로 메일을 보내주세요.


1. 아래 소스를 참고하여 아래와 같은 폴더에 파일을 위치시킨다.

(maven으로 빌드하는 경우의 source파일및 resource파일 구조)

가. TestMain.java에서 sch.hist.insert를 호출하는 형태를 갖는다.

나. Mybatis의 트랜잭션은 Sch2Service.java의 SqlSession sqlSession2 = factory2.openSession(); 부터 시작되며

SqlSession은 반드시 close해줘야한다.

다. rollback()은 따로 해주지 않아도 되는가 보다(?)



-----소스폴더 구조----

src/main/java/TestMain.java


src/main/resource/Configuration.xml

src/main/resource/mariadb/db.properties

src/main/resource/mariadb/mapper/sch/SchHist_SQL.xml



-------Sch_SQL.xml의 일부분

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="sch.hist">

<insert id="insert" parameterType="com.gooper.icbms.sda.comm.sch.dto.SchHistDTO">

<![CDATA[

insert into thsda_sch_hist(task_group_id, task_id, start_time, task_class, task_expression, work_cnt, work_time, cuser)

values (#{task_group_id}, #{task_id}, #{start_time}, #{task_class}, #{task_expression}, #{work_cnt}, #{work_time}, #{cuser})

]]>

</insert>



<select id="updateFinishTime" parameterType="com.gooper.icbms.sda.comm.sch.dto.SchHistDTO">

<![CDATA[

update thsda_sch_hist set finish_time=#{finish_time}, work_result=#{work_result}, triple_file_name=#{triple_file_name}, triple_check_result=#{triple_check_result},

uuser  = #{uuser}, udate = now()

where task_group_id = #{task_group_id} and task_id = #{task_id} and start_time = #{start_time}

]]>

</select>

</mapper>



------------TestMain.java(일부분임)

//test start
int id = idx.getAndIncrement();
log.info("direct calling from mybatis without spring test start(id : "+id+")");
String task_group_id = "PARKKKKKK_task_group_id_+"+id;
String task_id = "PARKKKKK_task_id_"+id;
int work_cnt = 10;
String start_time = Utils.dateFormat.format(new Date());
String end_time = Utils.dateFormat.format(new Date());
schComm.insertSchHist(task_group_id, task_id, work_cnt, start_time, end_time);
log.info("direct calling from mybatis without spring test end(id: "+id+")");
//test end


-------SchComm.java의 insertSchHist()부분 

// schHist테이블에 데이타 insert

public int insertSchHist(String task_group_id, String task_id, int work_cnt, String start_time, String end_time)

throws Exception {

int updateCnt = 0;

SchHistDTO schHistDTO = new SchHistDTO();

schHistDTO.setTask_group_id(task_group_id);

schHistDTO.setTask_id(task_id);

schHistDTO.setStart_time(start_time);


schHistDTO.setTask_class("NONE");

schHistDTO.setTask_expression("NONE");


schHistDTO.setWork_cnt(work_cnt);

schHistDTO.setWork_time(end_time);

schHistDTO.setCuser(user_id);

schHistDTO.setUuser(user_id);


List<SchHistDTO> list = new ArrayList<SchHistDTO>(); 

Map<String, List<SchHistDTO>> updateSchHistMap = new HashMap<String, List<SchHistDTO>>();

list.add(schHistDTO);

updateSchHistMap.put("list", list);

try {

updateCnt = sch2Service.insertSchHist(updateSchHistMap);

// pass

} catch (Exception e) {

e.printStackTrace();

throw e;

}

return updateCnt;

}



------------Sch2Service.java의 insertSchHist() 부분

         // 아래문장은 클래스 변수에 지정해주고 반복 사용되도록 해도된다.

SqlSessionFactory factory = SqlMapConfig.getSqlSession();



// sch_hist메서드

public int insertSchHist(Map<String, List<SchHistDTO>> map) throws Exception {

int cnt = 0;


// mapper에 접근하기 위한 SqlSession

SqlSession sqlSession = factory.openSession(); 


SchHist2DAO schHist2DAO = new SchHist2DAO(sqlSession);

List<SchHistDTO> list = map.get("list");

for(int i = 0; i < list.size(); i++) {

SchHistDTO schHistDTO = (SchHistDTO)list.get(i);

try {

log.debug("insertSchHist() ......................... start ");

cnt = schHist2DAO.insert(schHistDTO);


sqlSession.commit();

log.debug("insertSchHist()......................... commit() ");

} catch (Exception e) {

e.printStackTrace();

throw e;

} finally {

sqlSession.close();

log.debug("insertSchHist() ......................... close() ");

}

}

log.debug("insertSchHist() ......................... end ");

return cnt;

}


------Configuration.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>


<!-- DB접속정보 값을 가지고 있는 파일 설정 -->

<properties resource="mariadb/db.properties" />

<!-- 별명 부여 -->

<!-- 

<typeAliases> 

<typeAlias type="pack.business.DataDto" alias="dto"/>

</typeAliases>

-->

<!-- DB접속 정보 셋팅 -->

<environments default="development">

<environment id="development">

<transactionManager type="JDBC" />

<dataSource type="POOLED">

<property name="driver" value="${driver}" />

<property name="url" value="${url}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

<property name="poolMaximumActiveConnections" value="20"/>

                <property name="poolMaximumIdleConnections" value="20"/>

                                <property name="poolMaximumCheckoutTime" value="20000"/>

                        <property name="poolPingEnabled" value="true"/>

                        <property name="poolPingQuery" value="select 1"/>

                        <property name="poolPingConnectionsNotUsedFor" value="10000"/>

                        <property name="poolTimeToWait" value="15000"/>

</dataSource>

</environment>

</environments>

<!-- mapper.xml 파일 설정 -->

<mappers>

<mapper resource="mariadb/mapper/DataMapper.xml" />

<mapper resource="mariadb/mapper/sch/Sch_SQL.xml" />

<mapper resource="mariadb/mapper/sch/Aggr_SQL.xml" />

<mapper resource="mariadb/mapper/sch/SchHist_SQL.xml" />

</mappers>

</configuration>




------DB정보를 가지고 있는 파일(설정파일)(db.properties)

driver=org.mariadb.jdbc.Driver

url=jdbc:mariadb://XXX.XXX.XXX.XXX:3306/sda

username=db명

password=db패스워드



------DB정보를 가지고 있는 클래스 파일(SqlMapConfig.java)

package com.gooper.icbms.sda.comm;


import java.io.Reader;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;


public class SqlMapConfig {

private static SqlSessionFactory sqlSession;


static {

String resource = "Configuration.xml";


try {

Reader reader = Resources.getResourceAsReader(resource);

sqlSession = new SqlSessionFactoryBuilder().build(reader);

reader.close();

} catch (Exception e) {

System.out.println("SqlMapConfig 오류 : " + e);

}

}


public static SqlSessionFactory getSqlSession() {

return sqlSession;

}

}

번호 제목 글쓴이 날짜 조회 수
53 [gson]mongodb의 api를 이용하여 데이타를 가져올때 "com.google.gson.stream.MalformedJsonException: Unterminated object at line..." 오류발생시 조치사항 총관리자 2017.12.11 4396
52 Caused by: java.sql.SQLNonTransientConnectionException: Could not read resultset: unexpected end of stream, read 0 bytes from 4 오류시 확인/조치할 내용 총관리자 2016.10.31 3760
51 List<Map<String, String>>형태의 데이타에서 중복제거 하는 방법 총관리자 2016.12.23 1681
50 jsoup 사용 예제 총관리자 2014.06.06 1506
49 마이바티스(MyBatis)쿼리로그 출력및 정렬하기 총관리자 2015.12.01 1448
48 lagom-linux용 build.sbt파일 내용 총관리자 2017.10.12 1300
47 [ftgo_application]Unable to infer base url오류 발생시 조치방법 gooper 2023.02.20 1254
46 Mybatis foreach 문법정리(상황에 따른 사용법) 총관리자 2015.11.10 1147
45 mybatis와 spring을 org.apache.commons.dbcp2.BasicDataSource의 DataSource로 연동할때 DB설정(참고) 총관리자 2016.10.31 990
» [Mybatis]Spring과 연동하지 않고 Java+Mybatis 형태의 프로그램 샘플소스 총관리자 2016.09.01 806
43 python test.py실행시 "ImportError: No module named pyspark" 혹은 "ImportError: No module named py4j.protocol"등의 오류 발생시 조치사항 총관리자 2017.07.04 765
42 [springframework]Caused by: org.mariadb.jdbc.internal.util.dao.QueryException: Could not read resultset: unexpected end of stream, read 0 bytes from 4 오류 발생시 조치사항 총관리자 2017.01.23 680
41 AIX 7.1에 Python 2.7.11설치하기 총관리자 2016.10.06 651
40 초기 오류(java.lang.NoSuchMethodError)에 따른 후속 작업에서 오류(java.lang.NoClassDefFoundError)가 발생되는 상황(quartz에서 주기적으로 작업시) 총관리자 2016.08.29 591
39 servlet-api를 jar형태로 build할때 포함하지 말고 java 설치 위치의 jre/lib/ext에 복사하여 사용하는것이 좋다. 총관리자 2016.08.10 446
38 Eclipse실행시 Java was started but returned exit code=1이라는 오류가 발생할때 조치방법 총관리자 2016.11.07 397
37 embedded-cassandra의 data 저장위치 총관리자 2019.06.09 336
36 VisualVM 1.3.9을 이용한 JVM 모니터링 file 총관리자 2016.10.27 332
35 python2.7.4에서 Oracle DB(11.2)를 사용하기 위한 설정(RPM을 이용하여 RHEL 7.4에 설치) 총관리자 2021.11.26 331
34 [tomcat] logrotate를 이용하여 catalina.out로그파일 일별로 로테이션 저장하기 file 총관리자 2017.01.18 318

A personal place to organize information learned during the development of such Hadoop, Hive, Hbase, Semantic IoT, etc.
We are open to the required minutes. Please send inquiries to gooper@gooper.com.

위로