This article provides a way to convert a JSON string into a hive table using Scala, because it is relatively simple, just stick to the code and not explain. Questions can be discussed in the message
Package com.gabry.hive
Import Org.json4s._
Import Org.json4s.native.jsonmethods._
Import Scala.io.Source
Class json2hive{/** * Sealed abstract class Jvalue *case object jnothing extends Jvalue//' zero ' for Jvalue *c ASE Object Jnull extends Jvalue *case class jstring (s:string) extends Jvalue *case class jdouble (num:double) exten DS Jvalue *case class Jdecimal (num:bigdecimal) extends Jvalue *case class JInt (num:bigint) extends Jvalue *case Class Jbool (Value:boolean) extends Jvalue *case class Jobject (Obj:list[jfield]) extends Jvalue *case class Jarray (Arr:list[jvalue]) extends jvalue *type Jfield = (String, jvalue) *create table student_test (id INT, info struct< ; Name:string,age:int >) *jsonstring:{"People_type": 1, "people": {"person_id": 5, "Test_count": 5, "para": {"name": " Jack "," Age ": 6}}} */Private def fielddelimiter (level:int) = if (level = = 2)" Else ":" Private def Decodejson (JV: Any,level:int,hql:stringbuilder): Unit = {JV match {case js:jstring = Hql.append (FieldDelimiter (level) + "Str ING, ") Case jdo:jdouble = = Hql.append (level) + "double,") Case jde:jdecimal = Hql.append (FieldDelimiter (level) + "decimal" , ") Case ji:jint = Hql.append (FieldDelimiter (level) +" bigint, ") Case jb:jbool = Hql.append (fielddelimiter (level) + ' int, ') Case jf:jfield=> hql.append (jf._1) Decodejson (jf._2,level+1,hql) Case Ja:jarray = = Hql.append (level + "struct<") Ja.arr.foreach (Decodejson (_,LEVEL+1,HQL)) Hql.append ("& gt; ") Case Jo:jobject=> if (level!=0) hql.append ("struct<") Jo.obj.foreach (Decodejson (_,LEVEL+1,HQL)) if (Hql.endswith (")") Hql.deletecharat (Hql.length-1) if (level!=0) Hql.append ("A;,") Case JNu Ll=> Hql.append (FieldDelimiter (level) + "string,") Case _ =>println (JV)}} def tohive (Jsonstr:string,tablen ame:string): String = {val Jsonobj = Parse (jsonstr) val hql = new StringBuilder () Decodejson (JSONOBJ,0,HQL) "C reate table%s (%s) ". Format (tablename,hql.tostring ())}}object json2hive{val json2hive = new Json2hive () def main (args:array[str ing]): Unit = {if (args.length! = 2) println ("Usage:json2hive jsonfile hivetablename") val jsonfile = args (0) Val hivetablename = args (1)//val jsonstr = "{\" people_type\ ": 0,\" people_num\ ": 0.1,\" people\ ": {\" person_id\ ": 5,\" Te St_count\ ": 5,\" para\ ": {\" name\ ": \" Jack\ ", \" age\ ": 6}},\" Gender\ ": 1}"//val jsonstr = "{\" people_type\ ": 0,\" object\ " : {\ "f1\": 1,\ "f2\": 1},\ "Gender\": 1} "
/* Because the JSON string is not easy to pass with parameters, the JSON file instead of */val file = Source.fromfile (Jsonfile, "UTF-8")
/* Convert the JSON string in the file to the corresponding hive table */File.getlines (). foreach (Line=>println (json2hive.tohive (line.tostring,hivetablename ))) File.close ()}}
Here are the test results
CREATE TABLE Example (People_type bigint,people_num double,people Struct<person_id:bigint,test_count:bigint,para Struct<name:string,age:bigint>>,gender bigint)
Create a hive table based on JSON