February 11, 2017

Joyent의 만타 (Manta): 저장하고 계산하다

이제까지 우리는 Joyent의 Triton 사용했어요. 트리툰으로 Docker 컨테이너를 배포했어요. 아마 데이터를 저장하고 싶어요. 만타는 아마존 S3 같은 스토리지 서비스예요. 근데 만다는 계산도 할 수 있어요. 시작하자!

data store blog
Figure 1. 저장만 할 수 있으면 데이터를 항상 전송해야 돼요.
manta data ocean blog
Figure 2. 만타 계산할 수 있어서 데이데이터 전송 필요 없어요.

준비하자!

만다는 (Manta) REST-API가 있어요. 근데 우리는 이 블로그 글에 만타의 앱을 사용할 거예요. npm로 설치하세요:

sudo npm install manta -g

그리고 우리는 만타 환경을 준비해야 돼요. 만타 웹 사이트에서 복사하세요:

# https://my.joyent.com/main/#!/manta/intro Lists your's
export MANTA_URL=https://us-east.manta.joyent.com;
export MANTA_USER=<Joyent-User>;
export MANTA_KEY_ID=<ssh-public-key-finger-print>;
# Or read it directly from your local .ssh keys
export MANTA_KEY_ID=$(ssh-keygen -l -f $HOME/.ssh/id_rsa.pub | awk '{print $2}')

지금 다 준비됐어요!

파일들을 저장하자

우리는 먼저 파일을 몆게 업로드하세요.

echo "Hi there, internet" > ~/hello-manta.txt
# Putting up a simple file
mput -f ~/hello-manta.txt ~~/stor/hello-manta.txt

# Can get it again
mget ~~/stor/hello-manta.txt

# Can put any content, here piped beer data from stack exchange
curl -sL https://tools.ietf.org/rfc/rfc2616.txt | mput -p ~~/stor/blog/rfc2616.txt

# List directory
mls ~~/stor/blog/
# List more
mls -l ~~/stor/blog/

# Find stuff
mfind -n rfc*.txt ~~/stor/blog/

# Delete everything
mrm -r ~~/stor/blog/

