本文提供一种用SCALA把JSON串转换为HIVE表的方法,由于比较简单,只贴代码,不做解释。有问题可以留言探讨

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
*case object JNull extends JValue
*case class JString(s: String) extends JValue
*case class JDouble(num: Double) extends 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)+"string,")
case jdo:JDouble => hql.append(fieldDelimiter(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(">")
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(">,")
case JNull=> hql.append(fieldDelimiter(level)+"string,")
case _ =>println(jv)
}
}
def toHive(jsonStr:String,tableName:String):String = {
val jsonObj = parse(jsonStr)
val hql = new StringBuilder()
decodeJson(jsonObj,0,hql)
"create table %s ( %s )".format(tableName,hql.toString())
}
}
object Json2Hive{
val json2hive = new Json2Hive()
def main (args :Array[String]) : 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,\"test_count\": 5,\"para\":{\"name\":\"jack\",\"age\":6}},\"gender\":1}"
//val jsonstr ="{ \"people_type\":0,\"object\":{\"f1\":1,\"f2\":1},\"gender\":1}"
/* 由于JSON串不容易用参数传递,故此处以json文件代替 */
val file = Source.fromFile(jsonFile,"UTF-8")
/* 将文件中的json串转换为对应的HIVE表 */
file.getLines().foreach(line=>println(json2hive.toHive(line.toString,hiveTableName)))
file.close()
}
}

  

以下是测试结果

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 )

根据JSON创建对应的HIVE表的更多相关文章

  1. 创建function实现hive表结果导出到mysql

    1. 创建临时function (这里两个包都是hive自带的,不需要自己开发的,可以根据名称查找对应的版本) add jar /opt/local/hive/lib/hive-contrib-.ja ...

  2. flume的sink写入hive表

    flume的配置文件如下: a1.sources=r1 a1.channels=c1 a1.sinks=s1 a1.sources.r1.type=netcat a1.sources.r1.bind= ...

  3. hive中创建hive-json格式的表及查询

    在hive中对于json的数据格式,可以使用get_json_object或json_tuple先解析然后查询. 也可以直接在hive中创建json格式的表结构,这样就可以直接查询,实战如下(hive ...

  4. 【原】创建Hive表,分号分隔符“;”引起的异常

    [障碍再现] 在创建支持Map数据结构的Hive表时,抛出如下异常 hive> create table tab_map(name string,info map<string,strin ...

  5. 批量导入数据到hive表中:假设我有60张主子表如何批量创建导入数据

    背景:根据业务需要需要把60张主子表批量入库到hive表. 创建测试数据: def createBatchTestFile(): Unit = { to ) { val sWriter = new P ...

  6. Hive表中Partition的创建

    作用: 在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,在对应的partition里面去查找就可以,减少查询时间. 1. 创建表 ...

  7. hive 将hive表数据查询出来转为json对象和json数组输出

    一.将hive表数据查询出来转为json对象输出 1.将查询出来的数据转为一行一行,并指定分割符的数据 2.使用UDF函数,将每一行数据作为string传入UDF函数中转换为json再返回 1.准备数 ...

  8. [Hive]使用HDFS文件夹数据创建Hive表分区

    描写叙述: Hive表pms.cross_sale_path建立以日期作为分区,将hdfs文件夹/user/pms/workspace/ouyangyewei/testUsertrack/job1Ou ...

  9. 用puthivestreaming把hdfs里的数据流到hive表

    全景图:   1. 创建hive表 CREATE TABLE IF NOT EXISTS newsinfo.test( name STRING ) CLUSTERED BY (name)INTO 3 ...

随机推荐

  1. git帮助网址

    http://rogerdudler.github.io/git-guide/index.zh.html https://help.github.com/

  2. 网络基础——TCP

    TCP和UDP协议特点 1.TCP 1>.传输控制协议 2>.可靠的.面向连接的协议 3>.传输效率低 2.UDP 1>.用户数据报协议 2>.不可靠的.无连接的服务 3 ...

  3. stall and flow separation on airfoil or blade

    stall stall and flow separation Table of Contents 1. Stall and flow separation 1.1. Separation of Bo ...

  4. PAT 1141 PAT Ranking of Institutions

    After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...

  5. Maven学习总结(32)——Maven项目部署到Tomcat8中

    1.环境准备 Maven.Tomcat8.Eclipse 2.maven中的镜像配置 大家知道,mavne默认使用的是国外的镜像,但是速度很慢,这里建议大家使用阿里的中央仓库镜像. 阿里出品,必出精品 ...

  6. mdbtools使用

    1.导入数据库到mysql(将key.mdb导入MySQL的test数据库,此时只导入表结构) mdb-schema key.mdb mysql | mysql -u root -p test 2.将 ...

  7. hash扩展长度攻击及hashdump使用

    摘自: 1.http://www.freebuf.com/articles/web/69264.html 2.https://www.cnblogs.com/pcat/p/5478509.html 0 ...

  8. Java 学习(6):java Number & Math & String & 数组...常用类型

    目录 --- Number & Math类 --- Character 类 --- String 类 --- StringBuffer 类 --- 数组 Number & Math类: ...

  9. memory management in oracle 11G R2

    When we talking about memory management in Oracle, we are refering to SGA and PGA. The management me ...

  10. linux下nginx+svn

    http://fengqi.me/unix/23.html 因为没有什么可以定制的, 所以svn直接使用系统自带的包管理软件安装, 以centos系列为例, 命令如下: yum install sub ...