Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.pig.impl.logicalLayer.schema;
 
 import java.util.List;
 import java.util.Set;
 
A utility class for simplify the schema creation, especially for bag and tuple schema. Currently, it only support simple schema creation, nested tuple and bag is not supported
 
 
 public class SchemaUtil {
 
     private static Set<ByteSUPPORTED_TYPE_SET;
 
     static {
          = new HashSet<Byte>();
 
         .add(.);
     }

    
Create a new tuple schema according the tuple name and two list: names of fields, types of fields

Parameters:
tupleName
fieldNames
dataTypes
Returns:
tuple schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
 
     public static Schema newTupleSchema(String tupleName,
             List<StringfieldNamesList<BytedataTypes)
             throws FrontendException {
         checkParameters(fieldNamesdataTypes);
 
         List<Schema.FieldSchematokenSchemas = new ArrayList<Schema.FieldSchema>();
         for (int i = 0; i < fieldNames.size(); ++i) {
             String name = fieldNames.get(i);
             Byte type = dataTypes.get(i);
             tokenSchemas.add(new Schema.FieldSchema(nametype));
         }
 
         Schema tupleSchema = new Schema(tokenSchemas);
         Schema.FieldSchema tupleField = new Schema.FieldSchema(tupleName,
                 tupleSchema);
 
         return new Schema(tupleField);
     }

    
Create a new tuple schema according the tuple name and two arrays: names of fields, types of fields

Parameters:
tupleName
fieldNames
dataTypes
Returns:
tuple schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
 
     public static Schema newTupleSchema(String tupleNameString[] fieldNames,
             Byte[] dataTypesthrows FrontendException {
        return newTupleSchema(tupleName, Arrays.asList(fieldNames), Arrays
                .asList(dataTypes));
    }

    
Create a new tuple schema according the two list: names of fields, types of fields, the default tuple name is t.

Parameters:
fieldNames
dataTypes
Returns:
tuple schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
    public static Schema newTupleSchema(List<StringfieldNames,
            List<BytedataTypesthrows FrontendException {
        return newTupleSchema("t"fieldNamesdataTypes);
    }

    
Create a new tuple schema according one list: types of fields, the default names of fields are f0,f1,f2..., and the tuple name is t.

Parameters:
dataTypes
Returns:
tuple schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
    public static Schema newTupleSchema(List<BytedataTypes)
            throws FrontendException {
        List<Stringnames = newNames(dataTypes.size());
        return newTupleSchema("t"namesdataTypes);
    }

    
Create a new tuple schema according the two arrays: names of fields, types of fields, the default tuple name is t.

Parameters:
names
dataTypes
Returns:
tuple schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
    public static Schema newTupleSchema(String[] namesByte[] dataTypes)
            throws FrontendException {
        return newTupleSchema("t", Arrays.asList(names), Arrays
                .asList(dataTypes));
    }

    
Create a new tuple schema according one array: types of fields, the default names of fields are f0,f1,f2..., and the tuple name is t.

Parameters:
dataTypes
Returns:
tuple schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
    public static Schema newTupleSchema(Byte[] dataTypes)
            throws FrontendException {
        return newTupleSchema(Arrays.asList(dataTypes));
    }
    private static List<StringnewNames(int size) {
        List<Stringnames = new ArrayList<String>();
        for (int i = 0; i < size; ++i) {
            names.add("f" + i);
        }
        return names;
    }

    
Create a bag schema according the bag name,tuple name and two list: name of fields, type of fields

Parameters:
bagName
tupleName
fieldNames
dataTypes
Returns:
bag schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
    public static Schema newBagSchema(String bagNameString tupleName,
            List<StringfieldNamesList<BytedataTypes)
            throws FrontendException {
        checkParameters(fieldNamesdataTypes);
        Schema tupleSchema = newTupleSchema(tupleNamefieldNamesdataTypes);
        Schema.FieldSchema bagField = new Schema.FieldSchema(bagName,
                tupleSchema.);
        return new Schema(bagField);
    }
    public static Schema newBagSchema(String bagNameString tupleName,
            String[] fieldNamesByte[] dataTypesthrows FrontendException {
        return newBagSchema(bagNametupleName, Arrays.asList(fieldNames),
                Arrays.asList(dataTypes));
    }

    
Create a bag schema according two list: name of fields, type of fields, and the default bag name is b, the default tuple name is t.

Parameters:
names
dataTypes
Returns:
bag schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
    public static Schema newBagSchema(List<StringnamesList<BytedataTypes)
            throws FrontendException {
        checkParameters(namesdataTypes);
        Schema tupleSchema = newTupleSchema(namesdataTypes);
        Schema.FieldSchema bagField = new Schema.FieldSchema("b"tupleSchema,
                .);
        return new Schema(bagField);
    }

    
Create a new tuple schema according one list: types of fields, the default names of fields are f0,f1,f2..., and the tuple is t, the bag name is b.

Parameters:
dataTypes
Returns:
bag schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
    public static Schema newBagSchema(List<BytedataTypes)
            throws FrontendException {
        List<Stringnames = newNames(dataTypes.size());
        return newBagSchema(namesdataTypes);
    }

    
Create a new tuple schema according two arrays: names of field,types of fields. The default tuple name is t, and the bag is b.

Parameters:
names
dataTypes
Returns:
bag schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
    public static Schema newBagSchema(String[] namesByte[] dataTypes)
            throws FrontendException {
        return newBagSchema(Arrays.asList(names), Arrays.asList(dataTypes));
    }

    
Create a new tuple schema according one array: the type of fields, the tuple name is t, and the bag name is b.

Parameters:
dataTypes
Returns:
bag schema
Throws:
org.apache.pig.impl.logicalLayer.FrontendException
    public static Schema newBagSchema(Byte[] dataTypes)
            throws FrontendException {
        return newBagSchema(Arrays.asList(dataTypes));
    }
    private static void checkDataTypes(List<BytedataTypes)
            throws FrontendException {
        for (Byte type : dataTypes) {
            if (!.contains(type)) {
                throw new FrontendException(
                        "Currently pig do not support this kind of type using Schema:"
                                + DataType.findTypeName(type)
                                + ". You can write shema by yourself.");
            }
        }
    }
    private static void checkParameters(List<StringnamesList<BytedataTypes)
            throws FrontendException {
        // TODO Auto-generated method stub
        checkDataTypes(dataTypes);
        if (names.size() != dataTypes.size()) {
            throw new FrontendException(
                    "The number of names is not equal to the number of dataTypes");
        }
    }
New to GrepCode? Check out our FAQ X