Packages

  • package root
    Definition Classes
    root
  • package com
    Definition Classes
    root
  • package precisely
    Definition Classes
    com
  • package bigdata
    Definition Classes
    precisely
  • package addressing
    Definition Classes
    bigdata
  • package spark
    Definition Classes
    addressing
  • package api

    This section provides code snippets to simplify usage of Geo Addressing SDK for Big Data.

    This section provides code snippets to simplify usage of Geo Addressing SDK for Big Data.


    Geocode Operation Example

    import org.apache.spark.sql.functions._
    import com.pb.downloadmanager.api._
    import com.pb.downloadmanager.api.downloaders._
    import com.pb.downloadmanager.api.downloaders.hadoop._
    import com.precisely.addressing.v1.model._
    import com.precisely.bigdata.addressing.spark.api._
    import com.precisely.addressing.v1._
    
    // DownloadManager copies all the remote resources (HDFS, S3, Google Storage, etc.) to the
    // provided local path (e.g. ./downloads) of every worker node.
    // This happens in runtime during the first call. Subsequent calls skips downloading process.
    val downloadManager = new DownloadManagerBuilder("./downloads")
      .addDownloader(new S3Downloader(spark.sparkContext.hadoopConfiguration))
      .addDownloader(new GoogleDownloader(spark.sparkContext.hadoopConfiguration))
      .addDownloader(new HDFSDownloader(spark.sparkContext.hadoopConfiguration))
      .addDownloader(new LocalFilePassthroughDownloader())
      .build()
    
    // Remove the fields which are not required.
    val outputFields = Seq(
        "location.feature.geometry.coordinates.x as LON",
        "location.feature.geometry.coordinates.y as LAT",
        "address.formattedAddress as fullAddress",
        "address.city.longName as locality",
        "address.country.isoAlpha2Code as countryshortname",
        "address.country.name as country",
        "address.placeName as placename",
        "address.admin1.shortName as administrativearealevel1",
        "address.admin2.longName as administrativearealevel2",
        "address.suburb as suburb",
        "address.borough as borough",
        "address.postalCode as postalcode",
        "address.postalCodeExt as postalcodesuffix",
        "customFields['PB_KEY'] as sourceplaceid",
        "address.addressNumber as AddressNumber",
        "address.street as Street",
        "address.unit as Unit",
        "address.unitType as UnitType",
        "address.formattedStreetAddress as StreetAddress",
        "address.formattedLocationAddress as LocationAddress",
        "address.neighborhood as neighborhood",
        "address.floor as floor",
        "score",
        "explanation.source['label'] as label",
        "customFields['PRECISION_CODE'] as PrecisionCode",
        "customFields['MATCH_TYPE'] as MatchType",
        "customFields['LOC_CODE'] as LocationCode",
        "customFields['MATCH_CODE'] as MatchCode",
        "customFields['CBSA_NAME'] as CBSAName",
        "customFields['PREF_CITY'] as PREFCITY",
        "customFields['PB_KEY'] as PBKEY",
        "customFields['DATATYPE_NAME'] as DataTypeName"
    )
    
    // You can also use paths from HDFS or Databricks Volumes
    // (e.g. hdfs:///addressingDistribution/resources or /Volumes/precisely/default/geo-addressing/addressingDistribution/resources/)
    // The reference data is extracted at runtime in every worker node's extractionLocation path.
    val geocodeUdf = new AddressingBuilder()
        .withResourcesLocation("s3a:///addressingDistribution/resources")
        .withDownloadManager(downloadManager)
        .withDataLocations("s3a://com.precisely.data/path_to_spd1","s3a://path_to_spd2")
        .withExtractionLocation("./extracted")
        .udfBuilder()
        .withPreferences(new PreferencesBuilder().withReturnAllInfo(true).build())
        .withOutputFields(outputFields: _*)
        .withErrorField("error")
        .withResultAsJSON("jsonOutput")
        .forGeocode()
    
    val geocodeDF = input.withColumn("result", geocodeUdf(map(
      lit("addressLines[0]"), col("address"),
      lit("country"), col("country")
    ))).select("*", "result.*").drop(colName = "result")


    NOTE: If addressing.yaml is the present at the worker node, dataLocations and extractionLocation is preferred from addressing.yaml over code. In such cases where addressing.yaml is present, you should only provide resourcesLocation.


    Custom Executor Example for Multipass Geocoding:

    import com.precisely.bigdata.addressing.spark.api._
    import org.apache.spark.sql.functions._
    
    // Refer to Geocode Operation Example for Full Code.
    // Use .forCustomExecutor() API
    val customUdf = new AddressingBuilder()
     ...
     .forCustomExecutor(new AddressingExecutor {
          override def execute(input: RequestInput, preferences: Option[Preferences], addressing: Addressing): Response = {
    
              // First Pass
              val response = addressing.geocode(input.requestAddress(), preferences.orNull)
              // Validate the results.
              if (results.get(0).getCustomFields.get("PRECISION_CODE").toUpperCase().startsWith("S8"))
                    return response
    
              // Second Pass
              // Convert MultiLine Input Address to SingleLine
              val singleLineAddress: RequestAddress = new RequestAddress()
              val filterAddress: String = Seq(inputMultiLineAddress.getStreet,
                  inputMultiLineAddress.getCity,
                  inputMultiLineAddress.getAdmin1,
                  inputMultiLineAddress.getPostalCode
              ).filter(isNotNull).mkString(" ")
              singleLineAddress.setAddressLines(java.util.Collections.singletonList(filterAddress: String))
              singleLineAddress.setCountry(inputMultiLineAddress.getCountry)
    
              // Geocode with SingleLineAddress and CustomPreferences
              val singleLineResponse: Response = addressing.geocode(singleLineAddress,
                            new PreferencesBuilder().withReturnAllInfo(true).build())
    
              // Adding a Custom Field
              singleLineResponse.getResults.get(0).getCustomFields.put("TOTAL_MATCHES", singleLineResponse.getResults.size().toString)
              return singleLineResponse
          }
          override def execute(lookupType: LookupType, preferences: Option[Preferences], addressing: Addressing, keyValues: KeyValue*): Response = ???
          override def execute(x: Double, y: Double, country: String, preferences: Option[Preferences], addressing: Addressing): Response = ???
      })
    
    val geocodeDF = input.withColumn("result", customUdf(map(
      lit("addressLines[0]"), col("address"),
      lit("country"), col("country")
    ))).select("*", "result.*").drop(colName = "result")


    Verify Operation Example:

    import com.precisely.bigdata.addressing.spark.api.AddressingBuilder
    import org.apache.spark.sql.functions._
    
    // Refer to Geocode Operation Example for Full Code.
    // Use .forVerify() API
    val verifyUdf = new AddressingBuilder()
      ...
      .forVerify()
    
    val verifyDf = input.withColumn("result", verifyUdf(map(
      lit("addressLines[0]"), col("address"),
      lit("country"), col("country")
    ))).select("*", "result.*").drop(colName = "result")


    Reverse Geocode Operation Example:

    import com.precisely.bigdata.addressing.spark.api.AddressingBuilder
    import org.apache.spark.sql.functions._
    
    // Refer to Geocode Operation Example for Full Code.
    // Use .forReverseGeocode() API
    val reverseGeocodeUdf = new AddressingBuilder()
      ...
      .forReverseGeocode()
    
    val reverseGeocodeDf = input.withColumn("result", reverseGeocodeUdf(
      col("x"), col("y"), col("country")
    )).select("*", "result.*").drop(colName = "result")


    Lookup Operation Example:

    import com.precisely.bigdata.addressing.spark.api.AddressingBuilder
    import org.apache.spark.sql.functions._
    
    // Refer to Geocode Operation Example for Full Code.
    // Use .forLookup() API
    val lookupUdf = new AddressingBuilder()
      ...
      .forLookup()
    
    val lookUpUdf = input.withColumn("result", lookupUdf(
      col("keyType"), col("key"), col("country")
    )).select("*", "result.*").drop(colName = "result")


    Geocode Operation with SQL Queries:

    import com.precisely.bigdata.addressing.spark.api.AddressingBuilder
    import org.apache.spark.sql.functions._
    
    // Refer to Geocode Operation Example for Full Code.
    // Use .forGeocode(<udf-name>) API
    val geocodeUdf = new AddressingBuilder()
      ...
      .forGeocode("PreciselyGeocode")
    
    inputDF.createOrReplaceTempView("inputTable")
    val geocodeDF = spark.sql("select *, PreciselyGeocode(map('addressLines[0]', address, 'country', country)) as result from inputTable")
    Definition Classes
    spark
  • AddressingBuilder
  • AddressingExecutor
  • AddressingProvider
  • MapFunctionBuilder
  • RequestInput
  • UDFBuilder

