mysql 원격copy

BACK-END 2017. 5. 17. 12:09

옆동료에게 나의 db를 그대로 복사해주고 싶은 경우,


일단 원격 접속을 해야하기 때문에

1.  mysql> grant all privileges on *.* to 'root'@'10.33.%';   동료pc에서 이런식으로 접속 허용을 해준다.


2. $ mysqldump -u root databaseName > temp.sql

3. 동료 mysql에 접속한 후 ( $mysql -h 동료pc_IP  -u root ) 아래명령 실행.

   mysql> create database databaseName

   mysql> use dataBaseName

   mysql> source temp.sql




Posted by yongary
,

Builder 와 Factory

java core 2017. 5. 9. 23:20

Builder는 간단히 말하면 setter가 다 끝나고 생성자를 호출해 주는 패턴이다. 

개념은 간단한데 왜 필요한가에 대한 답은 :  복잡한 생성자와 복잡한 setter가 섞여있는 경우 유용하다. REF 

예) PersonInfo personInfo = new Builder("Mommoo").setAge(12).setPhonNumber(119).build( );

 

Factory는 여러개의 subClass가 있을경우 생성자가 각각 생기는 복잡한 문제가 있는데..

이를 하나의 생성자로 통일해 주는 큰 장접이 있다.   REF

 

 

Posted by yongary
,

@Bean 사용법

Spring 2017. 5. 8. 14:19

@Autowired 를 사용할 경우에는 classpath내에 존재해야 하는데,

그게 여의치 않거나 새로운 생성자가 필요한 경우에는


생성자를 직접 정의하면서 @Bean을 사용할 수 있다. 


1.정의시:


@Configuration

