1.用户自定义schema

data

json串格式如下:

{
"partner_code": "demo",
"app_name": "web",
"person_info": {
"name": "张三",
"age": 18
},
"items": [
{
"item_id": 1,
"item_name": "王家村",
"group": "group1"
},
{
"item_id": 2,
"item_name": "李家澡堂",
"item_detail": {
"platform_count": 2
},
"group": "group2"
}
]
}

spark1.3

在spark1.3我们是这样处理的

//定义schema
val struct =StructType(
StructField("partner_code", StringType, true) ::
StructField("app_name", StringType, true)::
StructField("person_info",MapType(StringType,StringType,true)) ::
StructField("items",ArrayType(MapType(StringType,StringType,true))) ::
Nil) val data = sc.textFile("path/jsonFile")
val df = sqlContext.jsonRDD(data,struct)
df.printSchema
df.show

spark1.4

//定义schema
val struct =StructType(
StructField("partner_code", StringType, true) ::
StructField("app_name", StringType, true)::
StructField("person_info",MapType(StringType,StringType,true)) ::
StructField("items",ArrayType(MapType(StringType,StringType,true))) ::
Nil) val df = sqlContext.read.schema(struct).json("path/jsonFile")

输出结果

//df.printSchema
root
|-- partner_code: string (nullable = true)
|-- app_name: string (nullable = true)
|-- person_info: map (nullable = true)
| |-- key: string
| |-- value: string (valueContainsNull = true)
|-- items: array (nullable = true)
| |-- element: map (containsNull = true)
| | |-- key: string
| | |-- value: string (valueContainsNull = true) //df.show
+------------+--------+--------------------+--------------------+
|partner_code|app_name| person_info| items|
+------------+--------+--------------------+--------------------+
| demo| web|Map(name -> 张三, a...|List(Map(item_id ...|
+------------+--------+--------------------+--------------------+

系统自动生成schema

直接使用自带的解析会更方便,不过那样会产生大量的struct结构,同时如果结构复杂多变将会产生大量的空值。

//不需要定义schema,系统自动判断生成
val df = sqlContext.read.json("path/jsonFile")
df.printSchema
df.show

输出结果

//df.printSchema
root
|-- app_name: string (nullable = true)
|-- items: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- group: string (nullable = true)
| | |-- item_detail: struct (nullable = true)
| | | |-- platform_count: long (nullable = true)
| | |-- item_id: long (nullable = true)
| | |-- item_name: string (nullable = true)
|-- partner_code: string (nullable = true)
|-- person_info: struct (nullable = true)
| |-- age: long (nullable = true)
| |-- name: string (nullable = true) //df.show
+--------+--------------------+------------+-----------+
|app_name| items|partner_code|person_info|
+--------+--------------------+------------+-----------+
| web|List([group1,null...| demo| [18,张三]|
+--------+--------------------+------------+-----------+

SparkSQL JSON数据操作(1.3->1.4)的更多相关文章

  1. python 发送json数据操作实例分析 - python

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 本文实例讲述了python 发送json数据操作.分享给大家供大家参考,具体如下: # !/usr/bin/env py ...

  2. JSON 数据操作

    2018,狗年.如果在你出生日期的年份上加12等于2018的话,私聊我,今年是你的本命年,你得发红包!!! 子(鼠).丑(牛).寅(虎).卯(兔).辰(龙).巳(蛇).午(马).未(羊).申(猴).酉 ...

  3. ajax 返回json数据操作

    例子: $.ajax({ url: "<?=Url::toRoute('add-all-staff')?>", type: 'get', dataType: 'json ...

  4. python json 数据操作

    python 有专门针对 json 操作的函数 #!/usr/bin/python3 import json mytest_js = { "a" : 1, "b" ...

  5. js常用JSON数据操作

    JSON字符串: var  str = '{"name": "jack", "age": 13}'; JSON对象: var obj = { ...

  6. json数据操作

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  7. Java读取json文件并对json数据进行读取、添加、删除与修改操作

    转载:http://blog.csdn.net/qing_yun/article/details/46865863#t0   1.介绍 开发过程中经常会遇到json数据的处理,而单独对json数据进行 ...

  8. js之操作JSON数据

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  9. js中如何操作json数据

    一.要想熟练的操作json数据,就先要了解json数据的结构,json有两种结构:对象和数组. 1.对象 一个对象以“{”开始,“}”结束.每个“名称”后跟一个“:”:“‘名称/值’ 对”之间使用“, ...

随机推荐

  1. php mongodb类

    class HMongodb { private $mongo;    //Mongodb连接    private $curr_db_name;    private $curr_table_nam ...

  2. ImageX用来做Windows OEM部署

    https://technet.microsoft.com/en-us/library/cc722145(v=ws.10).aspx http://download.csdn.net/user/phc ...

  3. new和delete malloc和free

    程序中动态分配的对象存放在自由存储区(free store)或堆(heap). C语言程序使用一对标准库函数malloc和free在自由存储区中分配存储空间,而C++语言则使用new和delete表达 ...

  4. Magento SSH 下载安装

    http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/installing_magento_via_shell_ ...

  5. OO之美

    ㈠ 设计的分寸 对于设计,还有很多看似"惯常"的法则与经验广泛存在于软件系统中,例如除了经典的23种设计设计模式.还有很多模式之外的模式,按照粒度的大小,系统的特点,规模的大小,而 ...

  6. ubuntu mysql 远程连接

    最近需要远程连接mysql服务器,先进行简单的测试,过程记录于此. 参考链接: http://blog.chinaunix.net/uid-28458801-id-3445261.html http: ...

  7. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  8. zookeeper节点数与watch的性能测试

    zookeeper中节点数量理论上仅受限于内存,但一个节点下的子节点数量受限于request/response 1M数据 (size of data / number of znodes) zooke ...

  9. 做SSH练习的时候,JUunit运行遇到报错信息

    提示说是applicationcontext文件中的jdbcUrl信息错误,可是xml文件中的url再三确认是没有错的,不知道怎么回事? 最下面贴上applicationContext.xml中的数据 ...

  10. Eclipse中添加Android系统jar包

    这样做的好处是,可以使用Eclipse开发系统应用了,这样可以调用系统中才使用的API. 1.首先在项目中右击->属性.如图所示依次操作 2.添加User Library 3.第一次要新建Use ...