用户自定义类型为星火java支持 [英] Support for User Defined Types for java in Spark
问题描述
是否有火花UDT对Java的支持?
Is there support for UDT for java in spark?
是否JavaSQLContext支持用户定义类型(UDT)转换为JavaRDD时JavaSchemaRDD?
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的一样,很容易充实为斯卡拉之一,因此它似乎为了巢类型,您可能需要建立自己的模式:
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);
这篇关于用户自定义类型为星火java支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!