org.apache.pig.builtin
Class JsonStorage

java.lang.Object
  extended by org.apache.pig.StoreFunc
      extended by org.apache.pig.builtin.JsonStorage
All Implemented Interfaces:
StoreFuncInterface, StoreMetadata

public class JsonStorage
extends StoreFunc
implements StoreMetadata

A JSON Pig store function. Each Pig tuple is stored on one line (as one value for TextOutputFormat) so that it can be read easily using TextInputFormat. Pig tuples are mapped to JSON objects. Pig bags are mapped to JSON arrays. Pig maps are also mapped to JSON objects. Maps are assumed to be string to string. A schema is stored in a side file to deal with mapping between JSON and Pig types. The schema file share the same format as the one we use in PigStorage.


Field Summary
protected  ResourceSchema schema
           
protected  org.apache.hadoop.mapreduce.RecordWriter writer
           
 
Constructor Summary
JsonStorage()
           
 
Method Summary
 void checkSchema(ResourceSchema s)
          Set the schema for data to be stored.
 org.apache.hadoop.mapreduce.OutputFormat getOutputFormat()
          Return the OutputFormat associated with StoreFunc.
 void prepareToWrite(org.apache.hadoop.mapreduce.RecordWriter writer)
          Initialize StoreFunc to write data.
 void putNext(Tuple t)
          Write a tuple to the data store.
 void setStoreFuncUDFContextSignature(String signature)
          This method will be called by Pig both in the front end and back end to pass a unique signature to the StoreFunc which it can use to store information in the UDFContext which it needs to store between various method invocations in the front end and back end.
 void setStoreLocation(String location, org.apache.hadoop.mapreduce.Job job)
          Communicate to the storer the location where the data needs to be stored.
 void storeSchema(ResourceSchema schema, String location, org.apache.hadoop.mapreduce.Job job)
          Store schema of the data being written
 void storeStatistics(ResourceStatistics stats, String location, org.apache.hadoop.mapreduce.Job job)
          Store statistics about the data being written.
 
Methods inherited from class org.apache.pig.StoreFunc
cleanupOnFailure, cleanupOnFailureImpl, cleanupOnSuccess, relToAbsPathForStoreLocation, warn
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

writer

protected org.apache.hadoop.mapreduce.RecordWriter writer

schema

protected ResourceSchema schema
Constructor Detail

JsonStorage

public JsonStorage()
Method Detail

getOutputFormat

public org.apache.hadoop.mapreduce.OutputFormat getOutputFormat()
                                                         throws IOException
Description copied from class: StoreFunc
Return the OutputFormat associated with StoreFunc. This will be called on the front end during planning and on the backend during execution.

Specified by:
getOutputFormat in interface StoreFuncInterface
Specified by:
getOutputFormat in class StoreFunc
Returns:
the OutputFormat associated with StoreFunc
Throws:
IOException - if an exception occurs while constructing the OutputFormat

setStoreLocation

public void setStoreLocation(String location,
                             org.apache.hadoop.mapreduce.Job job)
                      throws IOException
Description copied from class: StoreFunc
Communicate to the storer the location where the data needs to be stored. The location string passed to the StoreFunc here is the return value of StoreFunc.relToAbsPathForStoreLocation(String, Path) This method will be called in the frontend and backend multiple times. Implementations should bear in mind that this method is called multiple times and should ensure there are no inconsistent side effects due to the multiple calls. StoreFunc.checkSchema(ResourceSchema) will be called before any call to StoreFunc.setStoreLocation(String, Job).

Specified by:
setStoreLocation in interface StoreFuncInterface
Specified by:
setStoreLocation in class StoreFunc
Parameters:
location - Location returned by StoreFunc.relToAbsPathForStoreLocation(String, Path)
job - The Job object
Throws:
IOException - if the location is not valid.

setStoreFuncUDFContextSignature

public void setStoreFuncUDFContextSignature(String signature)
Description copied from class: StoreFunc
This method will be called by Pig both in the front end and back end to pass a unique signature to the StoreFunc which it can use to store information in the UDFContext which it needs to store between various method invocations in the front end and back end. This method will be called before other methods in StoreFunc. This is necessary because in a Pig Latin script with multiple stores, the different instances of store functions need to be able to find their (and only their) data in the UDFContext object. The default implementation is a no-op.

Specified by:
setStoreFuncUDFContextSignature in interface StoreFuncInterface
Overrides:
setStoreFuncUDFContextSignature in class StoreFunc
Parameters:
signature - a unique signature to identify this StoreFunc

checkSchema

public void checkSchema(ResourceSchema s)
                 throws IOException
Description copied from class: StoreFunc
Set the schema for data to be stored. This will be called on the front end during planning if the store is associated with a schema. A Store function should implement this function to check that a given schema is acceptable to it. For example, it can check that the correct partition keys are included; a storage function to be written directly to an OutputFormat can make sure the schema will translate in a well defined way. Default implementation is a no-op.

Specified by:
checkSchema in interface StoreFuncInterface
Overrides:
checkSchema in class StoreFunc
Parameters:
s - to be checked
Throws:
IOException - if this schema is not acceptable. It should include a detailed error message indicating what is wrong with the schema.

prepareToWrite

public void prepareToWrite(org.apache.hadoop.mapreduce.RecordWriter writer)
                    throws IOException
Description copied from class: StoreFunc
Initialize StoreFunc to write data. This will be called during execution on the backend before the call to putNext.

Specified by:
prepareToWrite in interface StoreFuncInterface
Specified by:
prepareToWrite in class StoreFunc
Parameters:
writer - RecordWriter to use.
Throws:
IOException - if an exception occurs during initialization

putNext

public void putNext(Tuple t)
             throws IOException
Description copied from class: StoreFunc
Write a tuple to the data store.

Specified by:
putNext in interface StoreFuncInterface
Specified by:
putNext in class StoreFunc
Parameters:
t - the tuple to store.
Throws:
IOException - if an exception occurs during the write

storeStatistics

public void storeStatistics(ResourceStatistics stats,
                            String location,
                            org.apache.hadoop.mapreduce.Job job)
                     throws IOException
Description copied from interface: StoreMetadata
Store statistics about the data being written.

Specified by:
storeStatistics in interface StoreMetadata
Parameters:
stats - statistics to be recorded
location - Location as returned by LoadFunc.relativeToAbsolutePath(String, org.apache.hadoop.fs.Path)
job - The Job object - this should be used only to obtain cluster properties through JobContext.getConfiguration() and not to set/query any runtime job information.
Throws:
IOException

storeSchema

public void storeSchema(ResourceSchema schema,
                        String location,
                        org.apache.hadoop.mapreduce.Job job)
                 throws IOException
Description copied from interface: StoreMetadata
Store schema of the data being written

Specified by:
storeSchema in interface StoreMetadata
Parameters:
schema - Schema to be recorded
location - Location as returned by LoadFunc.relativeToAbsolutePath(String, org.apache.hadoop.fs.Path)
job - The Job object - this should be used only to obtain cluster properties through JobContext.getConfiguration() and not to set/query any runtime job information.
Throws:
IOException


Copyright © 2007-2012 The Apache Software Foundation