How to cast incorrectly detected schema. Pyspark-ES

(Pramod Sripada) #1


I am reading in geo point data from ElasticSearch index using Pyspark. I am creating my DataFrame using the following command.

us_df ='es').option('es.query', us_q).option('', 'extra_tags').load('index').select('', 'centroid.lon')

When I print the Schema of the DataFrame I get the schema as

 |-- lat: double (nullable = true)
 |-- lon: double (nullable = true)

But when I try to get the first 10 records


I get an error

18/03/11 21:10:38 ERROR TaskSetManager: Task 0 in stage 6.0 failed 1 times; aborting job
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ksripada/spark-2.1.1-bin-hadoop2.7/python/pyspark/sql/", line 429, in take
    return self.limit(num).collect()
  File "/Users/ksripada/spark-2.1.1-bin-hadoop2.7/python/pyspark/sql/", line 391, in collect
    port = self._jdf.collectToPython()
  File "/Users/ksripada/spark-2.1.1-bin-hadoop2.7/python/lib/", line 1133, in __call__
  File "/Users/ksripada/spark-2.1.1-bin-hadoop2.7/python/pyspark/sql/", line 63, in deco
    return f(*a, **kw)
  File "/Users/ksripada/spark-2.1.1-bin-hadoop2.7/python/lib/", line 319, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o260.collectToPython.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 6.0 failed 1 times, most recent failure: Lost task 0.0 in stage 6.0 (TID 9, localhost, executor driver): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
	at scala.runtime.BoxesRunTime.unboxToDouble(
	at org.apache.spark.sql.catalyst.expressions.BaseGenericInternalRow$class.getDouble(rows.scala:44)
	at org.apache.spark.sql.catalyst.expressions.GenericInternalRow.getDouble(rows.scala:194)
	at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIterator.processNext(Unknown Source)
	at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(
	at org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8$$anon$1.hasNext(WholeStageCodegenExec.scala:377)
	at org.apache.spark.sql.execution.SparkPlan$$anonfun$2.apply(SparkPlan.scala:231)
	at org.apache.spark.sql.execution.SparkPlan$$anonfun$2.apply(SparkPlan.scala:225)
	at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$25.apply(RDD.scala:827)
	at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$25.apply(RDD.scala:827)
	at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
	at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
	at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
	at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
	at org.apache.spark.executor.Executor$
	at java.util.concurrent.ThreadPoolExecutor.runWorker(
	at java.util.concurrent.ThreadPoolExecutor$

I have seen the trace and the latitude and longitude is being returned as Strings from ES


I have seen an issue raised on Github #1047 that has a similar issue of latitudes that are strings being inferred as double, and while reading the data, it throws an error.

Could you please let me know how I can enforce latitude and longitude to be interpreted as Strings rather than double while creation of the DataFrame using any options.

Spark Version - 2.1.1
ES-Hadoop jar - elasticsearch-spark-20_2.11-5.2.2.jar
ES Version - 5.2.2


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.