class AddressingBuilder extends AnyRef

This class allows you to either build an AddressingProvider for use in your code, or to branch off into a builder for a UDF that executes an addressing operation.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. AddressingBuilder
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new AddressingBuilder()

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def addressingProvider(): AddressingProvider

    A serializable provider for the Addressing API, configured with the builder settings, that can be shared among tasks.

    A serializable provider for the Addressing API, configured with the builder settings, that can be shared among tasks.

    returns

    A serializable Addressing API provider.

  5. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  6. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @IntrinsicCandidate() @native()
  7. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  8. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  9. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @IntrinsicCandidate() @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @IntrinsicCandidate() @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. def mapFunctionBuilder(): MapFunctionBuilder

    A builder for an addressing MapFunction, configured with the current Addressing API settings from this builder.

    A builder for an addressing MapFunction, configured with the current Addressing API settings from this builder.

    returns

    A builder for an addressing map function.

  13. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  14. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @IntrinsicCandidate() @native()
  15. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @IntrinsicCandidate() @native()
  16. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  17. def toString(): String
    Definition Classes
    AnyRef → Any
  18. def udfBuilder(): UDFBuilder

    A builder for an addressing UDF, configured with the current Addressing API settings from this builder.

    A builder for an addressing UDF, configured with the current Addressing API settings from this builder.

    returns

    A builder for an addressing UDF.

  19. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  20. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  21. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  22. def withDataLocations(dataLocations: String*): AddressingBuilder

    Add locations to addressing datasets.

    Add locations to addressing datasets. If a dataLocation is a remote path, e.g. HDFS or S3, then a suitable download manager instance must also be provided to the builder (see withDownloadManager). A dataLocation may be a single dataset (extracted or an non-extracted SPD), or a location with multiple datasets. Local paths must be present on all nodes that tasks will run on.

  23. def withDownloadManager(downloadManager: DownloadManager): AddressingBuilder

    Add a configured download manager to use when downloading from remote paths.

    Add a configured download manager to use when downloading from remote paths.

    downloadManager

    The download manager configured for required remote locations.

  24. def withExtractionLocation(extractionLocation: String): AddressingBuilder

    Specify an alternate extraction location for addressing datasets.

    Specify an alternate extraction location for addressing datasets. Default location is the alongside the SPD.

  25. def withResourcesLocation(resourcesLocation: String): AddressingBuilder

    Sets the resources location for the Addressing API.

    Sets the resources location for the Addressing API. If the location is a remote path, e.g. HDFS or S3, then a suitable download manager instance must also be provided to the builder (see withDownloadManager). If a local path is supplied, then it must be accessible on all nodes that tasks will run on.

    Required.

    resourcesLocation

    The resources location for the Addressing API.

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

    (Since version 9)

Inherited from AnyRef

Inherited from Any

Ungrouped