<jupyter_start><jupyter_text>1. Python 1.1. Python library<jupyter_code>import h3
h3.geo_to_h3(0, 0, 8)<jupyter_output><empty_output><jupyter_text>1.2. Python udf<jupyter_code>@F.udf(T.StringType())
def geo_to_h3_py(latitude, longitude, resolution):
    if latitude is None or longitude is None:
        return None
    
    return h3.geo_to_h3(latitude, longitude, resolution)
(
    sdf
    .withColumn("h8", geo_to_h3_py("latitude", "longitude", F.lit(8)))
    .show()
)<jupyter_output>+-------------------+-------------------+---------------+
|           latitude|          longitude|             h8|
+-------------------+-------------------+---------------+
|-21.312207074342027| -160.0670321180626|88b4ee4e09fffff|
|  50.19929821057201| 162.28091785280452|8816a9480bfffff|
|  41.31619353494014|-126.25339414248694|88280e8c45fffff|
|  80.74685363258044| -146.7956606121309|880385740bfffff|
| 61.132119913605635| 3.9118133429158206|8809aad433fffff|
|   78.3401760559614|-106.11650754917403|880203b86dfffff|
| -58.54991587056003| 113.45900052031868|88e5a820d5fffff|
| -42.03748505685331|-161.50714721056474|88d4c6d5c3fffff|
|   68.6213813476812|  19.86834445821239|8808e89a8dfffff|
|  59.85531571059457|   5.23330694280304|8809802217fffff|
|-23.319622712468345|-21.532138804100356|88a4463b33fffff|
|  -33.4208833127299|-3.1036409270136858|88c15ba897fffff|
|  64.17563826776947| -9.793711461081045|88076481c1fffff|
| 21.269421204176865|-44.992039917657564|8857b671b7fffff|
|  36.02039510[...]<jupyter_text>2. Java 2.1. Java function<jupyter_code># Using h3 library
h3 = spark.sparkContext._jvm.com.uber.h3core.H3Core.newInstance()
h3.geoToH3Address(0.0, 0.0, 8)
# Using our static function
spark.sparkContext._jvm.com.villoro.SimpleH3.toH3Address(0.0, 0.0, 8)<jupyter_output><empty_output><jupyter_text>2.2. Java udf<jupyter_code>spark.udf.registerJavaFunction("geo_to_h3", "com.villoro.toH3AddressUDF", T.StringType())
(
    sdf
    .withColumn("h8", F.expr("geo_to_h3(latitude, longitude, 8)"))
    .show()
)<jupyter_output>+-------------------+-------------------+---------------+
|           latitude|          longitude|             h8|
+-------------------+-------------------+---------------+
|-21.312207074342027| -160.0670321180626|88b4ee4e09fffff|
|  50.19929821057201| 162.28091785280452|8816a9480bfffff|
|  41.31619353494014|-126.25339414248694|88280e8c45fffff|
|  80.74685363258044| -146.7956606121309|880385740bfffff|
| 61.132119913605635| 3.9118133429158206|8809aad433fffff|
|   78.3401760559614|-106.11650754917403|880203b86dfffff|
| -58.54991587056003| 113.45900052031868|88e5a820d5fffff|
| -42.03748505685331|-161.50714721056474|88d4c6d5c3fffff|
|   68.6213813476812|  19.86834445821239|8808e89a8dfffff|
|  59.85531571059457|   5.23330694280304|8809802217fffff|
|-23.319622712468345|-21.532138804100356|88a4463b33fffff|
|  -33.4208833127299|-3.1036409270136858|88c15ba897fffff|
|  64.17563826776947| -9.793711461081045|88076481c1fffff|
| 21.269421204176865|-44.992039917657564|8857b671b7fffff|
|  36.02039510[...]
{'fraction_non_alphanumeric': 0.19689973614775724, 'fraction_numerical': 0.39478891820580475, 'mean_word_length': 3.7839116719242902, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '374702', 'license': 'MIT', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1355, 'n_words': 118, 'path': '0034-pyspark_java_udf/Exploring udfs.ipynb', 'repo_name': 'villoro/villoro_posts', 'size': '3032'}