Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python script for avro conversion using Hadoop Streaming

Tags:

python

hadoop

I have 10 GB of input file which i am trying to convert to avro using python hadoop streaming, the job is successfull but i canot read the output using the avro reader.

It is giving 'utf8' codec can't decode byte 0xb4 in position 13924: invalid start byte.

The issue here is am using the stdout for mapper output for hadoop streaming, if i use the filename and use the script locally the avro output is readable.

Any ideas, how to fix this ? I think issue is around handling key/value in streaming....

hadoop jar /opt/cloudera/parcels/CDH/lib/hadoop-0.20-mapreduce/contrib/streaming/hadoop-streaming.jar \
                      -input "xxx.txt" \
                      -mapper "/opt/anaconda/anaconda21/bin/python mapper.py x.avsc"  \
                      -reducer NONE \
                      -output "xxxxx" -file "mapper.py" \
                      -lazyOutput \
                      -file "x.avsc"

The mapper script is

import sys
import re
import os
from avro import schema, datafile
import avro.io as io
import StringIO

schema_str = open("xxxxx.avsc", 'r').read()
SCHEMA = schema.parse(schema_str)
rec_writer = io.DatumWriter(SCHEMA)
df_writer  = datafile.DataFileWriter(sys.stdout, rec_writer, SCHEMA,)
header = []
for field in SCHEMA.fields:
        header.append(field.name)

for line in sys.stdin:
    fields = line.rstrip().split("\x01")
    data   = dict(zip(header, fields))
    try:
        df_writer.append(data)
    except Exception, e:
        print "failed with data: %s" % str(data)
        print str(e)
df_writer.close()
like image 730
Ranga Vure Avatar asked Aug 12 '26 07:08

Ranga Vure


1 Answers

Finally could fix this issue. use the output format class, and leave the avro binary conversion to this. And in streaming mapper, just emit the json record.

hadoop jar /opt/cloudera/parcels/CDH/lib/hadoop-0.20-mapreduce/contrib/streaming/hadoop-streaming.jar \
              -libjars avro-json-1.2.jar \
              -jobconf output.schema.url=hdfs:///x.avsc \
              -input "xxxxx" \
              -mapper "/opt/anaconda/anaconda21/bin/python mapper.py x.avsc"  \
              -reducer NONE \
              -output "/xxxxx"  \
              -outputformat com.cloudera.science.avro.streaming.AvroAsJSONOutputFormat \
              -lazyOutput \
              -file "mapper.py" \
              -file "x.avsc"

And here is mapper.py

import sys
from avro import schema
import json

schema_str = open("xxxxx.avsc", 'r').read()
SCHEMA = schema.parse(schema_str)

header = []
for field in SCHEMA.fields:
    header.append(field.name)

for line in sys.stdin:
    fields = line.rstrip().split("\x01")
    data   = dict(zip(header, fields))
    try:
       print >> sys.stdout, json.dumps(data, encoding='ISO-8859-1')
    except Exception, e:
       print "failed with data: %s" % str(data)
       print str(e)
like image 71
Ranga Vure Avatar answered Aug 13 '26 20:08

Ranga Vure