mput`로 업로드해요. `-f`는 이 파일 업로드해요. `~~/stor/hello-manta.txt`는 만타의 파일 어디에 저장해요. `~~’는 unix 같은 사용자의 폴더가 뜻이에요. 그리고 `mget`로 파일 다시 다운로드할 수 있어요. `mput`는 유닉스 파이프로 다 업로드할 수 있어요. 여기에 우리는 RFC-문서 업로드해요. `-p`는 폴더가 아직 없으면 새로운 폴더 만들 거예요. `mls 플더를 명부하고 mfind`로 파일을 이름으로 찾을 수 있어요. 그리고 `mrm 파일을 삭제해요.

만타의 비밀은: 계산

우리는 먼저 영화 몇게 업로드할게요.

curl -sL http://www.caminandes.com/download/01_llama_drama_1080p.zip | mput -p ~~/stor/blog/caminandes_01.zip
curl -sL http://www.caminandes.com/download/02_gran_dillama_1080p.zip | mput -p ~~/stor/blog/caminandes_02.zip
curl -sL http://www.caminandes.com/download/03_caminandes_llamigos_1080p.mp4 | mput -p ~~/stor/blog/caminandes_03.mp4

그럼 이 파일일 만타에서 저장했어요. 아마 핸드폰 위해 작은 비디오 파일 좋겠다. 그래서 우리는 비디오 다운 받고 트랜스 코드 하고 업로드할까요? 이 파일 몇GB 크면 다운 받고 다시 업로드할까요? 아니요! 만타는 계산할 수 있어서 우리는 만타에 트랜스 코드할 수 있어요. 아, 그래서 우리는 새로운 프레임워크(framework) 배워야 돼요? 아니요! 만타는 그냥 보통 유닉스 프로그램 사용해요! 시작하자!

# Login to you're stored file.
# After that, you get a regular prompt
mlogin ~~/stor/blog/caminandes_03.mp4

# Check out the enviroment
env
# The MANTA_INPUT_FILE will contain the actual file
# The MANTA_INPUT_OBJECT the name in the store
MANTA_OUTPUT_BASE=/Gamlor/jobs/f018b6a5-aa28-44fb-bc30-b5cca7a1db60/stor/Gamlor/stor/blog/caminandes_03.mp4.0.
MANTA_INPUT_FILE=/manta/Gamlor/stor/blog/caminandes_03.mp4
MANTA_INPUT_OBJECT=/Gamlor/stor/blog/caminandes_03.mp4

# Try out things you wish to do on the file. Like for a video, ffmpeg can transcode a video to smaller sizes.
# Like this one to make it way smaller, for like a phone
ffmpeg -i $MANTA_INPUT_FILE -strict -2 -b:v 500k -s 320x240 -vcodec mpeg4 -acodec aac ~/small.mp4

# Once you explored, you can exit
exit

우리는 `mlogin`로 이 파일을 연락해요. 진짜요. 이렇게 파일 옆에서 모은 유닉스 프로그램 사용하고 모은 계산할 수 있어요. 우리는 작은 비디오를 만들고 싶어서 ‘ffmpeg’ 사용해봐요. 그데 우니는 모든 비디오를 트렌수 코드 하고 싶어요. 그래요! `mjob`로 사용 시작해요.

#mjob: Takes a list of Manta path. Applies the program on it. -o waits for the task and returns the stdout
echo ~~/stor/blog/caminandes_03.mp4 | mjob create -o -m 'sha1sum'
#=> added 1 input to 61c30a0c-c7f0-e09c-c995-8fc5d61b06c3
#=> ddd2bf01f87be76b875efafd46c9930f722113b5  -

#So, with mfind, we can now do calculations across files
mfind  ~~/stor/blog/ | mjob create -o -m 'sha1sum'
#=> added 3 inputs to fb3aa947-b637-c5ea-8d38-b42620cbef1d
#=> 07acf89b74b677432acc3ee6579bcbb8ee13640e  -
#=> 0e502cad377f75973c597eab2318f39aa4763ad4  -
#=> ddd2bf01f87be76b875efafd46c9930f722113b5  -

#Or more pretty listing.
# First we hash the data: sha1sum
# Then fetch extract the first colum. Note that we escape the dollar sign: awk "{print \$1}"
# Last, compose together the hash and the file name: echo $(cat) $(basename $MANTA_INPUT_OBJECT)
mfind ~~/stor/blog/ | mjob create -o -m 'sha1sum | awk "{print \$1}"| echo $(cat) $(basename $MANTA_INPUT_OBJECT)'
#=> added 3 inputs to 8bc9b130-9b73-47c5-dec9-ed4e87cdc761
#=> 07acf89b74b677432acc3ee6579bcbb8ee13640e caminandes_02.zip
#=> ddd2bf01f87be76b875efafd46c9930f722113b5 caminandes_03.mp4
#=> 0e502cad377f75973c597eab2318f39aa4763ad4 caminandes_01.zip



# For our movie tranformation, we first unzip the zip files.
# zip files are not streamable. So we ditch the stdin, an4d read from the file: unzip $MANTA_INPUT_FILE -d ~/out < /dev/null
# Then, get the output file name: tail -n 1
# Extract the actual file name column: awk '{print $2}'
# Push that file to stdout: xargs cat
# And use manta's mpipe to create a named Manta output file: mpipe ${MANTA_INPUT_OBJECT}.mp4
mfind -t o -n '.zip$' ~~/stor/blog/ | mjob create -w -m "unzip \$MANTA_INPUT_FILE -d ~/out < /dev/null \
| tail -n 1 | awk '{print \$2}' | xargs cat | mpipe \${MANTA_INPUT_OBJECT}.mp4"

# Last step. Transcode the videos to a mobile format:
# The downloaded video might not be streamable. So we cannot take it from the stdin. So, just read if from the file.
# First transcode it to a 600kbit/s stream, 320x240 resolution, mp4 format: ffmpeg -nostdin -i \$MANTA_INPUT_FILE -strict -2 -b:v 300k -s 320x240 -vcodec mpeg4 -acodec aac ~/tmp.mp4
# Cat the file, pipe it to a named Manta output file: cat ~/tmp.mp4 | mpipe \${MANTA_INPUT_OBJECT}.mobile.mp4
mfind -t o -n '.mp4$' ~~/stor/blog/ | mjob create -w -m "ffmpeg -nostdin -i \$MANTA_INPUT_FILE \
-strict -2 -b:v 600k -s 320x240 -vcodec mpeg4 -acodec aac ~/tmp.mp4 > /dev/null && cat ~/tmp.mp4 | mpipe \${MANTA_INPUT_OBJECT}.mobile.mp4"

# After completion:
mls ~~/stor/blog
#=> caminandes_01.zip
#=> caminandes_01.zip.mp4
#=> caminandes_01.zip.mp4.mobile.mp4
#=> caminandes_02.zip
#=> caminandes_02.zip.mp4
#=> caminandes_02.zip.mp4.mobile.mp4
#=> caminandes_03.mp4
#=> caminandes_03.mp4.mobile.mp4

# fetch a mobile video to check it out:
mget ~~/stor/blog/caminandes_03.mp4.mobile.mp4 > caminandes_03.mp4.mobile.mp4

`mjob`는 만터 파일 경로를 `stdin`로 받아요. `mjob create`가 새로운 계산 시작해요. `-o`를 사용 하면 계산 끝날 때 결과를 생길을 거예요. 그리고 `-m`후에 계산 있어요.

1번 예: ~~/stor/blog/caminandes_03.mp4′ 파일의 `sha1 계산해요.

