Hi dears!
I want to convert a schemaRDD into RDD of String. How can we do that?
Currently I am doing like this which is not converting correctly no
exception but resultant strings are empty
here is my code
def SchemaRDDToRDD( schemaRDD : SchemaRDD ) : RDD[ String ] = {
var types = schemaRDD.schema.fields.map( field =>
field.dataType )
var list = new ArrayList[ String ]()
types.foreach( Type => {
list.add( Type.toString )
} )
schemaRDD.map( row => rowToString( row, list ) )
}
private def rowToString( row : Row, list : ArrayList[ String ] ) :
String =
{
var record = new StringBuilder
for ( i <- 0 until row.length ) {
record.append( getValue( list.get( i ), row, i ) )
record.append( "," )
}
var sub=record.setLength( record.length - 1 ).toString
println(sub)
sub
}
/** get a single value from row object
* @param datatype of value
* @param record of type Row
* @param i index of column in row object
* @return value in string
*/
private def getValue( dataType : String, record : Row, i : Int ) :
String =
{
var res=""
if ( dataType.equalsIgnoreCase( "IntegerType" ) )
res+=record.getInt( i )
else if ( dataType.equalsIgnoreCase( "FloatType" ) )
res+=record.getFloat( i )
else if ( dataType.equalsIgnoreCase( "LongType" ) )
res+=record.getLong( i )
else if ( dataType.equalsIgnoreCase( "DoubleType" ) )
res+=record.getDouble( i )
else if ( dataType.equalsIgnoreCase( "TimestampType" ) )
res+=record.getString( i )
else if ( dataType.equalsIgnoreCase( "ShortType" ) )
res+=record.getShort( i )
else if ( dataType.equalsIgnoreCase( "ByteType" ) )
res+=record.getByte( i )
else if ( dataType.equalsIgnoreCase( "StringType" ) )
res=record.getString( i )
else
res=record.getString( i )
println(res)
res
}
--
View this message in context:
http://apache-spark-user-list.1001560.n3.nabble.com/SchemaRDD-to-RDD-String-tp20846.html
Sent from the Apache Spark User List mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]