explode_json_array

description

表函数,需配合 Lateral View 使用。

展开一个 json 数组。根据数组元素类型,有三种函数名称。分别对应整型、浮点和字符串数组。

语法:

  1. explode_json_array_int(json_str)
  2. explode_json_array_double(json_str)
  3. explode_json_array_string(json_str)

example

原表数据:

  1. mysql> select k1 from example1 order by k1;
  2. +------+
  3. | k1 |
  4. +------+
  5. | 1 |
  6. | 2 |
  7. | 3 |
  8. | 4 |
  9. | 5 |
  10. | 6 |
  11. +------+

Lateral View:

  1. mysql> select k1, e1 from example1 lateral view explode_json_array_int('[]') tmp1 as e1 order by k1, e1;
  2. Empty set
  3. mysql> select k1, e1 from example1 lateral view explode_json_array_int('[1,2,3]') tmp1 as e1 order by k1, e1;
  4. +------+------+
  5. | k1 | e1 |
  6. +------+------+
  7. | 1 | 1 |
  8. | 1 | 2 |
  9. | 1 | 3 |
  10. | 2 | 1 |
  11. | 2 | 2 |
  12. | 2 | 3 |
  13. | 3 | 1 |
  14. | 3 | 2 |
  15. | 3 | 3 |
  16. +------+------+
  17. mysql> select k1, e1 from example1 lateral view explode_json_array_int('[1,"b",3]') tmp1 as e1 order by k1, e1;
  18. +------+------+
  19. | k1 | e1 |
  20. +------+------+
  21. | 1 | 1 |
  22. | 1 | 3 |
  23. | 2 | 1 |
  24. | 2 | 3 |
  25. | 3 | 1 |
  26. | 3 | 3 |
  27. +------+------+
  28. mysql> select k1, e1 from example1 lateral view explode_json_array_int('["a","b","c"]') tmp1 as e1 order by k1, e1;
  29. Empty set
  30. mysql> select k1, e1 from example1 lateral view explode_json_array_int('{"a": 3}') tmp1 as e1 order by k1, e1;
  31. Empty set
  32. mysql> select k1, e1 from example1 lateral view explode_json_array_double('[]') tmp1 as e1 order by k1, e1;
  33. Empty set
  34. mysql> select k1, e1 from example1 lateral view explode_json_array_double('[1,2,3]') tmp1 as e1 order by k1, e1;
  35. Empty set
  36. mysql> select k1, e1 from example1 lateral view explode_json_array_double('[1,"b",3]') tmp1 as e1 order by k1, e1;
  37. Empty set
  38. mysql> select k1, e1 from example1 lateral view explode_json_array_double('[1.0,2.0,3.0]') tmp1 as e1 order by k1, e1;
  39. +------+------+
  40. | k1 | e1 |
  41. +------+------+
  42. | 1 | 1 |
  43. | 1 | 2 |
  44. | 1 | 3 |
  45. | 2 | 1 |
  46. | 2 | 2 |
  47. | 2 | 3 |
  48. | 3 | 1 |
  49. | 3 | 2 |
  50. | 3 | 3 |
  51. +------+------+
  52. mysql> select k1, e1 from example1 lateral view explode_json_array_double('[1,"b",3]') tmp1 as e1 order by k1, e1;
  53. Empty set
  54. mysql> select k1, e1 from example1 lateral view explode_json_array_double('["a","b","c"]') tmp1 as e1 order by k1, e1;
  55. Empty set
  56. mysql> select k1, e1 from example1 lateral view explode_json_array_double('{"a": 3}') tmp1 as e1 order by k1, e1;
  57. Empty set
  58. mysql> select k1, e1 from example1 lateral view explode_json_array_string('[]') tmp1 as e1 order by k1, e1;
  59. Empty set
  60. mysql> select k1, e1 from example1 lateral view explode_json_array_string('[1.0,2.0,3.0]') tmp1 as e1 order by k1, e1;
  61. +------+----------+
  62. | k1 | e1 |
  63. +------+----------+
  64. | 1 | 1.000000 |
  65. | 1 | 2.000000 |
  66. | 1 | 3.000000 |
  67. | 2 | 1.000000 |
  68. | 2 | 2.000000 |
  69. | 2 | 3.000000 |
  70. | 3 | 1.000000 |
  71. | 3 | 2.000000 |
  72. | 3 | 3.000000 |
  73. +------+----------+
  74. mysql> select k1, e1 from example1 lateral view explode_json_array_string('[1,"b",3]') tmp1 as e1 order by k1, e1;
  75. +------+------+
  76. | k1 | e1 |
  77. +------+------+
  78. | 1 | 1 |
  79. | 1 | 3 |
  80. | 1 | b |
  81. | 2 | 1 |
  82. | 2 | 3 |
  83. | 2 | b |
  84. | 3 | 1 |
  85. | 3 | 3 |
  86. | 3 | b |
  87. +------+------+
  88. mysql> select k1, e1 from example1 lateral view explode_json_array_string('["a","b","c"]') tmp1 as e1 order by k1, e1;
  89. +------+------+
  90. | k1 | e1 |
  91. +------+------+
  92. | 1 | a |
  93. | 1 | b |
  94. | 1 | c |
  95. | 2 | a |
  96. | 2 | b |
  97. | 2 | c |
  98. | 3 | a |
  99. | 3 | b |
  100. | 3 | c |
  101. +------+------+
  102. mysql> select k1, e1 from example1 lateral view explode_json_array_string('{"a": 3}') tmp1 as e1 order by k1, e1;
  103. Empty set

keywords

  1. explode_json_array