메뉴 건너뛰기

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;

}

}

번호 제목 글쓴이 날짜 조회 수
400 ntp시간 맞추기 총관리자 2018.09.12 249
399 kafka의 re-balance를 이용하여 consumer를 multi thread로 돌려서 topic의 partitions을 활용 총관리자 2015.03.31 248
398 부팅을 외장하드에서 하도록 변경하는 방법 총관리자 2015.07.28 247
397 Hadoop의 Datanode를 Decommission하고 나서 HBase의 regionservers파일에 해당 노드명을 지웠는데 여전히 "Dead regionser"로 표시되는 경우 처리 총관리자 2018.01.25 246
396 oozie db변경후 재기동시 "Table 'oozie.VALIDATE_CONN' doesn't exist" 오류 발생시 조치방법 총관리자 2018.05.23 244
395 [우분투] suppoie 채굴 프로세스 발생시 자동으로 삭제하는 shell프로그램 총관리자 2018.04.01 243
394 ResultSet에서 데이타를 List<Map<String,String>>형태로 만들어서 리턴하는 소스(Collections.sort를 이용한 정렬 가능) 총관리자 2016.12.15 242
393 cumulusRDF 1.0.1설치및 "KeyspaceCumulus" keyspace확인하기 file 총관리자 2016.04.15 242
392 Hbase API를 이용하여 scan시 페이징을 고려하여 목록을 가져올때 사용할 수 있는 로직의 예시를 보여줌 총관리자 2017.04.26 239
391 down된 broker로 메세지를 전송하려는 경우의 오류 내용및 조치사항 총관리자 2016.08.12 238
390 [Dovecot] -ERR [SYS/PERM] Permission denied 총관리자 2017.06.13 237
389 AIX 7.1에 Hadoop설치(정리중) 총관리자 2016.09.12 236
388 Scala에서 countByWindow를 이용하기(예제) 총관리자 2018.03.08 235
387 Windows7 64bit 환경에서 Apache Hadoop 2.7.1설치하기 총관리자 2017.07.26 235
386 AIX 7.1에서 hive실행시 "hive: line 86: readlink: command not found" 오류가 발생시 임시 조치사항 총관리자 2016.09.25 233
385 CentOS에서 리눅스(Linux) 포트 열기, 방화벽 설정/해제 등. 총관리자 2016.03.14 231
384 Cloudera Manager에서 "Mismatched CDH versions: host has NONE but role expects 5 Suppress..."와 같이 오류 발생시 확인사항 총관리자 2018.06.06 228
383 HDFS 파일및 디렉토리 생성시 생성방법에 따라 권한이 다르게 부여된다. gooper 2022.05.30 227
382 hive 0.13.1 설치 + meta정보는 postgresql 9.3에 저장 총관리자 2015.04.30 227
381 [postgresql 9.x] PostgreSQL Replication 구축하기 총관리자 2018.07.17 226

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.

위로