PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/serde/src/test/org/apache/hadoop/hive/serde2/dynamic_type/TestDynamicSerDe.java

http://github.com/apache/hive
Java | 861 lines | 617 code | 145 blank | 99 comment | 66 complexity | 3007eac268e024d257d654f4bd14c16b MD5 | raw file
Possible License(s): Apache-2.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.dynamic_type;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.LinkedHashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Properties;
  25. import java.util.Random;
  26. import java.util.Map.Entry;
  27. import junit.framework.TestCase;
  28. import org.apache.hadoop.conf.Configuration;
  29. import org.apache.hadoop.hive.serde.serdeConstants;
  30. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  31. import org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol;
  32. import org.apache.hadoop.io.BytesWritable;
  33. /**
  34. * TestDynamicSerDe.
  35. *
  36. */
  37. public class TestDynamicSerDe extends TestCase {
  38. public static HashMap<String, String> makeHashMap(String... params) {
  39. HashMap<String, String> r = new HashMap<String, String>();
  40. for (int i = 0; i < params.length; i += 2) {
  41. r.put(params[i], params[i + 1]);
  42. }
  43. return r;
  44. }
  45. public void testDynamicSerDe() throws Throwable {
  46. try {
  47. // Try to construct an object
  48. ArrayList<String> bye = new ArrayList<String>();
  49. bye.add("firstString");
  50. bye.add("secondString");
  51. HashMap<String, Integer> another = new HashMap<String, Integer>();
  52. another.put("firstKey", 1);
  53. another.put("secondKey", 2);
  54. ArrayList<Object> struct = new ArrayList<Object>();
  55. struct.add(Integer.valueOf(234));
  56. struct.add(bye);
  57. struct.add(another);
  58. struct.add(Integer.valueOf(-234));
  59. struct.add(Double.valueOf(1.0));
  60. struct.add(Double.valueOf(-2.5));
  61. // All protocols
  62. ArrayList<String> protocols = new ArrayList<String>();
  63. ArrayList<Boolean> isBinaries = new ArrayList<Boolean>();
  64. ArrayList<HashMap<String, String>> additionalParams = new ArrayList<HashMap<String, String>>();
  65. protocols
  66. .add(org.apache.hadoop.hive.serde2.thrift.TBinarySortableProtocol.class
  67. .getName());
  68. isBinaries.add(true);
  69. additionalParams.add(makeHashMap("serialization.sort.order", "++++++"));
  70. protocols
  71. .add(org.apache.hadoop.hive.serde2.thrift.TBinarySortableProtocol.class
  72. .getName());
  73. isBinaries.add(true);
  74. additionalParams.add(makeHashMap("serialization.sort.order", "------"));
  75. protocols.add(org.apache.thrift.protocol.TBinaryProtocol.class.getName());
  76. isBinaries.add(true);
  77. additionalParams.add(null);
  78. protocols.add(org.apache.thrift.protocol.TJSONProtocol.class.getName());
  79. isBinaries.add(false);
  80. additionalParams.add(null);
  81. // TSimpleJSONProtocol does not support deserialization.
  82. // protocols.add(org.apache.thrift.protocol.TSimpleJSONProtocol.class.getName());
  83. // isBinaries.add(false);
  84. // additionalParams.add(null);
  85. // TCTLSeparatedProtocol is not done yet.
  86. protocols
  87. .add(org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol.class
  88. .getName());
  89. isBinaries.add(false);
  90. additionalParams.add(null);
  91. System.out.println("input struct = " + struct);
  92. for (int pp = 0; pp < protocols.size(); pp++) {
  93. String protocol = protocols.get(pp);
  94. boolean isBinary = isBinaries.get(pp);
  95. System.out.println("Testing protocol: " + protocol);
  96. Properties schema = new Properties();
  97. schema.setProperty(serdeConstants.SERIALIZATION_FORMAT, protocol);
  98. schema.setProperty(
  99. org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME,
  100. "test");
  101. schema
  102. .setProperty(
  103. serdeConstants.SERIALIZATION_DDL,
  104. "struct test { i32 _hello, list<string> 2bye, map<string,i32> another, i32 nhello, double d, double nd}");
  105. schema.setProperty(serdeConstants.SERIALIZATION_LIB, new DynamicSerDe()
  106. .getClass().toString());
  107. HashMap<String, String> p = additionalParams.get(pp);
  108. if (p != null) {
  109. for (Entry<String, String> e : p.entrySet()) {
  110. schema.setProperty(e.getKey(), e.getValue());
  111. }
  112. }
  113. DynamicSerDe serde = new DynamicSerDe();
  114. serde.initialize(new Configuration(), schema);
  115. // Try getObjectInspector
  116. ObjectInspector oi = serde.getObjectInspector();
  117. System.out.println("TypeName = " + oi.getTypeName());
  118. // Try to serialize
  119. BytesWritable bytes = (BytesWritable) serde.serialize(struct, oi);
  120. System.out.println("bytes =" + hexString(bytes));
  121. if (!isBinary) {
  122. System.out.println("bytes in text ="
  123. + new String(bytes.get(), 0, bytes.getSize()));
  124. }
  125. // Try to deserialize
  126. Object o = serde.deserialize(bytes);
  127. System.out.println("o class = " + o.getClass());
  128. List<?> olist = (List<?>) o;
  129. System.out.println("o size = " + olist.size());
  130. System.out.println("o[0] class = " + olist.get(0).getClass());
  131. System.out.println("o[1] class = " + olist.get(1).getClass());
  132. System.out.println("o[2] class = " + olist.get(2).getClass());
  133. System.out.println("o = " + o);
  134. assertEquals(struct, o);
  135. }
  136. } catch (Throwable e) {
  137. e.printStackTrace();
  138. throw e;
  139. }
  140. }
  141. public String hexString(BytesWritable bytes) {
  142. StringBuilder sb = new StringBuilder();
  143. for (int i = 0; i < bytes.getSize(); i++) {
  144. byte b = bytes.get()[i];
  145. int v = (b < 0 ? 256 + b : b);
  146. sb.append(String.format("x%02x", v));
  147. }
  148. return sb.toString();
  149. }
  150. private void testTBinarySortableProtocol(Object[] structs, String ddl,
  151. boolean ascending) throws Throwable {
  152. int fields = ((List) structs[structs.length - 1]).size();
  153. String order = "";
  154. for (int i = 0; i < fields; i++) {
  155. order = order + (ascending ? "+" : "-");
  156. }
  157. Properties schema = new Properties();
  158. schema.setProperty(serdeConstants.SERIALIZATION_FORMAT,
  159. org.apache.hadoop.hive.serde2.thrift.TBinarySortableProtocol.class
  160. .getName());
  161. schema.setProperty(
  162. org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME, "test");
  163. schema.setProperty(serdeConstants.SERIALIZATION_DDL, ddl);
  164. schema.setProperty(serdeConstants.SERIALIZATION_LIB, DynamicSerDe.class
  165. .getName());
  166. schema.setProperty(serdeConstants.SERIALIZATION_SORT_ORDER, order);
  167. DynamicSerDe serde = new DynamicSerDe();
  168. serde.initialize(new Configuration(), schema);
  169. ObjectInspector oi = serde.getObjectInspector();
  170. // Try to serialize
  171. BytesWritable bytes[] = new BytesWritable[structs.length];
  172. for (int i = 0; i < structs.length; i++) {
  173. bytes[i] = new BytesWritable();
  174. BytesWritable s = (BytesWritable) serde.serialize(structs[i], oi);
  175. bytes[i].set(s);
  176. if (i > 0) {
  177. int compareResult = bytes[i - 1].compareTo(bytes[i]);
  178. if ((compareResult < 0 && !ascending)
  179. || (compareResult > 0 && ascending)) {
  180. System.out.println("Test failed in "
  181. + (ascending ? "ascending" : "descending") + " order.");
  182. System.out.println("serialized data of " + structs[i - 1] + " = "
  183. + hexString(bytes[i - 1]));
  184. System.out.println("serialized data of " + structs[i] + " = "
  185. + hexString(bytes[i]));
  186. fail("Sort order of serialized " + structs[i - 1] + " and "
  187. + structs[i] + " are reversed!");
  188. }
  189. }
  190. }
  191. // Try to deserialize
  192. Object[] deserialized = new Object[structs.length];
  193. for (int i = 0; i < structs.length; i++) {
  194. deserialized[i] = serde.deserialize(bytes[i]);
  195. if (!structs[i].equals(deserialized[i])) {
  196. System.out.println("structs[i] = " + structs[i]);
  197. System.out.println("deserialized[i] = " + deserialized[i]);
  198. System.out.println("serialized[i] = " + hexString(bytes[i]));
  199. assertEquals(structs[i], deserialized[i]);
  200. }
  201. }
  202. }
  203. static int compare(Object a, Object b) {
  204. if (a == null && b == null) {
  205. return 0;
  206. }
  207. if (a == null) {
  208. return -1;
  209. }
  210. if (b == null) {
  211. return 1;
  212. }
  213. if (a instanceof List) {
  214. List la = (List) a;
  215. List lb = (List) b;
  216. assert (la.size() == lb.size());
  217. for (int i = 0; i < la.size(); i++) {
  218. int r = compare(la.get(i), lb.get(i));
  219. if (r != 0) {
  220. return r;
  221. }
  222. }
  223. return 0;
  224. } else if (a instanceof Number) {
  225. Number na = (Number) a;
  226. Number nb = (Number) b;
  227. if (na.doubleValue() < nb.doubleValue()) {
  228. return -1;
  229. }
  230. if (na.doubleValue() > nb.doubleValue()) {
  231. return 1;
  232. }
  233. return 0;
  234. } else if (a instanceof String) {
  235. String sa = (String) a;
  236. String sb = (String) b;
  237. return sa.compareTo(sb);
  238. }
  239. return 0;
  240. }
  241. private void sort(Object[] structs) {
  242. for (int i = 0; i < structs.length; i++) {
  243. for (int j = i + 1; j < structs.length; j++) {
  244. if (compare(structs[i], structs[j]) > 0) {
  245. Object t = structs[i];
  246. structs[i] = structs[j];
  247. structs[j] = t;
  248. }
  249. }
  250. }
  251. }
  252. public void testTBinarySortableProtocol() throws Throwable {
  253. try {
  254. System.out.println("Beginning Test testTBinarySortableProtocol:");
  255. int num = 100;
  256. Random r = new Random(1234);
  257. Object structs[] = new Object[num];
  258. String ddl;
  259. // Test double
  260. for (int i = 0; i < num; i++) {
  261. ArrayList<Object> struct = new ArrayList<Object>();
  262. if (i == 0) {
  263. struct.add(null);
  264. } else {
  265. struct.add(Double.valueOf((r.nextDouble() - 0.5) * 10));
  266. }
  267. structs[i] = struct;
  268. }
  269. sort(structs);
  270. ddl = "struct test { double hello}";
  271. System.out.println("Testing " + ddl);
  272. testTBinarySortableProtocol(structs, ddl, true);
  273. testTBinarySortableProtocol(structs, ddl, false);
  274. // Test integer
  275. for (int i = 0; i < num; i++) {
  276. ArrayList<Object> struct = new ArrayList<Object>();
  277. if (i == 0) {
  278. struct.add(null);
  279. } else {
  280. struct.add((int) ((r.nextDouble() - 0.5) * 1.5 * Integer.MAX_VALUE));
  281. }
  282. structs[i] = struct;
  283. }
  284. sort(structs);
  285. // Null should be smaller than any other value, so put a null at the front
  286. // end
  287. // to test whether that is held.
  288. ((List) structs[0]).set(0, null);
  289. ddl = "struct test { i32 hello}";
  290. System.out.println("Testing " + ddl);
  291. testTBinarySortableProtocol(structs, ddl, true);
  292. testTBinarySortableProtocol(structs, ddl, false);
  293. // Test long
  294. for (int i = 0; i < num; i++) {
  295. ArrayList<Object> struct = new ArrayList<Object>();
  296. if (i == 0) {
  297. struct.add(null);
  298. } else {
  299. struct.add((long) ((r.nextDouble() - 0.5) * 1.5 * Long.MAX_VALUE));
  300. }
  301. structs[i] = struct;
  302. }
  303. sort(structs);
  304. // Null should be smaller than any other value, so put a null at the front
  305. // end
  306. // to test whether that is held.
  307. ((List) structs[0]).set(0, null);
  308. ddl = "struct test { i64 hello}";
  309. System.out.println("Testing " + ddl);
  310. testTBinarySortableProtocol(structs, ddl, true);
  311. testTBinarySortableProtocol(structs, ddl, false);
  312. // Test string
  313. for (int i = 0; i < num; i++) {
  314. ArrayList<Object> struct = new ArrayList<Object>();
  315. if (i == 0) {
  316. struct.add(null);
  317. } else {
  318. struct.add(String.valueOf((r.nextDouble() - 0.5) * 1000));
  319. }
  320. structs[i] = struct;
  321. }
  322. sort(structs);
  323. // Null should be smaller than any other value, so put a null at the front
  324. // end
  325. // to test whether that is held.
  326. ((List) structs[0]).set(0, null);
  327. ddl = "struct test { string hello}";
  328. System.out.println("Testing " + ddl);
  329. testTBinarySortableProtocol(structs, ddl, true);
  330. testTBinarySortableProtocol(structs, ddl, false);
  331. // Test string + double
  332. for (int i = 0; i < num; i++) {
  333. ArrayList<Object> struct = new ArrayList<Object>();
  334. if (i % 9 == 0) {
  335. struct.add(null);
  336. } else {
  337. struct.add("str" + (i / 5));
  338. }
  339. if (i % 7 == 0) {
  340. struct.add(null);
  341. } else {
  342. struct.add(Double.valueOf((r.nextDouble() - 0.5) * 10));
  343. }
  344. structs[i] = struct;
  345. }
  346. sort(structs);
  347. // Null should be smaller than any other value, so put a null at the front
  348. // end
  349. // to test whether that is held.
  350. ((List) structs[0]).set(0, null);
  351. ddl = "struct test { string hello, double another}";
  352. System.out.println("Testing " + ddl);
  353. testTBinarySortableProtocol(structs, ddl, true);
  354. testTBinarySortableProtocol(structs, ddl, false);
  355. System.out.println("Test testTBinarySortableProtocol passed!");
  356. } catch (Throwable e) {
  357. e.printStackTrace();
  358. throw e;
  359. }
  360. }
  361. public void testConfigurableTCTLSeparated() throws Throwable {
  362. try {
  363. // Try to construct an object
  364. ArrayList<String> bye = new ArrayList<String>();
  365. bye.add("firstString");
  366. bye.add("secondString");
  367. LinkedHashMap<String, Integer> another = new LinkedHashMap<String, Integer>();
  368. another.put("firstKey", 1);
  369. another.put("secondKey", 2);
  370. ArrayList<Object> struct = new ArrayList<Object>();
  371. struct.add(Integer.valueOf(234));
  372. struct.add(bye);
  373. struct.add(another);
  374. Properties schema = new Properties();
  375. schema.setProperty(serdeConstants.SERIALIZATION_FORMAT,
  376. org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol.class
  377. .getName());
  378. schema.setProperty(
  379. org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME,
  380. "test");
  381. schema
  382. .setProperty(serdeConstants.SERIALIZATION_DDL,
  383. "struct test { i32 hello, list<string> bye, map<string,i32> another}");
  384. schema.setProperty(serdeConstants.SERIALIZATION_LIB, new DynamicSerDe()
  385. .getClass().toString());
  386. schema.setProperty(serdeConstants.FIELD_DELIM, "9");
  387. schema.setProperty(serdeConstants.COLLECTION_DELIM, "1");
  388. schema.setProperty(serdeConstants.LINE_DELIM, "2");
  389. schema.setProperty(serdeConstants.MAPKEY_DELIM, "4");
  390. DynamicSerDe serde = new DynamicSerDe();
  391. serde.initialize(new Configuration(), schema);
  392. TCTLSeparatedProtocol prot = (TCTLSeparatedProtocol) serde.oprot_;
  393. assertTrue(prot.getPrimarySeparator().equals("\u0009"));
  394. ObjectInspector oi = serde.getObjectInspector();
  395. // Try to serialize
  396. BytesWritable bytes = (BytesWritable) serde.serialize(struct, oi);
  397. hexString(bytes);
  398. String compare = "234" + "\u0009" + "firstString" + "\u0001"
  399. + "secondString" + "\u0009" + "firstKey" + "\u0004" + "1" + "\u0001"
  400. + "secondKey" + "\u0004" + "2";
  401. System.out.println("bytes in text ="
  402. + new String(bytes.get(), 0, bytes.getSize()) + ">");
  403. System.out.println("compare to =" + compare + ">");
  404. assertTrue(compare.equals(new String(bytes.get(), 0, bytes.getSize())));
  405. // Try to deserialize
  406. Object o = serde.deserialize(bytes);
  407. System.out.println("o class = " + o.getClass());
  408. List<?> olist = (List<?>) o;
  409. System.out.println("o size = " + olist.size());
  410. System.out.println("o[0] class = " + olist.get(0).getClass());
  411. System.out.println("o[1] class = " + olist.get(1).getClass());
  412. System.out.println("o[2] class = " + olist.get(2).getClass());
  413. System.out.println("o = " + o);
  414. assertEquals(o, struct);
  415. } catch (Throwable e) {
  416. e.printStackTrace();
  417. throw e;
  418. }
  419. }
  420. /**
  421. * Tests a single null list within a struct with return nulls on.
  422. */
  423. public void testNulls1() throws Throwable {
  424. try {
  425. // Try to construct an object
  426. ArrayList<String> bye = null;
  427. HashMap<String, Integer> another = new HashMap<String, Integer>();
  428. another.put("firstKey", 1);
  429. another.put("secondKey", 2);
  430. ArrayList<Object> struct = new ArrayList<Object>();
  431. struct.add(Integer.valueOf(234));
  432. struct.add(bye);
  433. struct.add(another);
  434. Properties schema = new Properties();
  435. schema.setProperty(serdeConstants.SERIALIZATION_FORMAT,
  436. org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol.class
  437. .getName());
  438. schema.setProperty(
  439. org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME,
  440. "test");
  441. schema
  442. .setProperty(serdeConstants.SERIALIZATION_DDL,
  443. "struct test { i32 hello, list<string> bye, map<string,i32> another}");
  444. schema.setProperty(serdeConstants.SERIALIZATION_LIB, new DynamicSerDe()
  445. .getClass().toString());
  446. schema.setProperty(TCTLSeparatedProtocol.ReturnNullsKey, "true");
  447. DynamicSerDe serde = new DynamicSerDe();
  448. serde.initialize(new Configuration(), schema);
  449. ObjectInspector oi = serde.getObjectInspector();
  450. // Try to serialize
  451. BytesWritable bytes = (BytesWritable) serde.serialize(struct, oi);
  452. hexString(bytes);
  453. // Try to deserialize
  454. Object o = serde.deserialize(bytes);
  455. assertEquals(struct, o);
  456. } catch (Throwable e) {
  457. e.printStackTrace();
  458. throw e;
  459. }
  460. }
  461. /**
  462. * Tests all elements of a struct being null with return nulls on.
  463. */
  464. public void testNulls2() throws Throwable {
  465. try {
  466. // Try to construct an object
  467. ArrayList<String> bye = null;
  468. HashMap<String, Integer> another = null;
  469. ArrayList<Object> struct = new ArrayList<Object>();
  470. struct.add(null);
  471. struct.add(bye);
  472. struct.add(another);
  473. Properties schema = new Properties();
  474. schema.setProperty(serdeConstants.SERIALIZATION_FORMAT,
  475. org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol.class
  476. .getName());
  477. schema.setProperty(
  478. org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME,
  479. "test");
  480. schema
  481. .setProperty(serdeConstants.SERIALIZATION_DDL,
  482. "struct test { i32 hello, list<string> bye, map<string,i32> another}");
  483. schema.setProperty(serdeConstants.SERIALIZATION_LIB, new DynamicSerDe()
  484. .getClass().toString());
  485. schema.setProperty(TCTLSeparatedProtocol.ReturnNullsKey, "true");
  486. DynamicSerDe serde = new DynamicSerDe();
  487. serde.initialize(new Configuration(), schema);
  488. ObjectInspector oi = serde.getObjectInspector();
  489. // Try to serialize
  490. BytesWritable bytes = (BytesWritable) serde.serialize(struct, oi);
  491. hexString(bytes);
  492. // Try to deserialize
  493. Object o = serde.deserialize(bytes);
  494. List<?> olist = (List<?>) o;
  495. assertTrue(olist.size() == 3);
  496. assertEquals(null, olist.get(0));
  497. assertEquals(null, olist.get(1));
  498. assertEquals(null, olist.get(2));
  499. // assertEquals(o, struct); Cannot do this because types of null lists are
  500. // wrong.
  501. } catch (Throwable e) {
  502. e.printStackTrace();
  503. throw e;
  504. }
  505. }
  506. /**
  507. * Tests map and list being empty with return nulls on.
  508. */
  509. public void testNulls3() throws Throwable {
  510. try {
  511. // Try to construct an object
  512. ArrayList<String> bye = new ArrayList<String>();
  513. HashMap<String, Integer> another = null;
  514. ArrayList<Object> struct = new ArrayList<Object>();
  515. struct.add(null);
  516. struct.add(bye);
  517. struct.add(another);
  518. Properties schema = new Properties();
  519. schema.setProperty(serdeConstants.SERIALIZATION_FORMAT,
  520. org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol.class
  521. .getName());
  522. schema.setProperty(
  523. org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME,
  524. "test");
  525. schema
  526. .setProperty(serdeConstants.SERIALIZATION_DDL,
  527. "struct test { i32 hello, list<string> bye, map<string,i32> another}");
  528. schema.setProperty(serdeConstants.SERIALIZATION_LIB, new DynamicSerDe()
  529. .getClass().toString());
  530. schema.setProperty(TCTLSeparatedProtocol.ReturnNullsKey, "true");
  531. DynamicSerDe serde = new DynamicSerDe();
  532. serde.initialize(new Configuration(), schema);
  533. ObjectInspector oi = serde.getObjectInspector();
  534. // Try to serialize
  535. BytesWritable bytes = (BytesWritable) serde.serialize(struct, oi);
  536. hexString(bytes);
  537. // Try to deserialize
  538. Object o = serde.deserialize(bytes);
  539. List<?> olist = (List<?>) o;
  540. assertTrue(olist.size() == 3);
  541. assertEquals(null, olist.get(0));
  542. assertEquals(0, ((List<?>) olist.get(1)).size());
  543. assertEquals(null, olist.get(2));
  544. // assertEquals(o, struct); Cannot do this because types of null lists are
  545. // wrong.
  546. } catch (Throwable e) {
  547. e.printStackTrace();
  548. throw e;
  549. }
  550. }
  551. /**
  552. * Tests map and list null/empty with return nulls *off*.
  553. */
  554. public void testNulls4() throws Throwable {
  555. try {
  556. // Try to construct an object
  557. ArrayList<String> bye = new ArrayList<String>();
  558. HashMap<String, Integer> another = null;
  559. ArrayList<Object> struct = new ArrayList<Object>();
  560. struct.add(null);
  561. struct.add(bye);
  562. struct.add(another);
  563. Properties schema = new Properties();
  564. schema.setProperty(serdeConstants.SERIALIZATION_FORMAT,
  565. org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol.class
  566. .getName());
  567. schema.setProperty(
  568. org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME,
  569. "test");
  570. schema
  571. .setProperty(serdeConstants.SERIALIZATION_DDL,
  572. "struct test { i32 hello, list<string> bye, map<string,i32> another}");
  573. schema.setProperty(serdeConstants.SERIALIZATION_LIB, new DynamicSerDe()
  574. .getClass().toString());
  575. schema.setProperty(TCTLSeparatedProtocol.ReturnNullsKey, "false");
  576. DynamicSerDe serde = new DynamicSerDe();
  577. serde.initialize(new Configuration(), schema);
  578. ObjectInspector oi = serde.getObjectInspector();
  579. // Try to serialize
  580. BytesWritable bytes = (BytesWritable) serde.serialize(struct, oi);
  581. hexString(bytes);
  582. // Try to deserialize
  583. Object o = serde.deserialize(bytes);
  584. List<?> olist = (List<?>) o;
  585. assertTrue(olist.size() == 3);
  586. assertEquals(new Integer(0), (Integer) olist.get(0));
  587. List<?> num1 = (List<?>) olist.get(1);
  588. assertTrue(num1.size() == 0);
  589. Map<?, ?> num2 = (Map<?, ?>) olist.get(2);
  590. assertTrue(num2.size() == 0);
  591. // assertEquals(o, struct); Cannot do this because types of null lists are
  592. // wrong.
  593. } catch (Throwable e) {
  594. e.printStackTrace();
  595. throw e;
  596. }
  597. }
  598. /**
  599. * Tests map and list null/empty with return nulls *off*.
  600. */
  601. public void testStructsinStructs() throws Throwable {
  602. try {
  603. Properties schema = new Properties();
  604. // schema.setProperty(serdeConstants.SERIALIZATION_FORMAT,
  605. // org.apache.thrift.protocol.TJSONProtocol.class.getName());
  606. schema.setProperty(serdeConstants.SERIALIZATION_FORMAT,
  607. org.apache.thrift.protocol.TBinaryProtocol.class.getName());
  608. schema.setProperty(
  609. org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME,
  610. "test");
  611. schema.setProperty(
  612. serdeConstants.SERIALIZATION_DDL,
  613. "struct inner { i32 field1, string field2 },struct test {inner foo, i32 hello, list<string> bye, map<string,i32> another}");
  614. schema.setProperty(serdeConstants.SERIALIZATION_LIB, new DynamicSerDe()
  615. .getClass().toString());
  616. //
  617. // construct object of above type
  618. //
  619. // construct the inner struct
  620. ArrayList<Object> innerStruct = new ArrayList<Object>();
  621. innerStruct.add(new Integer(22));
  622. innerStruct.add(new String("hello world"));
  623. // construct outer struct
  624. ArrayList<String> bye = new ArrayList<String>();
  625. bye.add("firstString");
  626. bye.add("secondString");
  627. HashMap<String, Integer> another = new HashMap<String, Integer>();
  628. another.put("firstKey", 1);
  629. another.put("secondKey", 2);
  630. ArrayList<Object> struct = new ArrayList<Object>();
  631. struct.add(innerStruct);
  632. struct.add(Integer.valueOf(234));
  633. struct.add(bye);
  634. struct.add(another);
  635. DynamicSerDe serde = new DynamicSerDe();
  636. serde.initialize(new Configuration(), schema);
  637. ObjectInspector oi = serde.getObjectInspector();
  638. // Try to serialize
  639. BytesWritable bytes = (BytesWritable) serde.serialize(struct, oi);
  640. // Try to deserialize
  641. Object o = serde.deserialize(bytes);
  642. List<?> olist = (List<?>) o;
  643. assertEquals(4, olist.size());
  644. assertEquals(innerStruct, olist.get(0));
  645. assertEquals(new Integer(234), olist.get(1));
  646. assertEquals(bye, olist.get(2));
  647. assertEquals(another, olist.get(3));
  648. } catch (Throwable e) {
  649. e.printStackTrace();
  650. throw e;
  651. }
  652. }
  653. public void testSkip() throws Throwable {
  654. try {
  655. // Try to construct an object
  656. ArrayList<String> bye = new ArrayList<String>();
  657. bye.add("firstString");
  658. bye.add("secondString");
  659. LinkedHashMap<String, Integer> another = new LinkedHashMap<String, Integer>();
  660. another.put("firstKey", 1);
  661. another.put("secondKey", 2);
  662. ArrayList<Object> struct = new ArrayList<Object>();
  663. struct.add(Integer.valueOf(234));
  664. struct.add(bye);
  665. struct.add(another);
  666. Properties schema = new Properties();
  667. schema.setProperty(serdeConstants.SERIALIZATION_FORMAT,
  668. org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol.class
  669. .getName());
  670. schema.setProperty(
  671. org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME,
  672. "test");
  673. schema
  674. .setProperty(serdeConstants.SERIALIZATION_DDL,
  675. "struct test { i32 hello, list<string> bye, map<string,i32> another}");
  676. schema.setProperty(serdeConstants.SERIALIZATION_LIB, new DynamicSerDe()
  677. .getClass().toString());
  678. schema.setProperty(serdeConstants.FIELD_DELIM, "9");
  679. schema.setProperty(serdeConstants.COLLECTION_DELIM, "1");
  680. schema.setProperty(serdeConstants.LINE_DELIM, "2");
  681. schema.setProperty(serdeConstants.MAPKEY_DELIM, "4");
  682. DynamicSerDe serde = new DynamicSerDe();
  683. serde.initialize(new Configuration(), schema);
  684. TCTLSeparatedProtocol prot = (TCTLSeparatedProtocol) serde.oprot_;
  685. assertTrue(prot.getPrimarySeparator().equals("\u0009"));
  686. ObjectInspector oi = serde.getObjectInspector();
  687. // Try to serialize
  688. BytesWritable bytes = (BytesWritable) serde.serialize(struct, oi);
  689. hexString(bytes);
  690. String compare = "234" + "\u0009" + "firstString" + "\u0001"
  691. + "secondString" + "\u0009" + "firstKey" + "\u0004" + "1" + "\u0001"
  692. + "secondKey" + "\u0004" + "2";
  693. System.out.println("bytes in text ="
  694. + new String(bytes.get(), 0, bytes.getSize()) + ">");
  695. System.out.println("compare to =" + compare + ">");
  696. assertTrue(compare.equals(new String(bytes.get(), 0, bytes.getSize())));
  697. schema
  698. .setProperty(serdeConstants.SERIALIZATION_DDL,
  699. "struct test { i32 hello, skip list<string> bye, map<string,i32> another}");
  700. serde.initialize(new Configuration(), schema);
  701. // Try to deserialize
  702. Object o = serde.deserialize(bytes);
  703. System.out.println("o class = " + o.getClass());
  704. List<?> olist = (List<?>) o;
  705. System.out.println("o size = " + olist.size());
  706. System.out.println("o = " + o);
  707. assertEquals(null, olist.get(1));
  708. // set the skipped field to null
  709. struct.set(1, null);
  710. assertEquals(o, struct);
  711. } catch (Throwable e) {
  712. e.printStackTrace();
  713. throw e;
  714. }
  715. }
  716. }