在 Spark 中为具有 1500 列的表定义 DataFrame Schema [英] Defining DataFrame Schema for a table with 1500 columns in Spark

查看:21
本文介绍了在 Spark 中为具有 1500 列的表定义 DataFrame Schema的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 中有一个包含大约 1500 列的表.我需要从该表中读取数据,然后将其转换为正确的数据类型格式,然后将记录插入 Oracle DB.

I have a table with around 1500 columns in SQL Server. I need to read the data from this table and then convert it to proper datatype format and then insert the records into Oracle DB.

为表中超过 1500 列的此类表定义架构的最佳方法是什么.除了对列名和数据类型进行硬编码之外,还有其他选择吗?

What is the best way to define the schema for this type of table with more than 1500 columns in a table. Is there any other option than hard coding the column names along with the datatypes?

  1. 使用案例类
  2. 使用StructType.

使用的 Spark 版本是 1.4

Spark Version used is 1.4

推荐答案

针对此类需求.我会提供 case 类方法来准备数据框

是的,有一些限制,例如生产力,但我们可以克服...你可以像下面的例子一样为 <2.11 版:

Yes, There are some limitations like productarity but we can overcome... you can do like below example for < versions 2.11 :

准备一个案例类,它扩展产品并覆盖方法.

prepare a case class which extends Product and overrides methods.

喜欢...

  • productArity():Int: 这将返回属性的大小.在我们的例子中,它是 33.所以,我们的实现看起来像这样:

  • productArity():Int: This returns the size of the attributes. In our case, it's 33. So, our implementation looks like this:

productElement(n:Int):Any: 给定一个索引,这将返回属性.作为保护,我们还有一个默认情况,它会抛出一个 IndexOutOfBoundsException 异常:

productElement(n:Int):Any: Given an index, this returns the attribute. As protection, we also have a default case, which throws an IndexOutOfBoundsException exception:

canEqual (that:Any):Boolean:这是三个函数中的最后一个,当对类进行相等检查时,它用作边界条件:>

canEqual (that:Any):Boolean: This is the last of the three functions, and it serves as a boundary condition when an equality check is being done against class:

使用 StructType 定义架构并创建数据框.(如果您不想使用 spark csv api)

Use the StructType to define the schema and create the dataframe.(if you don't want to use spark csv api)

这篇关于在 Spark 中为具有 1500 列的表定义 DataFrame Schema的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