-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for date types (hive 1.2.0 and later)
- Loading branch information
Showing
4 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ava/org/openx/data/jsonserde/objectinspector/primitive/JavaStringDateObjectInspector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.openx.data.jsonserde.objectinspector.primitive; | ||
|
||
import org.apache.hadoop.hive.serde2.io.DateWritable; | ||
import org.apache.hadoop.hive.serde2.objectinspector.primitive.AbstractPrimitiveJavaObjectInspector; | ||
import org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableDateObjectInspector; | ||
|
||
import java.sql.Date; | ||
import java.text.SimpleDateFormat; | ||
|
||
/** | ||
* Created by rcongiu on 11/12/15. | ||
*/ | ||
public class JavaStringDateObjectInspector extends AbstractPrimitiveJavaObjectInspector | ||
implements SettableDateObjectInspector { | ||
|
||
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
|
||
public JavaStringDateObjectInspector() { | ||
super(TypeEntryShim.dateType); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public Object set(Object o, java.sql.Date d) { | ||
return d.toString(); | ||
} | ||
|
||
@Override | ||
public Object set(Object o, DateWritable tw) { | ||
return create( tw.get()); | ||
} | ||
|
||
|
||
@Override | ||
public Object create(Date d) { | ||
return d.toString(); | ||
} | ||
|
||
@Override | ||
public DateWritable getPrimitiveWritableObject(Object o) { | ||
if(o == null) return null; | ||
|
||
if(o instanceof String) { | ||
return new DateWritable(parse((String)o)); | ||
} else { | ||
return new DateWritable(((Date) o)); | ||
} | ||
} | ||
|
||
@Override | ||
public Date getPrimitiveJavaObject(Object o) { | ||
if(o instanceof String) { | ||
parse((String)o); | ||
} else { | ||
if (o instanceof Date) return ((Date) o); | ||
} | ||
return null; | ||
} | ||
|
||
protected java.sql.Date parse(String o) { | ||
try { | ||
return new java.sql.Date(sdf.parse(o).getTime()); | ||
} catch(java.text.ParseException e) { | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters