메뉴 건너뛰기

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;

}

}

번호 제목 글쓴이 날짜 조회 수
399 Mysql DB 생성 및 권한. 특정아이피, 대역에 대한 접근 허용 총관리자 2017.05.04 60
398 Hive MetaStore Server기동시 Could not create "increment"/"table" value-generation container SEQUENCE_TABLE since autoCreate flags do not allow it. 오류발생시 조치사항 총관리자 2017.05.03 341
397 Ubuntu 16.04 LTS에 Hive 2.1.1설치하면서 "Version information not found in metastore"발생하는 오류원인및 조치사항 총관리자 2017.05.03 469
396 우분투에서 패키지 설치시 E: Sub-process /usr/bin/dpkg returned an error code 발생시 조치 총관리자 2017.05.02 251
395 hadoop에서 yarn jar ..를 이용하여 appliction을 실행하여 정상적(?)으로 수행되었으나 yarn UI의 어플리케이션 목록에 나타나지 않는 문제 총관리자 2017.05.02 77
394 hadoop에서 yarn jar ..를 이용하여 appliction을 실행하여 정상적으로 수행되었으나 yarn UI의 어플리케이션 목록에 나타나지 않는 문제 총관리자 2017.05.02 117
393 hadoop에서 yarn jar ..를 이용하여 appliction을 실행하여 정상적으로 수행되었으나 yarn UI의 어플리케이션 목록에 나타나지 않는 문제 총관리자 2017.05.02 51
392 hadoop에서 yarn jar ..를 이용하여 appliction을 실행하여 정상적으로 수행되었으나 yarn UI의 어플리케이션 목록에 나타나지 않는 문제 총관리자 2017.05.02 24
391 Cleaning up the staging area file시 'cannot access' 혹은 'Directory is not writable' 발생시 조치사항 총관리자 2017.05.02 333
390 test333444 총관리자 2017.05.01 113
389 test333 총관리자 2017.05.01 189
388 test2 총관리자 2017.05.01 153
387 Ubuntu 16.04 LTS에 MariaDB 10.1설치 및 포트변경 및 원격접속 허용 총관리자 2017.05.01 1068
386 Ubuntu 16.04 LTS에 4대에 Hadoop 2.8.0설치 총관리자 2017.05.01 520
385 fuseki webUI를 통해서 전체 카운트를 하면 급격하게 메모리를 소모해 버리는 문제가 있음 file 총관리자 2017.04.28 162
384 Kafka의 API중 Consumer.createJavaConsumerConnector()를 이용하고 다수의 thread를 생성하여 Kafka broker의 topic에 접근하여 데이타를 가져오고 처리하는 예제 소스 총관리자 2017.04.26 226
383 Spark에서 KafkaUtils.createStream()를 이용하여 이용하여 kafka topic에 접근하여 객채로 저장된 값을 가져오고 처리하는 예제 소스 총관리자 2017.04.26 292
382 Hbase API를 이용하여 scan시 페이징을 고려하여 목록을 가져올때 사용할 수 있는 로직의 예시를 보여줌 총관리자 2017.04.26 238
381 linux에서 특정 포트를 사용하는 프로세스 확인하기 총관리자 2017.04.26 346
380 Spark에서 Serializable관련 오류및 조치사항 총관리자 2017.04.21 4901

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.

위로