2번 예: mfind`로 모든 파일 찾고 파일의 `sha1 계산해요.

3번 예: 우리가 유닉스 파이프 사용할 수 있어요. 그래서 잘 읽을 수 있는 줄 만들어요.

그럼 유용한 계산 해봐요. caminandes_01.zip하고 caminandes_02.zip 파일 `unzip`로 열고 `mpipe`로 새로운 만타 파일을 저장해요. `-w`가 계산의 끝을 기다려요. 그리고 이제 작은 핸드폰의 비디오 만들 수 있어요. `ffmpeg`로 트렌수 쿠딩 하고 mpipe로 파일을 저장해요. 타다~~! 작은 비디오 있어요. `mget`로 다운 받으세요. 이건 다 만타로 했어요. 이 파일들 크면 아무것도 다운 받지 않고 저기에, 만타에 계산했어요. 대박!

맵리듀스

이제까지 우리는 `mjob -m`만 사용했어요. `mjob`가 맵리듀스 할 수 있어요. 개요를 만들고 싶으면 맵리듀스 할 수 있어요. 예를 보일 거예요. 모든 비디오의 비트 전송속도 통계 해요:

# Let's list the bit rate:
# First find the bitrate: ffprobe  $MANTA_INPUT_FILE 2>&1
# Find the bitrate line: grep bitrate
mfind -n mp4$ ~~/stor/blog | mjob create -o -m 'ffprobe  $MANTA_INPUT_FILE 2>&1 | grep bitrate'
#=> added 6 inputs to 00abc11b-2bad-405c-8add-941400614cc4
#=> Duration: 00:02:26.05, start: 0.000000, bitrate: 6900 kb/s
#=> Duration: 00:02:30.13, start: 0.000000, bitrate: 10680 kb/s
#=> Duration: 00:02:30.12, start: 0.021333, bitrate: 717 kb/s
#=> Duration: 00:01:30.02, start: 0.023220, bitrate: 672 kb/s
#=> Duration: 00:02:26.08, start: 0.021333, bitrate: 725 kb/s
#=> Duration: 00:01:30.00, start: 0.000000, bitrate: 3120 kb/s

# Let's list the bit rate again:
# Only extract the bit rate colum: awk "{print \$6}"
mfind -n mp4$ ~~/stor/blog | mjob create -o -m 'ffprobe  $MANTA_INPUT_FILE 2>&1 | grep bitrate | awk "{print \$6}"'
#=> added 6 inputs to 6c1b8b80-1516-e8e5-f6b6-99c5ebcd9f3b
#=> 6900
#=> 672
#=> 10680
#=> 717
#=> 725
#=> 3120

# With the reduce phase we can collect the result's back together.
# For example, get the min, max and mean bit reate of all our videos
mfind -n mp4$ ~~/stor/blog | mjob create -o -m 'ffprobe  $MANTA_INPUT_FILE 2>&1 | grep bitrate | awk "{print \$6}"' \
-r 'maggr max,min,mean'

먼저 `mjob`로 비디오 전송속도 받아요. `ffprobe`가 비디오의 정보의 받고 `grep`로 bitrate를 찾고 `awk`로 bitrate의 6열 받아요. 마지막 , `-r’후에 리듀스 있어요. 여기에 maggr로 통계 해요. (ㅅ_ㅅ)

만타 탐색해주세요

저는 많은 주제 빼었어요. 만타의 안내 보고 탐색해주세요. =)

Tags: 한국어 Joyent Manta