Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parsing primitive values in a try/catch block #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public Object deserialize(Writable w) throws SerDeException {
} else if (txt.startsWith("[")){
jObj = new JSONArray(txt);
}
} catch (JSONException e) {
} catch (Exception e) {
// If row is not a JSON object, make the whole row NULL
onMalformedJson("Row is not a valid JSON Object - JSONException: "
+ e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import org.apache.hadoop.hive.serde2.objectinspector.primitive.AbstractPrimitiveJavaObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableDoubleObjectInspector;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.hive.serde2.io.DoubleWritable;

/**
*
Expand All @@ -31,20 +31,28 @@ public JavaStringDoubleObjectInspector() {
public Object getPrimitiveWritableObject(Object o) {
if(o == null) return null;

if(o instanceof String) {
return new DoubleWritable(Double.parseDouble((String)o));
} else {
return new DoubleWritable(((Double) o));
try {
if(o instanceof String) {
return new DoubleWritable(Double.parseDouble((String)o));
} else {
return new DoubleWritable(((Double) o));
}
}catch (Exception e){
return new DoubleWritable(0.0);
}
}

@Override
public double get(Object o) {

if(o instanceof String) {
return Double.parseDouble((String)o);
} else {
return (((Double) o));
try {
if(o instanceof String) {
return Double.parseDouble((String)o);
} else {
return (((Double) o));
}
}catch (Exception e){
return 0.0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ public Object getPrimitiveWritableObject(Object o) {

@Override
public float get(Object o) {
if(o instanceof String) {
return Float.parseFloat((String)o);
} else {
return ((Float) o);
try {
if(o instanceof String) {
return Float.parseFloat((String)o);
} else {
return ((Float) o);
}
}catch (Exception e){
return 0.0f;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ public Object getPrimitiveWritableObject(Object o) {

@Override
public int get(Object o) {
if(o instanceof String) {
return ParsePrimitiveUtils.parseInt((String)o);
} else {
return ((Integer) o);
try {
if(o instanceof String) {
return ParsePrimitiveUtils.parseInt((String)o);
} else {
return ((Integer) o);
}
}catch (Exception e){
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ public Object getPrimitiveWritableObject(Object o) {
@Override
public long get(Object o) {

if(o instanceof String) {
return ParsePrimitiveUtils.parseLong((String)o);
} else {
return ((Long) o);
try {
if(o instanceof String) {
return ParsePrimitiveUtils.parseLong((String)o);
} else {
return ((Long) o);
}
}catch (Exception e){
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ public Object getPrimitiveWritableObject(Object o) {
@Override
public short get(Object o) {

if(o instanceof String) {
return ParsePrimitiveUtils.parseShort((String)o);
} else {
return ((Short) o);
try {
if(o instanceof String) {
return ParsePrimitiveUtils.parseShort((String)o);
} else {
return ((Short) o);
}
}catch (Exception e){
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Text getPrimitiveWritableObject(Object o) {

@Override
public String getPrimitiveJavaObject(Object o) {
return (String) o.toString();
return o==null?null:(String) o.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,38 @@ public static byte parseByte(String s) {
}

public static int parseInt(String s) {
if (isHex(s)) {
return Integer.parseInt(s.substring(2), 16);
} else {
return Integer.parseInt(s);
try {
if (isHex(s)) {
return Integer.parseInt(s.substring(2), 16);
} else {
return Integer.parseInt(s);
}
}catch (Exception e){
return 0;
}
}

public static short parseShort(String s) {
if (isHex(s)) {
return Short.parseShort(s.substring(2), 16);
} else {
return Short.parseShort(s);
try {
if (isHex(s)) {
return Short.parseShort(s.substring(2), 16);
} else {
return Short.parseShort(s);
}
} catch (Exception e){
return 0;
}
}

public static long parseLong(String s) {
if (isHex(s)) {
return Long.parseLong(s.substring(2), 16);
} else {
return Long.parseLong(s);
try {
if (isHex(s)) {
return Long.parseLong(s.substring(2), 16);
} else {
return Long.parseLong(s);
}
}catch (Exception e){
return 0;
}
}

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
<cdh4.hive.version>0.10.0-cdh${cdh4.version}</cdh4.hive.version>
<cdh4.hadoop.version>2.0.0-cdh${cdh4.version}</cdh4.hadoop.version>
<!-- cdh5 versions -->
<cdh5.version>5.0.0</cdh5.version>
<cdh5.hive.version>0.12.0-cdh${cdh5.version}</cdh5.hive.version>
<cdh5.hadoop.version>2.3.0-cdh${cdh5.version}</cdh5.hadoop.version>
<cdh5.version>5.3.0</cdh5.version>
<cdh5.hive.version>0.13.1-cdh${cdh5.version}</cdh5.hive.version>
<cdh5.hadoop.version>2.5.0-cdh${cdh5.version}</cdh5.hadoop.version>
</properties>

<profiles>
Expand Down