Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

重构 Excel 数据源的插件实现 #45

Open
ZhengshuaiPENG opened this issue Aug 4, 2022 · 5 comments
Open

重构 Excel 数据源的插件实现 #45

ZhengshuaiPENG opened this issue Aug 4, 2022 · 5 comments

Comments

@ZhengshuaiPENG
Copy link
Contributor

背景:

目前 Excel 数据源插件在 load 和 save 都有不少问题,需要做一次更新

关于 Excel 处理理解的原则

我们可以对单一的 excel 认知为 db , 每一个 sheet 是 该 db 下的 table, csv 可以等同于单一sheet 的 excel 文件。
在加载时,我们应该将 sheet 映射成表, load 和 save 都是以 sheet 为单位来进行操作。

Load CSV:

需要支持的特性:

  1. 指定 sheetname 进行 load 为表,在加载时可以根据参数 sheetName 参数来进行加载制定的sheet,如未显式指定,则默认加载第一个sheet

  2. 支持 skip first n lines 功能, 需要在 load 是在 where 语句中新增 skipFirstNLines 参数,值接受正整数 int 类型。比如整个 sheet 有 100 行, 当 skip 5 行,且header=false时,这里预期应该是返回第 6 - 100 行(共95行),如果skip 5 行且 header = true 时,预期是返回第 7 - 100 行,共 94 行 正确预期行为

Save Excel

Append Save

现有的行为,当 save append 时将表作为一张新的 sheet 进行 append 写入,考虑以下的代码

SET rawData=''' 
{"jack":1,"jack2":2}
{"jack":2,"jack2":3}
''';

SET savePath="/tmp/test_excel.xlsx";

LOAD jsonStr.`rawData` AS table1;

SAVE append table1 AS excel.`${savePath}`
where header = "true";

生成的结果是 “sheet1”,如下图
image

再次执行一次, 会将表以 sheet2 的方式增加到 excel 中,如下
image

这里的行为不满足 append 的定义,或者说这里要表达的语义是 SaveAsNewSheet 这么一个语义。

期望的一个 append 的 save 行为

  • 默认不指定sheet 的时候,append 应该是以追加的方式,写入默认的第一个sheet 中,需要保持 schema 对齐,否则失败

  • 指定已存在的 sheet 中,append 以追加的方式到该 sheet (行为和上一条其实是一致的)

  • 指定未存在的 sheet 中,append 以追加写的方式写入新的指定的 sheet name 中(理解为新加sheet,写表进去)

行为兼容性变更:append sheet 的方式由原先默认新增 sheet, 变成指定新的 sheet name 来进行 sheet append,能力上可以保持一致

Overwrite Save

save overwrite 会涉及 excel 文件的覆写还是sheet 的覆写,这里关键的问题,是默认不指定任何 sheet 的情况下,是覆写第一个sheet,还是将整个文件删除进行覆写。

期望的一个 overwrite 的save行为

  • 默认不指定 sheet 的时候,覆写应该是全文件覆写(从用户的心智来看,不指定任何sheet 就有两种情况,一种我只有一个sheet,另一个是我全部的sheet都不想要了)
  • 指定 sheet 的时候,覆写单一sheet

这样 save overwrite 的行为在理解上和 save append 是保持一致的, 且与 csv 数据源保持一致

@ZhengshuaiPENG
Copy link
Contributor Author

@ZhengshuaiPENG
Copy link
Contributor Author

需要注意的事情如下:
当前项目中 excel 的实现引用了 https://github.com/crealytics/spark-excel 的源码,需要考虑是否将源码引用的方式进行更改为依赖的方式引用

@ZhengshuaiPENG
Copy link
Contributor Author

现有 Excel 插件在 load 时中支持的参数有:

参数 默认值 说明
header true 必填,是否将第一行作为列名
treatEmptyValuesAsNulls true 是否将空值视为 Null
usePlainNumberFormat false 是否使用四舍五入和科学计数法格式化单元格
inferSchema false 是否开启推断模式
addColorColumns false 是否需要获取列的背景颜色
dataAddress A1 读取数据的范围
timestampFormat yyyy-mm-dd hh:mm:ss[.fffffffff] 时间戳格式
maxRowsInMemory None 内存中缓存的最大数据条数
excerptSize 10 如果同时设置 inferSchemaexcerptSize,则会推断的数据条数
workbookPassword None excel 的密码

ZhengshuaiPENG pushed a commit that referenced this issue Aug 10, 2022
* add config params when initializing a data loader

* resolve save issues in excel ET

* revise skip first n lines var name
@ZhengshuaiPENG
Copy link
Contributor Author

整体 scope 合并至 2.3.4 进行发布,需要更新手册中的章节:

@ZhengshuaiPENG
Copy link
Contributor Author

在 2.3.3 版本中优化了 Excel 的读取,支持了 SkipFirstNLines 功能

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants