Compartilhar via


string_agg_distinct

Função de agregação: retorna a concatenação de valores de entrada não nulos distintos, separados pelo delimitador. Um alias de listagg_distinct.

Sintaxe

import pyspark.sql.functions as sf

sf.string_agg_distinct(col=<col>)

# With delimiter
sf.string_agg_distinct(col=<col>, delimiter=<delimiter>)

Parâmetros

Parâmetro Tipo Description
col pyspark.sql.Column ou str Coluna de destino para computação ativada.
delimiter pyspark.sql.Column, str ou bytes Optional. O delimitador para separar os valores. O valor padrão é None.

Devoluções

pyspark.sql.Column: a coluna para resultados computados.

Exemplos

Exemplo 1: usando string_agg_distinct função.

import pyspark.sql.functions as sf
df = spark.createDataFrame([('a',), ('b',), (None,), ('c',), ('b',)], ['strings'])
df.select(sf.string_agg_distinct('strings')).show()
+----------------------------------+
|string_agg(DISTINCT strings, NULL)|
+----------------------------------+
|                               abc|
+----------------------------------+

Exemplo 2: usando string_agg_distinct função com um delimitador.

import pyspark.sql.functions as sf
df = spark.createDataFrame([('a',), ('b',), (None,), ('c',), ('b',)], ['strings'])
df.select(sf.string_agg_distinct('strings', ', ')).show()
+--------------------------------+
|string_agg(DISTINCT strings, , )|
+--------------------------------+
|                         a, b, c|
+--------------------------------+

Exemplo 3: usando string_agg_distinct função com uma coluna binária e um delimitador.

import pyspark.sql.functions as sf
df = spark.createDataFrame([(b'\x01',), (b'\x02',), (None,), (b'\x03',), (b'\x02',)],
                           ['bytes'])
df.select(sf.string_agg_distinct('bytes', b'\x42')).show()
+---------------------------------+
|string_agg(DISTINCT bytes, X'42')|
+---------------------------------+
|                 [01 42 02 42 03]|
+---------------------------------+

Exemplo 4: usando string_agg_distinct função em uma coluna com todos os valores None.

import pyspark.sql.functions as sf
from pyspark.sql.types import StructType, StructField, StringType
schema = StructType([StructField("strings", StringType(), True)])
df = spark.createDataFrame([(None,), (None,), (None,), (None,)], schema=schema)
df.select(sf.string_agg_distinct('strings')).show()
+----------------------------------+
|string_agg(DISTINCT strings, NULL)|
+----------------------------------+
|                              NULL|
+----------------------------------+