Generate JSON with Null Columns in Spark
Install JDK8 Oracle on Ubuntu 18.04
https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-ubuntu-18-04
> sudo add-apt-repository ppa:webupd8team/java
> sudo apt update
> sudo apt install oracle-java8-installer
Java Version
> java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
Having Hadoop and Zeppelin Running
Dump the CSV data from DynamoDB
> export-dynamodb -t meeting-prod-meetings -f csv -o meetingdb.csv
Create the data directory
> hdfs dfs -mkdir hdfs://localhost:9000/meetingpin
Put the data to remote
> hdfs dfs -put ./meetingdb.csv hdfs://localhost:9000/meetingpin/meetingdb.csv
Fetch the result to local
> hdfs dfs -get hdfs://localhost:9000/meetingpin/meetings_to_updatepin ./meetings_to_updatepin
Here are some normal steps to investigate the data
val meetingRawDF = sqlContext.read.format("csv")
.option("header", "true")
.option("inferSchema", "true")
.load("hdfs://localhost:9000/meetingpin/meetingdb.csv")
val meeting1DF = meetingRawDF.toDF(meetingRawDF.columns map(_.toLowerCase): _*)
val meetingDF = meeting1DF.columns.foldLeft(meeting1DF)((curr, n) => curr.withColumnRenamed(n, n.replaceAll("\\s", "_")))
meetingDF.printSchema()
meetingDF.createOrReplaceTempView("meetings")
%sql
SELECT
pinrequired as pinRequired,
maxparticipants as maxParticipants,
lastcalldate as lastCallDate,
clientapiid as clientApiID,
ownerclientapiid as ownerClientApiID,
uuid as UUID,
displayname as displayName,
email as email
FROM meetings
WHERE pin = 'NA' limit 5
Here is the hard part, because usually, the normal thing is to ignore the JSON key if the value is null, we need to create a Case Class and doing the converting to JSON ourselves
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.databind.ObjectMapper
import spark.implicits._
case class MeetingToUpdate(UUID:String, pin:String, action:String, `type`:String)
val meetingsToUpdateDF = sqlContext.sql("""
SELECT
uuid as UUID,
null as pin,
'UPDATED' as action,
'MEETING' as type
FROM meetings
WHERE pin = 'NA'
""")
val meetingsCaseDF = meetingsToUpdateDF.map(row => MeetingToUpdate(row.getString(0), null, row.getString(2), row.getString(3)))
val meetingsToUpdateNullDF = meetingsCaseDF.mapPartitions(records => {
val mapper = new ObjectMapper with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
records.map(mapper.writeValueAsString(_))
}
)
meetingsToUpdateNullDF.show(1)
meetingsToUpdateNullDF.coalesce(1).write.text("hdfs://localhost:9000/meetingpin/meetings_to_updatepin2")
References:
https://stackoverflow.com/questions/28166555/how-to-convert-row-of-a-scala-dataframe-into-case-class-most-efficiently
https://stackoverflow.com/questions/45235593/write-a-spark-dataset-to-json-with-all-keys-in-the-schema-including-null-column?rq=1
https://stackoverflow.com/questions/44271612/retain-keys-with-null-values-while-writing-json-in-spark
分享到:
相关推荐
Practical instruction on using JavaScript Object Notation (JSON) with MySQL This hands-on guide teaches, step by step, how to use JavaScript Object Notation (JSON) with MySQL. Written by a MySQL ...
JSON Generate 是一种基于 JSON 示例创建 go 结构的工具。 安装 $ go get github.com/calavera/json_generate 用法 在 go 代码中添加一个 JSON 示例作为常量。 就像是: const JSONExample_User = `{ "name": ...
// Generate JSON document dynamically using namespace std; using namespace jsonxx; Array a; a ; a ; a ; a ; a ; a ("key", "value"); Object o; o ; o ; o ; cout << o.json() ; To do Custom JSON ...
Focusing on a sequence of tutorials that deliver a working news intelligence service, you will learn about advanced Spark architectures, how to work with geographic data in Spark, and how to tune ...
nifi-generatejson-处理器 一个NiFi处理器,用于基于json-data-generator java库( )生成JSON。 安装 : 从github版本选项卡下载nar文件并将其放在NiFi lib文件夹$ NIFIROOT / lib /中 重新启动NiFi以加载新处理器...
此外,了解JSON的基本语法和规则也非常重要,比如JSON支持的数据类型(字符串、数字、布尔、null、对象和数组),以及对象和数组的表示方式。在Delphi中,与JSON交互时,还需要关注数据类型之间的转换,比如从JSON...
function string GenerateJson(dictionary dict) string jsonStr // 遍历字典并构建JSON字符串 for each key, value in dict jsonStr &= "{" & key & ": " & value & "}" end for return jsonStr end function...
fs.writeFileSync('config.json', JSON.stringify(config, null, 2)); ``` 这段代码首先读取JSON模式文件,然后创建一个`Ajv`实例。`generateConfig`函数使用`ajv.compile`方法编译模式,并生成一个函数,该函数...
这些API包括`UTL_JSON`包,它包含了一系列的函数和过程,如`READ_JSON`用于解析JSON字符串,`WRITE_JSON`用于构建JSON字符串,以及`GET_VALUE`和`SET_VALUE`用于读写JSON对象的特定值。不过,`UTL_JSON`的功能相对...
【标题】"generate-json:生成随机批量JSON"是一款基于JavaScript的工具,用于创建批量的随机JSON数据。这个项目是原有项目的一个扩展,旨在提供一种灵活的方式,根据预定义的模板生成JSON格式的数据。 【描述】在...
codegenerate-1.0.4.jar
5. **生成Schema**:调用Generator的`generateSchema()`方法,传入Bean的Class对象,得到Json Schema的JSON表示。 6. **处理结果**:将生成的JSON字符串保存为文件,或者直接用于API文档或其他验证目的。 示例代码...
- cJSON_NULL:表示JSON的null。 **4. cJSON的API** cJSON库提供了丰富的API来操作JSON数据,包括: - cJSON_Parse():解析JSON字符串并返回根对象。 - cJSON_Print()和cJSON_PrintUnformatted():将JSON对象转换...
它提供了`JSON.parse`来解析JSON字符串和`JSON.generate`来生成JSON。对于更复杂的场景,`oj`或`yajl-ruby`等库可以提供更高的性能。 这些依赖包在处理JSON数据时,不仅可以进行基本的编码和解码,还能进行对象映射...
在Spark大数据处理框架中,Driver的角色至关重要,它负责协调整个计算过程,生成Jobs并调度Tasks。本篇文章将深入探讨Spark Driver的工作机制,以及如何生成Jobs并启动Tasks。 首先,我们来理解Spark作业(Job)与...
快捷生成json格式的微信公众号自定义菜单;Quickly generate the WeChat public custom menu in json format
在Verilog-2001版本中,引入了`generate`语句,这极大地增强了代码的可读性和复用性,特别是在创建并行结构和根据条件实例化模块时。`generate`允许你基于变量或者条件来动态地创建模块实例、网线(net)、变量...
如果你需要将Ruby对象转换成JSON字符串,可以使用`JSON.generate`方法。例如: ```ruby require 'json' data = { name: "John", age: 30, city: "New York" } json_string = JSON.generate(data) puts json_...
public String generateJson() { return ActionSupport.SUCCESS; } } ``` 3. **配置Action**: 在`struts.xml`中为Action添加配置,并设置返回JSON的结果类型: ```xml <result type="json"> ...
标题中的“精品软件工具--Automatically generate model files, support JSON an.zip”暗示了一个软件工具,它能够自动地生成模型文件,并且支持JSON格式。这个工具可能是面向开发人员的,特别是那些在编程过程中...