Spark上的SQL:如何获取DISTINCT的所有值



所以,假设我有下表:

Name | Color
------------------------------
John | Blue
Greg | Red
John | Yellow
Greg | Red
Greg | Blue

我想得到每个名称的不同颜色表 - 多少及其值。意思是,像这样:

Name | Distinct | Values
--------------------------------------
John |   2      | Blue, Yellow
Greg |   2      | Red, Blue

有什么想法吗?

collect_list会给你一个列表,而不会删除重复项。collect_set将自动删除重复项所以只是

select 
Name,
count(distinct color) as Distinct, # not a very good name
collect_set(Color) as Values
from TblName
group by Name

此功能从 Spark 1.6.0 开始实现,请查看:

https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/functions.scala

/**
   * Aggregate function: returns a set of objects with duplicate elements eliminated.
   *
   * For now this is an alias for the collect_set Hive UDAF.
   *
   * @group agg_funcs
   * @since 1.6.0
   */
  def collect_set(columnName: String): Column = collect_set(Column(columnName))

For PySPark;我来自R/Pandas背景,所以我实际上发现Spark数据帧更容易使用。

为此:

  1. 设置 Spark SQL 上下文
  2. 将文件读入数据帧
  3. 将数据帧注册为临时表
  4. 使用 SQL 语法直接查询
  5. 将结果另存为对象,输出到文件。做你的事

这是我为此创建的类:

class SQLspark():
def __init__(self, local_dir='./', hdfs_dir='/users/', master='local', appname='spark_app', spark_mem=2):
    self.local_dir = local_dir
    self.hdfs_dir = hdfs_dir
    self.master = master
    self.appname = appname
    self.spark_mem = int(spark_mem)
    self.conf = (SparkConf()
           .setMaster(self.master)
           .setAppName(self.appname)
           .set("spark.executor.memory", self.spark_mem))
    self.sc = SparkContext(conf=self.conf)
    self.sqlContext = SQLContext(self.sc)

def file_to_df(self, input_file):
    # import file as dataframe, all cols will be imported as strings
    df = self.sqlContext.read.format("com.databricks.spark.csv").option("header", "true").option("delimiter", "t").option("inferSchema", "true").load(input_file)
    # # cache df object to avoid rebuilding each time
    df.cache()
    # register as temp table for querying, use 'spark_df' as table name
    df.registerTempTable("spark_df")
    return df
# you also cast a spark dataframe as a pandas df
def sparkDf_to_pandasDf(self, input_df):
    pandas_df = input_df.toPandas()
    return pandas_df
def find_distinct(self, col_name):
    my_query = self.sqlContext.sql("""SELECT distinct {} FROM spark_df""".format(col_name))
   # now do your thing with the results etc
    my_query.show() 
    my_query.count()
    my_query.collect()
###############
if __name__ == '__main__':
# instantiate class 
# see function for variables to input
spark = TestETL(os.getcwd(), 'hdfs_loc', "local", "etl_test", 10)

# specify input file to process
tsv_infile = 'path/to/file'

相关内容

  • 没有找到相关文章

最新更新