json 데이터를 저장할 hive 테이블을 생성하는 예시이다. 

CREATE TABLE if not exists member

(

   name STRING,

   id INT, 

   props STRUCT <

       departname:STRING,

       alias:STRING

   >

) ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'

STORED AS TEXTFILE





json 데이터를 hive에 저장할 때 개행 문자가 있으면 에러가 발생한다. 


$ cat "test.json"

{

  "name":"samuel",

  "id":1,

  "props": {

    "departname":"none",

    "alias":"kim"

  }

}


다음 에러로 발생한다. 

Failed with exception java.io.IOException:org.apache.hadoop.hive.serde2.SerDeException: Row is not a valid JSON Object - JSONException: A JSONObject text must end with '}' at 2 [character 3 line 1]


따라서 개행 문자 없이 사용해야 한다. 


$ cat "test.json"

{  "name":"samuel",  "id":1,  "props":{    "departname":"none",    "alias":"kim"  }}






이제 하둡에 json 파일을 업로드한다.


$ hadoop fs -put test.json /lib/test.json


hive 실행을 실행해서 데이터를 읽게 한다.



hive> LOAD DATA INPATH '/lib/test.json' OVERWRITE INTO TABLE member;



hive> select * from member;

OK

samuel 1 {"departname":"none","alias":"kim"}

Time taken: 0.614 seconds, Fetched: 1 row(s)


SELECT props.alias FROM member WHERE props.departname="none";

kim



Posted by '김용환'
,



hive에서 json 컬럼에 대해서 json 쿼리를 진행할 수 있다. 



hive> select * from googleplus_log where date_id='2037-08-23' and label='birthday' and get_json_object(props, '$.id')='11935385';




json_tuple도 사용할 수 있지만, lateral view도 써야 하고 parittion table이라면 strict 모드로 바꾸라 한다. 


hive> select log.date_id, b.* from googleplus_log log lateral view json_tuple(log.props, 'result', 'birth', 'id', 'action') b where date_id='2017-02-23' and label='birthday'; 


This happens cos the hive is set to strict mode. this allow the partition table to access the respective partition /folder in hdfs .



hive>  set hive.mapred.mode=unstrict; 


hive> select log.date_id, b.* from googleplus_log log lateral view json_tuple(log.props, 'result', 'birth', 'id', 'action') b where date_id='2017-02-23' and label='birthday'; 

##잘 동작한다.






Posted by '김용환'
,


read에 -r 옵션을 실행한 상태에서 라인 끝에 \ 문자를 추가하더라도 더 이상 라인을 계속 입력받지 않는다.



[~] read a

adfasdf asdfasdf afdsaf \

> 1 2 \

> 3 4

[~] echo $a

adfasdf asdfasdf afdsaf 1 2 3 4

[~] read -r b

asdfsad asdfasfd asdfsafd \

[~] echo $b

asdfsad asdfasfd asdfsafd \

Posted by '김용환'
,


Spark에서 Executor 또는 Driver의 메모리가 너무 적으면 OutOfMemoryError가 발생할 수 있다. 


WARN TaskSetManager: Lost task 68.0 in stage 9.0 (TID 510, story-hadoop-dn11.dakao.io): java.lang.OutOfMemoryError: GC overhead limit exceeded

	at java.io.ObjectStreamClass.newInstance(ObjectStreamClass.java:967)
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1785)
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
	at org.apache.spark.serializer.JavaDeserializationStream.readObject(JavaSerializer.scala:76)
	at org.apache.spark.serializer.DeserializationStream.readKey(Serializer.scala:169)
	at org.apache.spark.serializer.DeserializationStream$$anon$2.getNext(Serializer.scala:201)
	at org.apache.spark.serializer.DeserializationStream$$anon$2.getNext(Serializer.scala:198)
	at org.apache.spark.util.NextIterator.hasNext(NextIterator.scala:73)
	at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:396)
	at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:369)
	at org.apache.spark.util.CompletionIterator.hasNext(CompletionIterator.scala:32)
	at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:39)
	at scala.collection.Iterator$class.foreach(Iterator.scala:742)
	at org.apache.spark.InterruptibleIterator.foreach(InterruptibleIterator.scala:28)
	at org.apache.spark.rdd.SubtractedRDD.integrate$1(SubtractedRDD.scala:122)
	at org.apache.spark.rdd.SubtractedRDD.compute(SubtractedRDD.scala:127)
	at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306)



Spark는 모두 메모리에 올리기 때문에. 이런 현상이 자주 발생될 수 있는데, 


해결하기 위해서는 Hdfs 파일의 크기(압축이라면 이에 맞게 *8 또는 *10해서 대략 예측 크기를 고려한다)


