I have written a simple bash script. The exact code is here.
ideone.com/8XQCjH
#!/bin/bash
if ! bzip2 -t "$file"
then
printf '%s is corrupted\n' "$file"
rm -f "$file"
#echo "$file" "is corrupted" >> corrupted.log
else
tar -xjvf "$file" -C ./uncompressed
rm -f "$file"
fi
Basically, it reads a compressed file, tests it and uncompresses it and moves it to another directory.
How do I modify this code so that it will be able to read files in a hdfs input directory instead and output to another hdfs output directory ?
I have seen some examples here which though involves reading the contents of the file. Though in my case, I am not interested in reading any contents.
http://www.oraclealchemist.com/news/tf-idf-hadoop-streaming-bash-part-1/
If anyone could write a hadoop command which unzips files in a hdfs or a similar example, that'll greatly help me.
Edit:
Try 1:
hadoop fs -get /input/temp.tar.bz2 | tar -xjv | hadoop fs -put - /output
Not good as it moves the file into the native filesystem, uncompresses it and puts it back into the output directory in hdfs.
Try 2:
wrote a script uncompress.sh with just one line of code
uncompress.sh
tar -xjv
hadoop jar contrib/streaming/hadoop-streaming.jar \
-numReduceTasks 0 \
-file /home/hadoop/uncompress.sh \
-input /input/temp.tar.bz2 \
-output /output \
-mapper uncompress.sh \
-verbose
However this gave the below error.
INFO mapreduce.Job: Task Id : attempt_1409019525368_0015_m_000002_0, Status : FAILED
Error: java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 2
Thanks
From the man page of bzip2:
-t --test
Check integrity of the specified file(s), but don't decompress them. This really performs a trial decompression and throws away the result
This means that there is no way to check the file without reading it. Also, if you are going to perform the decompression after that if the archive is deemed valid, you should probably decompress it directly.
That said, you can use
hadoop fs -cat hdfs://my_file_name | bzip2 -ct
to test the file and
tmpdir=`mktmp -d`
hadoop fs -cat hdfs://my_file_name | tar jxv -C $tmpdir
hadoop fs -copyFromLocal $tmpdir/ hdfs://dest_dir
to decompress it. There is no way to have tar write the files directly into hdfs. Hadoop streaming is thought as "download the stuff you need, perform the job in a temp directory, upload them back".
That said, you are using hadoop to perform decompression of a large number of files, or you want to parallelize the decompression of a big single giant file? In the second case you have to write an ad-hoc program to split the input into multiple parts, and decompress them. Hadoop will not automatically parallelize tasks for you. In the first case, you can use a script like this as mapper:
#!/bin/bash
while IFS="\n" read filename ; do
tmpdir=`mktmp -d`
hadoop fs -cat "hdfs:/$filename" | tar jxv -C $tmpdir
hadoop fs -copyFromLocal $tmpdir/ "hdfs:/$filename".dir/
rm -rf $tmpdir
done
and as input you use instead a file with the list of the tar.bz2 files to decompress
...
/path/my_file.tar.bz2
/path2/other_file.tar.bz2
....
Related
I am copying a folder from one path to another, basically creating a backup.
The source(input) folder size is 5 TB. I use the following distcp command to copy:
hadoop distcp -m 150 <source_folder_path> <destination_folder_path>
hadoop fs -du -s -h source_folder
hadoop fs -du -s -h destination_folder
hadoop fs -ls source_folder | wc -l
hadoop fs -ls destination_folder | wc -l
This is within the same cluster.
I am unable to understand as why my input folder is 5 tb and output folder is only 1 tb. The job completes successfully without any error.
Also I see that the number of files is same in input and output.
I don't use compression or anything in the process. Can someone point out to me why is it like this.
Hadoop version is 2.7
When I execute these commands, it is very slow and always takes five hours or more to finish.
hdfs dfsadmin -fetchImage ${t_save_fsimage_path}
# 获取下载的fsimage具体文件路径
t_fsimage_file=`ls ${t_save_fsimage_path}/fsimage*`
# 处理fsimage为可读的csv格式文件
hdfs oiv -i ${t_fsimage_file} -o ${t_save_fsimage_path}/fsimage.csv -p Delimited
# 删除fsimage.csv的首行数据
sed -i -e "1d" ${t_save_fsimage_path}/fsimage.csv
# 创建数据目录
hadoop fs -test -e ${t_save_fsimage_path}/fsimage || hdfs dfs -mkdir -p ${t_save_fsimage_path}/fsimage
# 拷贝fsimage.csv到指定的路径
hdfs dfs -copyFromLocal -f ${t_save_fsimage_path}/fsimage.csv ${t_save_fsimage_path}/fsimage/
The below helped especially when dealing with a large fsimage:
Setting the java heap size:
export HADOOP_OPTS="-Xmx55G"
Using the -t or --temp option to use temporary dir (instead of memory) to cache intermediate result: e.g., hdfs oiv -i fsimage_example -o fsimage_example.csv -p Delimited -delimiter "|" --temp /tmp/fsimage_example
You could either programmatically analyze the fsimage using HFSA lib or HFSA cli tool (depending on your use case).
When I run hadoop streaming like this:
hadoop jar /opt/cloudera/parcels/CDH/lib/hadoop-mapreduce/hadoop-streaming.jar
-Dmapred.reduce.tasks=16
-input foo
-output bar
-mapper "python zot.py"
-reducer gzip
I get 16 files in the output directory which are, alas, corrupt:
$ hadoop fs -get bar/part-00012
$ file part-00012
gzip compressed data, from Unix
$ cat part-00012 | gunzip >/dev/null
gzip: stdin: invalid compressed data--format violated
when I inspect the output of cat part-00012 | gunzip visually, I see parts which look somewhat right and then quite wrong and then gunzip dies.
why is the file corrupt?
PS. I know I can have my data set split into a small number gzip-compressed files using mapred.output.compress=true.
PPS. This is for vw.
You'll want to use output.compress directly in the jobconf settings. No need to send it through gzip.
See my answer to your other question.
I have written up a small bash script that has, essentially, 2 parts.
Part 1: wget
Part 2: hadoop put
Part 1
wget -r -nH –cut-dirs=1 -R index.html -P /home/snoiniM/data/in/ https://www.someWebSite.com/folder/level2 --user=someUserName --password=P#ssword
The files downloaded are saved to /home/snoiniM/data/in/. Immediately after it's done downloading files, I'd tell it to load all files from /home/snoiniM/data/in/ to /place/in/hadoop/downloaded/
hadoop fs -put /home/snoiniM/data/in/ /place/in/hadoop/downloaded/
The problem is, the script does the wget part, but does not do the hadoop put part. However, when I comment out the wget line (nothing else change), it ran the hadoop -put part without errors.
Why can't both parts work together? I can't seem to figure out why. Does anyone know?
As requested, here is my actual code.
Code snippet
#! /bin/bash
temp_dir=/tmp/snoiniM
paypal_date=2013-07-01
hdfsdir=/warehouse/hive/f_paypal_agg #target destination
Log INFO "Downloading Paypal Data..."
wget -r -nH -nd -R index.html -A *$paypal_date.zip -P $temp_dir/paypal https://secure.paypaldata.com/gru/ --user=$paypal_user --password=$paypal_passwd
echo "I'll sleep for 2 seconds. Meanwhile, when you see this you know wget is done."
sleep 2
echo "All done. Proceed to hadoop part"
Log INFO "Clearing down any old/duplicate files or data in HDFS..."
for file in /tmp/snoiniM/paypal/*.zip
do
# hadoop fs -rm -f /warehouse/hive/f_paypal_agg/${file##*/}
hadoop fs -rm -f /warehouse/hive/f_paypal_agg/paypal/${file##*/}
done
Log INFO "Loading all trackpal export zip files to HDFS..."
hadoop fs -put $temp_dir/paypal/ $hdfsdir
Log INFO "------------------------------------------------------------"
Log INFO "Paypal exports for $paypal_date loaded to HDFS."
rm -f $temp_dir/paypal/*$paypal_date.zip
Log INFO "Contents in $temp_dir/paypal cleaned and is ready for next run."
A quick recap: the program runs without errors, but it stops right after wget -- I don't get to see the sleep message. However, if I then comment out wget and run it for the 2nd time, it does sleep part and then on to hadoop put.
Probably a noob question but is there a way to read the contents of file in hdfs besides copying to local and reading thru unix?
So right now what I am doing is:
bin/hadoop dfs -copyToLocal hdfs/path local/path
nano local/path
I am wondering if I can open a file directly to hdfs rather than copying it on local and then opening it.
I believe hadoop fs -cat <file> should do the job.
If the file size is huge (which will be the case most of the times), by doing 'cat' you don't want to blow up your terminal by throwing the entire content of your file. Instead, use piping and get only few lines of the file.
To get the first 10 lines of the file, hadoop fs -cat 'file path' | head -10
To get the last 5 lines of the file, hadoop fs -cat 'file path' | tail -5
If you are using hadoop 2.x , you can use
hdfs dfs -cat <file>
hadoop dfs -cat <filename> or hadoop dfs -cat <outputDirectory>/*
SSH onto your EMR cluster ssh hadoop#emrClusterIpAddress -i yourPrivateKey.ppk
Run this command /usr/lib/spark/bin/spark-shell --conf spark.eventLog.enabled=true --conf spark.eventLog.dir=hdfs://yourEmrClusterIpAddress:8020/eventLogging --class org.apache.spark.examples.SparkPi --master yarn --jars /usr/lib/spark/examples/jars/spark-examples_2.11-2.4.0.jar
List the contents of that directory we just created which should now have a new log file from the run we just did
[hadoop#ip-1-2-3-4 bin]$ hdfs dfs -ls /eventLogging
Found 1 items
-rwxrwx--- 1 hadoop hadoop 53409 2019-05-21 20:56 /eventLogging/application_1557435401803_0106
Now to view the file run hdfs dfs -cat /eventLogging/application_1557435401803_0106
Resources:
https://hadoop.apache.org/docs/r2.7.3/hadoop-project-dist/hadoop-hdfs/HDFSCommands.html
I usually use
$ hdfs dfs -cat <filename> | less
This also helps me to search for words to find what I'm interested in while looking at the contents.
For less context irrelevant purposes like knowing if a particular word exists in a file, or count word occurrences, I use.
$ hdfs dfs -cat <filename> | grep <search_word>
Note: grep also have -C option for contexts, with -A and -B for lines after/before the match.
I was trying to figure out the above commands and that didnt work for me to read the file.
But this did,
cat <filename>
For example,
cat data.txt