public class OkHttpClientConfig {

@Bean(name = CHAT_BILLING_HTTP_CLIENT)

OkHttpClient chatBillingHttpClient() {

OkHttpClient client = new OkHttpClient();

client.setTimeOut(blabla)


return client;

}



2.사용시:


@Qualifier(OkHttpClientConfig.CHAT_BILLING_HTTP_CLIENT)

@Autowired

OkHttpClient chatBillingHttpClient;




Posted by yongary
,


REF



mySql에 dateTime을 지정해서 입력하는 경우,


STR_TO_DATE('12-01-2014 00:00:00','%m-%d-%Y %H:%i:%s') 을 사용해서 insert할 수도 있지만

아래와 같이 간단히 숫자로도 가능하다. 



insert into campaign(campaign_code,version,memo,start_at,end_at,created_at,updated_at) values('bogo',1,'test',20170509150000,20170511110000,now(),now());

Posted by yongary
,

Worker Thread 패턴

java core 2017. 4. 27. 15:19

REF

 

제한된 thread개수를 이용하여 여러개의 작업을 하고 싶을때 Worker Thread와 Pool을 이용하면 된다.

 

REF:myBlog

Posted by yongary
,

각종 DB에러 exception

BACK-END 2017. 4. 25. 16:57

여러가지 DB 관련된 에러를 포함하는 java exception은  DataAccessException으로 보인다.


http://helols.tistory.com/12  

Posted by yongary
,

Regex

java core 2017. 4. 20. 18:28

내가 많이 쓰는 간단한 방식은

String pattern = "[Tt]he";                                          // [A-Za-z]   \\s \\w \\d

boolean matched = "the".matches(pattern);  //=> true : 시작^ 끝$까지 포함해서정확히 일치해야 함.

(참고: https://codechacha.com/ko/java-regex/)

 

알파벳/숫자/언더바만 포함되었는지 체크하려면..
String pattern = "^[\\w]*$";
boolean a = "abc123".matches(pattern);

 

알파벳/숫자 만 체크하려면..
String pattern = "^[a-zA-Z0-9]*$";
boolean a = "abc123".matches(pattern);

 

그룹에서 \1 즉 "\\1"은 첫번째 엘리먼트를 뜻함. = m.group(1)
\\b (바운더리) 는 이스케이프 캐릭터로서 단어를 찾을때 앞뒤에 넣으면 좋음.

중복된 단어 하나로 만들기 예제
        // String regex = "\\b(\\w+)\\b(?=.*\\b\\1\\b)";
        // String regex = "(\\b\\w+)(\\W\\1\\b)+";
        
        //이것도 잘되나 String regex = "\\b(\\w+)(\\s\\1)+\\b";
        String regex = "(\\w+)(\\s\\1)+"; //이게 제일 심플하면서 어느정도 동작 확인. 
          //스페이스 제외하고 앞부분만을 word로 보는듯. 
        
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

            String input = in.nextLine();
            
            Matcher m = p.matcher(input);
           
            // Check for subsequences of input that match the compiled pattern
            while (m.find()) {
                //  System.out.println("==" + m.group() + "," + m.group(1)+".");
                input = input.replaceAll(m.group() , m.group(1));
            }
            // Prints the modified sentence.
            System.out.println(input);

 

Regex Test 사이트는  https://regex101.com/

 

java API:  https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html 

Regex자료는 여기서 참고: Ref

 

표현식  설명 
 ^  문자열의 시작,   [^a-z] 에서는 NOT의 의미로 쓰임.
 문자열의 종료
 .  임의의 한 문자 (문자의 종류 가리지 않음)
 단, \ 는 넣을 수 없음
 *  앞 문자가 없을 수도 무한정 많을 수도 있음
 앞 문자가 하나 이상
 앞 문자가 없거나 하나있음
 []  문자의 집합이나 범위를 나타내며 두 문자 사이는 - 기호로 범위를 나타낸다. []내에서 ^가 선행하여 존재하면 not 을 나타낸다.
 {}  횟수 또는 범위를 나타낸다. =>   {1,3} 콤마를 써야 함.
 ()  소괄호 안의 문자를 하나의 문자로 인식 
 |  패턴 안에서 or 연산을 수행할 때 사용
 \s  공백 문자
 \S  공백 문자가 아닌 나머지 문자
 \w  알파벳이나 숫자 (언더바도 체크함)
\W   알파벳이나 숫자를 제외한 문자
\d   숫자 [0-9]와 동일
\D   숫자를 제외한 모든 문자
 정규표현식 역슬래시(\)는 확장 문자
 역슬래시 다음에 일반 문자가 오면 특수문자로 취급하고 역슬래시 다음에 특수문자가 오면 그 문자 자체를 의미
(?i)   앞 부분에 (?i) 라는 옵션을 넣어주면 대소문자를 구분하지 않음

▲ 출처 : http://lng1982.tistory.com/141

 

 

 

Posted by yongary
,

spring 에서 async기능구현 필요가 있다면 메쏘드에 @Async를 사용하면 된다.

이 때, Application에는 @EnableAsync가 필요하다.


 REF



Retryable은 몇번의 실패을 다시 해주게 해주는데, 아래와 같이 사용하면된다.

정해진 실패 회수를 초과할 경우 @Recover함수가 불리게 되는데, exception + 파라미터가 동일하다는 점에 주의! 


Application에는  @EnableRetry가 필요하다.  


private static final int MAX_ATTEMPTS = 4;
private static final long BACK_OFF_DELAY = 2000;  //msec
@Retryable(value = DataAccessResourceFailureException.class,
maxAttempts = MAX_ATTEMPTS,
backoff = @Backoff(delay = BACK_OFF_DELAY))
public void checkAndProcess(String userKey, String itemId) {

@Recover
public void recover(DataAccessResourceFailureException e, String userKey, String itemId) {
log.error("All retries have failed!, userKey:{} " + e.toString(), userKey);
}


Posted by yongary
,

reflection

java core 2017. 4. 12. 10:03

private 함수를 테스트 할 경우, java의 reflection기능을 사용해서 test할 수 있다.


import java.lang.reflect.Method;


Map<String, Long> repeatMap = null;
try {
Method method = ChatRankingService.class.getDeclaredMethod("getRepeatCount", List.class);
method.setAccessible(true);
repeatMap = (Map<String, Long>)method.invoke(chatRankingService, serviceCodes);
} catch (Exception e) {
}

이와 같이 method.setAccessible(true)를 하면 private함수도 실행이 가능하다. 

Posted by yongary
,

REF



oracle에서 workbench를 다운로드 해서( GPL 라이선스라 로그인은 필요함)

실행한 후, 


Database메뉴에서 

 => Reverse Engineering을 하면, 




기존시스템의 ERD를 뽑을 수 있게 된다.



Posted by yongary
,