You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pig.apache.org by "Jonathan Holloway (JIRA)" <ji...@apache.org> on 2011/03/11 13:35:59 UTC

[jira] Updated: (PIG-1896) CastUtils - Converting Pig DataTypes to Java Data Types

     [ https://issues.apache.org/jira/browse/PIG-1896?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Jonathan Holloway updated PIG-1896:
-----------------------------------

    Status: Patch Available  (was: Open)

### Eclipse Workspace Patch 1.0
#P pig
Index: src/org/apache/pig/impl/util/CastUtils.java
===================================================================
--- src/org/apache/pig/impl/util/CastUtils.java	(revision 1080348)
+++ src/org/apache/pig/impl/util/CastUtils.java	(working copy)
@@ -21,6 +21,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.pig.PigWarning;
+import org.apache.pig.data.DataType;
 
 public class CastUtils {
 
@@ -154,5 +155,31 @@
 			}
 		}
 	}
+	
+	 /**
+     * Converts a value to the correct Java data type based on the byte value for {@link DataType}.
+     * @param dataType is the datatype byte value to convert the value to
+     * @param value is the text value to convert 
+     * @return the corresponding datatype, defaulting to a {@link java.lang.String} for other types.
+     */
+    public static Object convert(String value, byte dataType) {
+        
+        switch (dataType) {
+            case DataType.BOOLEAN:
+                return new Boolean(value);
+            case DataType.BYTE:
+                return new Byte(value);
+            case DataType.INTEGER:
+                return CastUtils.stringToInteger(value);
+            case DataType.LONG:
+                return CastUtils.stringToLong(value);
+            case DataType.FLOAT:
+                return CastUtils.stringToFloat(value);
+            case DataType.DOUBLE:
+                return CastUtils.stringToDouble(value);
+            default:
+                return value;
+        }
+    }
 
 }

> CastUtils - Converting Pig DataTypes to Java Data Types
> -------------------------------------------------------
>
>                 Key: PIG-1896
>                 URL: https://issues.apache.org/jira/browse/PIG-1896
>             Project: Pig
>          Issue Type: Improvement
>          Components: impl
>    Affects Versions: 0.8.0
>            Reporter: Jonathan Holloway
>            Priority: Minor
>
> I ran into an issue with parsing log lines whereby I had to generate a schema in a user defined function.  Part of that involved converting various values into their associated data types, but I couldn't see a way to do it via Pig.  
> Enclosed is a patch to convert org.apache.pig.data.DataType into an associated Java datatype falling back to a String by default - should go into CastUtils.  Any comments/criticism, please let me know.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira