pyspark

pyspark 的相关函数

PySpark SQL provides several built-in standard functions pyspark.sql.functions to work with DataFrame and SQL queries. All these PySpark Functions return pyspark.sql.Column type.

get_json_object
复制代码
  /**
   * Extracts json object from a json string based on json path specified, and returns json string
   * of the extracted json object. It will return null if the input json string is invalid.
   *
   * @group json_funcs
   * @since 1.6.0
   */
  def get_json_object(e: Column, path: String): Column =
    Column.fn("get_json_object", e, lit(path))

以下可以看出 python 的函数实现,复用的是jvm 的那套实现

复制代码
@_try_remote_functions
def get_json_object(col: "ColumnOrName", path: str) -> Column:
    """
    Extracts json object from a json string based on json `path` specified, and returns json string
    of the extracted json object. It will return null if the input json string is invalid.

    .. versionadded:: 1.6.0

    .. versionchanged:: 3.4.0
        Supports Spark Connect.

    Parameters
    ----------
    col : :class:`~pyspark.sql.Column` or str
        string column in json format
    path : str
        path to the json object to extract

    Returns
    -------
    :class:`~pyspark.sql.Column`
        string representation of given JSON object value.

    Examples
    --------
    >>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), ("2", '''{"f1": "value12"}''')]
    >>> df = spark.createDataFrame(data, ("key", "jstring"))
    >>> df.select(df.key, get_json_object(df.jstring, '$.f1').alias("c0"), \\
    ...                   get_json_object(df.jstring, '$.f2').alias("c1") ).collect()
    [Row(key='1', c0='value1', c1='value2'), Row(key='2', c0='value12', c1=None)]
    """
    from pyspark.sql.classic.column import _to_java_column

    return _invoke_function("get_json_object", _to_java_column(col), path)


   def _invoke_function(name: str, *args: Any) -> Column:
    """
    Invokes JVM function identified by name with args
    and wraps the result with :class:`~pyspark.sql.Column`.
    """
    from pyspark import SparkContext

    assert SparkContext._active_spark_context is not None
    jf = _get_jvm_function(name, SparkContext._active_spark_context)
    return Column(jf(*args))

def _get_jvm_function(name: str, sc: "SparkContext") -> Callable:
    """
    Retrieves JVM function identified by name from
    Java gateway associated with sc.
    """
    assert sc._jvm is not None
    return getattr(getattr(sc._jvm, "org.apache.spark.sql.functions"), name)

def _to_java_column(col: "ColumnOrName") -> "JavaObject":
    if isinstance(col, Column):
        jcol = col._jc
    elif isinstance(col, str):
        jcol = _create_column_from_name(col)
    else:
        raise PySparkTypeError(
            error_class="NOT_COLUMN_OR_STR",
            message_parameters={"arg_name": "col", "arg_type": type(col).__name__},
        )
    return jcol

FQA

复制代码
ModuleNotFoundError: No module named 'numpy'

遇到该问题, 先看看 pyspark 使用的 python 版本

复制代码
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /__ / .__/\_,_/_/ /_/\_\   version 3.3.2
      /_/

Using Python version 3.9.9 (main, Mar  3 2024 19:54:45)
Spark context Web UI available at http://172.17.48.107:4040
Spark context available as 'sc' (master = local[10], app id = local-1719386128955).
SparkSession available as 'spark'.

然后使用响应的 python 版本 pip去安装 numpy, 否则安装了也 识别不了

复制代码
pip3.9 install numpy
相关推荐
大厂技术总监下海1 天前
从Hadoop MapReduce到Apache Spark:一场由“磁盘”到“内存”的速度与范式革命
大数据·hadoop·spark·开源
麦麦大数据1 天前
F052pro 基于spark推荐的中医古籍知识图谱可视化推荐系统|spark mlib|hadoop|docker集群
docker·spark-ml·spark·知识图谱·可是还·中医推荐·ehcarts
巧克力味的桃子2 天前
Spark 课程核心知识点复习汇总
大数据·分布式·spark
Light602 天前
智能重构人货场:领码SPARK破解快消行业增长困局的全景解决方案
spark·数字化转型·ai大模型·智能营销·快消行业·供应链优化
叫我:松哥2 天前
基于大数据和深度学习的智能空气质量监测与预测平台,采用Spark数据预处理,利用TensorFlow构建LSTM深度学习模型
大数据·python·深度学习·机器学习·spark·flask·lstm
火龙谷3 天前
day1-部署集群
spark
火龙谷3 天前
day3-构建数仓
spark
阿里云大数据AI技术4 天前
迅雷基于阿里云 EMR Serverless Spark 实现数仓资源效率与业务提升
spark
伟大的大威4 天前
在 NVIDIA DGX Spark部署 Stable Diffusion 3.5 并使用ComfyUI
stable diffusion·spark·comfyui
叫我:松哥4 天前
基于Spark智能推荐算法的农业作物推荐系统,推荐算法使用Spark ML风格推荐引擎
大数据·python·机器学习·spark-ml·spark·flask·推荐算法