Review Board 1.7.22


Review for PIG-2641 - add ToJson builtin to Pig

Review Request #9481 - Created Feb. 16, 2013 and updated

Russell Jurney
trunk
PIG-2641
Reviewers
pig
ashutoshc, billgraham, dvryaboy, gates, jcoveney
pig-git
This patch is for JIRA, https://issues.apache.org/jira/browse/PIG-2641 PIG-2641 - which adds a ToJson builtin based on the serialization in JsonStorage.
This works for me locally, and there is a working unit test.

Diff revision 2

This is not the most recent revision of the diff. The latest diff is revision 3. See what's changed.

1 2 3
1 2 3

  1. src/org/apache/pig/builtin/JsonStorage.java: Loading...
  2. src/org/apache/pig/builtin/ToJson.java: Loading...
  3. test/org/apache/pig/test/TestToJson.java: Loading...
  4. test/org/apache/pig/test/data/jsonStorage1.tojson: Loading...
src/org/apache/pig/builtin/JsonStorage.java
Revision 56086ff New Change
[20] 142 lines
[+20] [+] public void putNext(Tuple t) throws IOException {
143
        // Write the beginning of the top level tuple object
143
        // Write the beginning of the top level tuple object
144
        json.writeStartObject();
144
        json.writeStartObject();
145
        
145
        
146
        ResourceFieldSchema[] fields = schema.getFields();
146
        ResourceFieldSchema[] fields = schema.getFields();
147
        for (int i = 0; i < fields.length; i++) {
147
        for (int i = 0; i < fields.length; i++) {
148
            writeField(json, fields[i], t.get(i));
148
            JsonSerializer.writeField(json, fields[i], t.get(i));
149
        }
149
        }
150
        json.writeEndObject();
150
        json.writeEndObject();
151
        json.close();
151
        json.close();
152

    
   
152

   
153
        // Hand a null key and our string to Hadoop
153
        // Hand a null key and our string to Hadoop
154
        try {
154
        try {
155
            writer.write(null, new Text(baos.toByteArray()));
155
            writer.write(null, new Text(baos.toByteArray()));
156
        } catch (InterruptedException ie) {
156
        } catch (InterruptedException ie) {
157
            throw new IOException(ie);
157
            throw new IOException(ie);
158
        }
158
        }
159
    }
159
    }
160

    
   
160

   

    
   
161
    public static class JsonSerializer {

    
   
162

   
161
    @SuppressWarnings("unchecked")
163
        @SuppressWarnings("unchecked")
162
    private void writeField(JsonGenerator json,
164
        public static void writeField(JsonGenerator json,
163
                            ResourceFieldSchema field, 
165
                                ResourceFieldSchema field,
164
                            Object d) throws IOException {
166
                                Object d) throws IOException {
165

    
   
167

   
166
        // If the field is missing or the value is null, write a null
168
            // If the field is missing or the value is null, write a null
167
        if (d == null) {
169
            if (d == null) {
168
            json.writeNullField(field.getName());
170
                json.writeNullField(field.getName());
[+20] [20] 84 lines
[+20] private void writeField(JsonGenerator json, public static void writeField(JsonGenerator json,
253
            }
255
                    }
254
            json.writeEndArray();
256
                    json.writeEndArray();
255
            return;
257
                    return;
256
        }
258
            }
257
    }
259
        }

    
   
260
    }
258

    
   
261

   
259
    public void storeStatistics(ResourceStatistics stats,
262
    public void storeStatistics(ResourceStatistics stats,
260
                                String location,
263
                                String location,
261
                                Job job) throws IOException {
264
                                Job job) throws IOException {
262
        // We don't implement this method
265
        // We don't implement this method
[+20] [20] 15 lines
src/org/apache/pig/builtin/ToJson.java
New File
 
test/org/apache/pig/test/TestToJson.java
New File
 
test/org/apache/pig/test/data/jsonStorage1.tojson
New File
 
  1. src/org/apache/pig/builtin/JsonStorage.java: Loading...
  2. src/org/apache/pig/builtin/ToJson.java: Loading...
  3. test/org/apache/pig/test/TestToJson.java: Loading...
  4. test/org/apache/pig/test/data/jsonStorage1.tojson: Loading...