在Scala中动态生成案例类 [英] Dynamically generate case class in Scala

查看:454
本文介绍了在Scala中动态生成案例类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个相当大的csv文件并对其进行处理(切片,切块,汇总等).interactively(数据浏览).我的想法是将文件读入数据库(H2)并使用SQL进行处理:

I want to read a rather large csv file and process it (slice, dice, summarize etc.) interactively (data exploration). My idea is to read the file into a database (H2) and use SQL to process it:

  1. 读取文件:我使用 Ostermiller csv解析器

确定每一列的类型:我随机选择50行,并得出每一列的类型(int,long,double,date,string)

Determine the type of each column: I select randomly 50 rows and derive the type (int, long, double, date, string) of each column

我想使用 Squeryl 进行处理.为此,我需要动态创建一个case类.到目前为止,这是瓶颈!

I want to use Squeryl to process. To do so I need to create a case class dynamically. That's the bottleneck so far!

我将文件上传到H2并使用任何SQL命令.

I upload the file to H2 and use any SQL command.

我的问题:

  • 在Scala中,有没有更好的通用交互方式?
  • 有没有办法解决第三点?换句话说,给定类型列表(对应于csv文件中的列),是否可以动态创建与Squeryl中的表相对应的case类?据我了解,我可以使用宏来做到这一点,但是我没有足够的知识来做到这一点.

推荐答案

我认为您对第一个问题的回答听起来很合理.

I think your approach to the first question sounds reasonable.

关于第二个问题-作为drexin答案的补充-可以使用 ASM .使用这样的库,您可以生成与案例类相同的字节码.

Regarding your 2nd question - as an addition to drexin's answer - it is possible to generate the bytecode, with a library such as ASM. With such a library you can generate the same byte code as a case class would.

这篇关于在Scala中动态生成案例类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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