在 Spark 中支持用户定义的 java 类型 [英] Support for User Defined Types for java in Spark

查看:44
本文介绍了在 Spark 中支持用户定义的 java 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

spark 中是否支持 Java 的 UDT?

Is there support for UDT for java in spark?

在将 JavaRDD 转换为 JavaSchemaRDD 时,JavaSQLContext 是否支持用户定义类型(UDT)?

Does JavaSQLContext support User Defined Types(UDTs) when converting JavaRDD to JavaSchemaRDD?

如果是,是否有任何样本可以证明该能力.

If yes, is there any sample to demonstrate the capability.

推荐答案

是的,最简单的方法是通过反射来推断.查看 SparkSQL 文档,然后单击 Java 选项卡.然后,阅读标记为

Yes, the simplest way is to have it inferred via reflection. See the SparkSQL documentation and click on the Java tab. Then, read the section labelled

使用反射推断模式

从评论中编辑

我不确定 Java API 是否像 Scala API 那样容易充实,因此似乎为了嵌套类型,您可能需要自己构建架构:

I'm not sure that the Java API is as easily fleshed out as the scala one, so it seems in order to nest types you may need to build the schema yourself:

//First create the address
List<StructField> addressFields = new ArrayList<StructField>();
fields.add(DataType.createStructField("street", DataType.StringType, true));    
StructType addressStruct = DataType.createStructType(addressFields)

//Then create the person, using the address struct
List<StructField> personFields = new ArrayList<StructField>();
fields.add(DataType.createStructField("name", DataType.StringType, true));
fields.add(DataType.createStructField("age", DataType.IntType, true));
fields.add(DataType.createStructField("address", addressStruct, true));

StructType schema = DataType.createStructType(fields);

这篇关于在 Spark 中支持用户定义的 java 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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