PageRenderTime 53ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorConverter.java

#
Java | 317 lines | 234 code | 33 blank | 50 comment | 17 complexity | 337280907158eaa39e6e5e5c0156603e MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.serde2.objectinspector.primitive;
  19. import org.apache.hadoop.hive.serde2.ByteStream;
  20. import org.apache.hadoop.hive.serde2.lazy.LazyInteger;
  21. import org.apache.hadoop.hive.serde2.lazy.LazyLong;
  22. import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
  23. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter;
  24. import org.apache.hadoop.io.Text;
  25. /**
  26. * PrimitiveObjectInspectorConverter.
  27. *
  28. */
  29. public class PrimitiveObjectInspectorConverter {
  30. /**
  31. * A converter for the byte type.
  32. */
  33. public static class BooleanConverter implements Converter {
  34. PrimitiveObjectInspector inputOI;
  35. SettableBooleanObjectInspector outputOI;
  36. Object r;
  37. public BooleanConverter(PrimitiveObjectInspector inputOI,
  38. SettableBooleanObjectInspector outputOI) {
  39. this.inputOI = inputOI;
  40. this.outputOI = outputOI;
  41. r = outputOI.create(false);
  42. }
  43. @Override
  44. public Object convert(Object input) {
  45. if (input == null) {
  46. return null;
  47. }
  48. try {
  49. return outputOI.set(r, PrimitiveObjectInspectorUtils.getBoolean(input,
  50. inputOI));
  51. } catch (NumberFormatException e) {
  52. return null;
  53. }
  54. }
  55. }
  56. /**
  57. * A converter for the byte type.
  58. */
  59. public static class ByteConverter implements Converter {
  60. PrimitiveObjectInspector inputOI;
  61. SettableByteObjectInspector outputOI;
  62. Object r;
  63. public ByteConverter(PrimitiveObjectInspector inputOI,
  64. SettableByteObjectInspector outputOI) {
  65. this.inputOI = inputOI;
  66. this.outputOI = outputOI;
  67. r = outputOI.create((byte) 0);
  68. }
  69. @Override
  70. public Object convert(Object input) {
  71. if (input == null) {
  72. return null;
  73. }
  74. try {
  75. return outputOI.set(r, PrimitiveObjectInspectorUtils.getByte(input,
  76. inputOI));
  77. } catch (NumberFormatException e) {
  78. return null;
  79. }
  80. }
  81. }
  82. /**
  83. * A converter for the short type.
  84. */
  85. public static class ShortConverter implements Converter {
  86. PrimitiveObjectInspector inputOI;
  87. SettableShortObjectInspector outputOI;
  88. Object r;
  89. public ShortConverter(PrimitiveObjectInspector inputOI,
  90. SettableShortObjectInspector outputOI) {
  91. this.inputOI = inputOI;
  92. this.outputOI = outputOI;
  93. r = outputOI.create((short) 0);
  94. }
  95. @Override
  96. public Object convert(Object input) {
  97. if (input == null) {
  98. return null;
  99. }
  100. try {
  101. return outputOI.set(r, PrimitiveObjectInspectorUtils.getShort(input,
  102. inputOI));
  103. } catch (NumberFormatException e) {
  104. return null;
  105. }
  106. }
  107. }
  108. /**
  109. * A converter for the int type.
  110. */
  111. public static class IntConverter implements Converter {
  112. PrimitiveObjectInspector inputOI;
  113. SettableIntObjectInspector outputOI;
  114. Object r;
  115. public IntConverter(PrimitiveObjectInspector inputOI,
  116. SettableIntObjectInspector outputOI) {
  117. this.inputOI = inputOI;
  118. this.outputOI = outputOI;
  119. r = outputOI.create(0);
  120. }
  121. @Override
  122. public Object convert(Object input) {
  123. if (input == null) {
  124. return null;
  125. }
  126. try {
  127. return outputOI.set(r, PrimitiveObjectInspectorUtils.getInt(input,
  128. inputOI));
  129. } catch (NumberFormatException e) {
  130. return null;
  131. }
  132. }
  133. }
  134. /**
  135. * A converter for the long type.
  136. */
  137. public static class LongConverter implements Converter {
  138. PrimitiveObjectInspector inputOI;
  139. SettableLongObjectInspector outputOI;
  140. Object r;
  141. public LongConverter(PrimitiveObjectInspector inputOI,
  142. SettableLongObjectInspector outputOI) {
  143. this.inputOI = inputOI;
  144. this.outputOI = outputOI;
  145. r = outputOI.create(0);
  146. }
  147. @Override
  148. public Object convert(Object input) {
  149. if (input == null) {
  150. return null;
  151. }
  152. try {
  153. return outputOI.set(r, PrimitiveObjectInspectorUtils.getLong(input,
  154. inputOI));
  155. } catch (NumberFormatException e) {
  156. return null;
  157. }
  158. }
  159. }
  160. /**
  161. * A converter for the float type.
  162. */
  163. public static class FloatConverter implements Converter {
  164. PrimitiveObjectInspector inputOI;
  165. SettableFloatObjectInspector outputOI;
  166. Object r;
  167. public FloatConverter(PrimitiveObjectInspector inputOI,
  168. SettableFloatObjectInspector outputOI) {
  169. this.inputOI = inputOI;
  170. this.outputOI = outputOI;
  171. r = outputOI.create(0);
  172. }
  173. @Override
  174. public Object convert(Object input) {
  175. if (input == null) {
  176. return null;
  177. }
  178. try {
  179. return outputOI.set(r, PrimitiveObjectInspectorUtils.getFloat(input,
  180. inputOI));
  181. } catch (NumberFormatException e) {
  182. return null;
  183. }
  184. }
  185. }
  186. /**
  187. * A converter for the double type.
  188. */
  189. public static class DoubleConverter implements Converter {
  190. PrimitiveObjectInspector inputOI;
  191. SettableDoubleObjectInspector outputOI;
  192. Object r;
  193. public DoubleConverter(PrimitiveObjectInspector inputOI,
  194. SettableDoubleObjectInspector outputOI) {
  195. this.inputOI = inputOI;
  196. this.outputOI = outputOI;
  197. r = outputOI.create(0);
  198. }
  199. @Override
  200. public Object convert(Object input) {
  201. if (input == null) {
  202. return null;
  203. }
  204. try {
  205. return outputOI.set(r, PrimitiveObjectInspectorUtils.getDouble(input,
  206. inputOI));
  207. } catch (NumberFormatException e) {
  208. return null;
  209. }
  210. }
  211. }
  212. /**
  213. * A helper class to convert any primitive to Text.
  214. */
  215. public static class TextConverter implements Converter {
  216. private PrimitiveObjectInspector inputOI;
  217. private Text t = new Text();
  218. private ByteStream.Output out = new ByteStream.Output();
  219. private static byte[] trueBytes = {'T', 'R', 'U', 'E'};
  220. private static byte[] falseBytes = {'F', 'A', 'L', 'S', 'E'};
  221. public TextConverter(PrimitiveObjectInspector inputOI) {
  222. // The output ObjectInspector is writableStringObjectInspector.
  223. this.inputOI = inputOI;
  224. }
  225. public Text convert(Object input) {
  226. if (input == null) {
  227. return null;
  228. }
  229. switch (inputOI.getPrimitiveCategory()) {
  230. case VOID:
  231. return null;
  232. case BOOLEAN:
  233. t.set(((BooleanObjectInspector) inputOI).get(input) ? trueBytes
  234. : falseBytes);
  235. return t;
  236. case BYTE:
  237. out.reset();
  238. LazyInteger.writeUTF8NoException(out, ((ByteObjectInspector) inputOI).get(input));
  239. t.set(out.getData(), 0, out.getCount());
  240. return t;
  241. case SHORT:
  242. out.reset();
  243. LazyInteger.writeUTF8NoException(out, ((ShortObjectInspector) inputOI).get(input));
  244. t.set(out.getData(), 0, out.getCount());
  245. return t;
  246. case INT:
  247. out.reset();
  248. LazyInteger.writeUTF8NoException(out, ((IntObjectInspector) inputOI).get(input));
  249. t.set(out.getData(), 0, out.getCount());
  250. return t;
  251. case LONG:
  252. out.reset();
  253. LazyLong.writeUTF8NoException(out, ((LongObjectInspector) inputOI).get(input));
  254. t.set(out.getData(), 0, out.getCount());
  255. return t;
  256. case FLOAT:
  257. t.set(String.valueOf(((FloatObjectInspector) inputOI).get(input)));
  258. return t;
  259. case DOUBLE:
  260. t.set(String.valueOf(((DoubleObjectInspector) inputOI).get(input)));
  261. return t;
  262. case STRING:
  263. t.set(((StringObjectInspector) inputOI).getPrimitiveJavaObject(input));
  264. return t;
  265. default:
  266. throw new RuntimeException("Hive 2 Internal error: type = " + inputOI.getTypeName());
  267. }
  268. }
  269. }
  270. /**
  271. * A helper class to convert any primitive to String.
  272. */
  273. public static class StringConverter implements Converter {
  274. PrimitiveObjectInspector inputOI;
  275. public StringConverter(PrimitiveObjectInspector inputOI) {
  276. // The output ObjectInspector is writableStringObjectInspector.
  277. this.inputOI = inputOI;
  278. }
  279. @Override
  280. public Object convert(Object input) {
  281. return PrimitiveObjectInspectorUtils.getString(input, inputOI);
  282. }
  283. }
  284. }