You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# helper_module.py# From the Python Standard Libraryimportstruct# From PySparkimportpyspark.sql.functionsasFimportpyspark.sql.typesasTfrompyspark.sqlimportDataFramedefstr_hex_to_numeric(
hex_value: str,
data_type_name: str
) ->float:
"""Convert a hex string to a numeric value."""ifdata_type_name=="Float":
returnstruct.unpack('!f', bytes.fromhex(hex_value))[0]
raiseValueError(f"Unknown data type: {data_type_name}")
defvalue_col_hex_to_numeric(
df: DataFrame,
value_col: str="VALUE",
data_type_name_col: str="DATA_TYPE_NAME"
) ->DataFrame:
"""Convert a hex string to a numeric value."""returndf.withColumn(
value_col,
F.udf(
str_hex_to_numeric, T.FloatType()
)(F.col(value_col), F.col(data_type_name_col))
)
# notebook.ipynb# Navigate to the repo root directory and install the helper module%pipinstall-e .
# Import the helper modulefromhelperimporthelper_module# Create a Spark DataFramedf=spark.createDataFrame([("1", "Float", "3f800000"), ("2", "Float", "40000000"),
("3", "Float", "40400000"), ("4", "Float", "40800000")],
["INDEX", "DATA_TYPE_NAME", "VALUE"])
# Convert the hex string to a numeric valuedf=helper_module.value_col_hex_to_numeric(df)
# Display the DataFramedf.show()
# -- Databricks Connect returns the following error --# ModuleNotFoundError: No module named 'helper'# # -- While Azure Databricks returns the expected output --# +-----+--------------+----------+# |INDEX|DATA_TYPE_NAME| VALUE|# +-----+--------------+----------+# | 1| Float| 1.0|# | 2| Float| 2.0|# | 3| Float| 3.0|# | 4| Float| 4.0|# +-----+--------------+----------+
The text was updated successfully, but these errors were encountered:
The only solution I've found to this point is to package up the helper in a wheel and install the wheel on the cluster. And then run my notebook from the databricks workspace rather than vscode.
System information
Code structure
Code sample
The text was updated successfully, but these errors were encountered: