ST_geomfromtext function using Spark / java

后端 未结 2 2002
南旧
南旧 2020-12-20 03:39

Since the ST_GeomFromText is not the part of org.apache.spark.sql.functions so it will not recognise it internally.I need to first define the UDF for this function. means I

2条回答
  •  太阳男子
    2020-12-20 04:06

    Similar question-

    1. GeoSpark librairy using Spark Java
    2. From ResultSet to Spark dataframe using Java
    3. GeoSpark using Spark / Java
    4. Undefined function: 'ST_GeomFromText' Using Spark / Java

    I think, you haven't followed the GeoSparkSQL-Overview/#quick-start thoroughly-

    1. As per the quick start you need to Add GeoSpark-core and GeoSparkSQL into your project POM.xml or build.sbt
    
            
                org.datasyslab
                geospark-sql_2.3
                1.3.1
            
            
            
                com.vividsolutions
                jts
                1.13
            
            
            
                org.datasyslab
                geospark-viz_2.3
                1.3.1
            
            
                org.datasyslab
                geospark
                1.3.1
            
    
    1. Declare your Spark Session
    SparkSession sparkSession = SparkSession.builder()
                    .config("spark.serializer", KryoSerializer.class.getName())
                    .config("spark.kryo.registrator", GeoSparkKryoRegistrator.class.getName())
                    .master("local[*]")
                    .appName("myGeoSparkSQLdemo")
                    .getOrCreate();
    
    1. Register all the functions from geospark-sql_2.3 to the sparkSession so that it can be used directly spark-sql
    // register all functions from geospark-sql_2.3 to sparkSession
    GeoSparkSQLRegistrator.registerAll(sparkSession);
    

    Now Here is the working example-

       SparkSession sparkSession = SparkSession.builder()
                    .config("spark.serializer", KryoSerializer.class.getName())
                    .config("spark.kryo.registrator", GeoSparkKryoRegistrator.class.getName())
                    .master("local[*]")
                    .appName("myGeoSparkSQLdemo")
                    .getOrCreate();
    
            // register all functions from geospark-sql_2.3 to sparkSession
            GeoSparkSQLRegistrator.registerAll(sparkSession);
            try {
                System.out.println(sparkSession.catalog().getFunction("ST_Geomfromtext"));
                // Function[name='ST_GeomFromText', className='org.apache.spark.sql.geosparksql.expressions.ST_GeomFromText$', isTemporary='true']
            } catch (Exception e) {
                e.printStackTrace();
            }
            // https://datasystemslab.github.io/GeoSpark/api/sql/GeoSparkSQL-Function/
            Dataset dataframe = sparkSession.sql("select ST_GeomFromText('POINT(-7.07378166 33.826661)')");
            dataframe.show(false);
            dataframe.printSchema();
            /**
             * +---------------------------------------------+
             * |st_geomfromtext(POINT(-7.07378166 33.826661))|
             * +---------------------------------------------+
             * |POINT (-7.07378166 33.826661)                |
             * +---------------------------------------------+
             */
    
            // using longitude and latitude column from existing dataframe
            Dataset df = sparkSession.sql("select -7.07378166 as longitude, 33.826661 as latitude");
            df.withColumn("ST_Geomfromtext ",
                    expr("ST_GeomFromText(CONCAT('POINT(',longitude,' ',latitude,')'))"))
            .show(false);
            /**
             * +-----------+---------+-----------------------------+
             * |longitude  |latitude |ST_Geomfromtext              |
             * +-----------+---------+-----------------------------+
             * |-7.07378166|33.826661|POINT (-7.07378166 33.826661)|
             * +-----------+---------+-----------------------------+
             */
    

提交回复
热议问题