core 개수와 driver, executer 메모리도 이에 맞게 늘리면 더 이상 문제가 발생하지 않는다. 

'scala' 카테고리의 다른 글

[scala] List와 Array의 lift 메소드  (0) 2017.03.04
[펌] spark 2.0 소개(성능)  (0) 2017.03.02
[spark] spark summit 자료  (0) 2017.02.22
[scala] Array.transpose 예시  (0) 2017.02.17
[spark1.6] rdd를 dataframe으로 만드는 방법  (0) 2017.02.14
Posted by '김용환'
,


powertop 커맨드는 네트워크 카드의 상태 모니터링 뿐 아니라, c-state로 들어갈 지 여부를 설정할 수 있다. 

또한 정보를 csv로 사용할 수 있다.


c-state는 Intel에서 만든 전원 상태를 관리할 수 있는 방법이라 할 수 있다. cpu 내부에서 사용중이면 활성화하고 사용 중이 아니라면 낮은 성능을 유지시켜 전원을 낮게 쓰게 하는 효율적으로 유지한다. 


아래 링크의 공식 문서(Controlling Processor C-StateUsage in Linux)에 따르면 설명이 되어 있다. 




while it does save power, it takes time to enter and exit C-states. Generally the higher theC-state, more power is saved when the processor is idle, but it takes longer to get the CPU back upagain when it is needed. The C-states are:


C0 The CPU is actively running code


C1 The CPU uses the HLT instruction when idle-the clock is gated off to parts of the core, but it is relatively quick to wake up


C1E This is actually just C1, except when C1E is enabled, the CPU lowers the CPU’s speed & voltage when it is in C1


C2 & up The CPU will shut off various parts of the core for greater power savings, at thecost of taking longer to wake up


In most Linux distributions today, C-states are enabled by default, which is great for most users. Theoperating system will choose which C-state to use based on processor utilization and other factors.Some users, though, want very low latency, and are very sensitive to how quickly a CPU can run codewhen needed, so user control is desired.



문제는 c-state 상태를 intel cpu가 마음대로 결정하는 것이 default 설정이라서, 서비스 장비의 네트워크 카드까지 idle로 만들 수 있다는 점이다. 잠깐 쉬고 있는 것이지 아예 안쓰는 것은 아니라서..


그래서 자바 애플리케이션 서버에서 다음과 같은 에러를 발생한다.


java.net.UnknownHostException: googleplus001: unknown error
    at java.net.InetAddress.getLocalHost(InetAddress.java:1505)



c-state가 적용되지 않게 하려면 다음을 설정한다. 아래 링크의 공식 문서(Controlling Processor C-StateUsage in Linux)대로 진행한다. 


if you want control over C-states, or if your system might have CPUs that are newerthan the version of intel_idle in your kernel, you should use kernel parameter "intel_idle.max_cstate=0" to disable this driver.


커널 매개 변수로 intel_idle.max_cstate=0을 적용한다. 

grub 파일을 쓰고 있다면, /boot/grub/grub.conf 파일에 intel_idle.max_cstate=0를 설정하고 재시작한다. 

기존에 어떻게 동작하고 있었는지 보려면 grep idle /var/log/messages를 실행한다. 




그러면 항상 cpu에서는 모든 프로세스를 c0 상태로 두게 될 것이다. (acpi_idle 모듈이 사용됨을 의미한다)



부팅후에는 아래 커맨드로 확인해서 정상적으로 적용되었는지 확인한다.  (디폴트 값도 확인할 수 있다)


$ dmesg | grep -i intel_idle


acpi_idle이 동작되는지 살펴보면 다음과 같다. 


$ cat /proc/acpi/processor/CPU0/power

active state:            C0

max_cstate:              C1

maximum allowed latency: 2000000000 usec

states:






powertop 설치와 사용 방법은 다음과 같다. 


$ yum install powertop


su 사용자이거나 sudo를 사용해 실행한다.

$ sudo powertop


탭 간 이동은 tab을 이용하고 종료는 q를 누른다. idle 상태를 확인한다. 




Idle stat을 보면 C0로 되어 있는지 여부를 확인할 수 있다. 











여기서 Bad는 실제 c-state과 관련된 설정임을 의미한다. 나쁘다는 의미는 아니다. 



참고 


http://www.tecmint.com/powertop-monitors-linux-laptop-battery-usage/


https://www.scribd.com/document/132381013/Controlling-Processor-C-State-Usage-in-Linux-Nov2012


https://events.linuxfoundation.org/slides/2010/linuxcon2010_brown.pdf



Posted by '김용환'
,