如何以avro格式查询数据集?

时间:2017-09-26 19:20:25

标签: apache-spark apache-spark-sql spark-avro

这适用于实木复合地板

 val sqlDF = spark.sql("SELECT DISTINCT field FROM parquet.`file-path'")

我尝试使用与Avro相同的方法,但即使我使用com.databricks.spark.avro,它仍然会给我一个错误。

当我执行以下查询时:

val sqlDF = spark.sql("SELECT DISTINCT Source_Product_Classification FROM avro.`file path`")

我得到AnalysisException。为什么呢?

org.apache.spark.sql.AnalysisException: Failed to find data source: avro. Please find an Avro package at http://spark.apache.org/third-party-projects.html;; line 1 pos 51
  at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:42)
  at org.apache.spark.sql.execution.datasources.ResolveDataSource$$anonfun$apply$1.applyOrElse(rules.scala:61)
  at org.apache.spark.sql.execution.datasources.ResolveDataSource$$anonfun$apply$1.applyOrElse(rules.scala:38)
  at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan$$anonfun$resolveOperators$1.apply(LogicalPlan.scala:61)
  at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan$$anonfun$resolveOperators$1.apply(LogicalPlan.scala:61)
  at org.apache.spark.sql.catalyst.trees.CurrentOrigin$.withOrigin(TreeNode.scala:70)
  at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.resolveOperators(LogicalPlan.scala:60)
  at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan$$anonfun$1.apply(LogicalPlan.scala:58)
  at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan$$anonfun$1.apply(LogicalPlan.scala:58)
  at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$4.apply(TreeNode.scala:307)
  at org.apache.spark.sql.catalyst.trees.TreeNode.mapProductIterator(TreeNode.scala:188)
  at org.apache.spark.sql.catalyst.trees.TreeNode.mapChildren(TreeNode.scala:305)
  at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.resolveOperators(LogicalPlan.scala:58)
  at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan$$anonfun$1.apply(LogicalPlan.scala:58)
  at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan$$anonfun$1.apply(LogicalPlan.scala:58)
  at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$4.apply(TreeNode.scala:307)
  at org.apache.spark.sql.catalyst.trees.TreeNode.mapProductIterator(TreeNode.scala:188)
  at org.apache.spark.sql.catalyst.trees.TreeNode.mapChildren(TreeNode.scala:305)
  at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.resolveOperators(LogicalPlan.scala:58)
  at org.apache.spark.sql.execution.datasources.ResolveDataSource.apply(rules.scala:38)
  at org.apache.spark.sql.execution.datasources.ResolveDataSource.apply(rules.scala:37)
  at org.apache.spark.sql.catalyst.rules.RuleExecutor$$anonfun$execute$1$$anonfun$apply$1.apply(RuleExecutor.scala:85)
  at org.apache.spark.sql.catalyst.rules.RuleExecutor$$anonfun$execute$1$$anonfun$apply$1.apply(RuleExecutor.scala:82)
  at scala.collection.LinearSeqOptimized$class.foldLeft(LinearSeqOptimized.scala:124)
  at scala.collection.immutable.List.foldLeft(List.scala:84)
  at org.apache.spark.sql.catalyst.rules.RuleExecutor$$anonfun$execute$1.apply(RuleExecutor.scala:82)
  at org.apache.spark.sql.catalyst.rules.RuleExecutor$$anonfun$execute$1.apply(RuleExecutor.scala:74)
  at scala.collection.immutable.List.foreach(List.scala:381)
  at org.apache.spark.sql.catalyst.rules.RuleExecutor.execute(RuleExecutor.scala:74)
  at org.apache.spark.sql.execution.QueryExecution.analyzed$lzycompute(QueryExecution.scala:69)
  at org.apache.spark.sql.execution.QueryExecution.analyzed(QueryExecution.scala:67)
  at org.apache.spark.sql.execution.QueryExecution.assertAnalyzed(QueryExecution.scala:50)
  at org.apache.spark.sql.Dataset$.ofRows(Dataset.scala:63)
  at org.apache.spark.sql.SparkSession.sql(SparkSession.scala:592)

将格式名称更改为com.databricks.spark.avro没有任何区别,查询失败。

val sqlDF = spark.sql("SELECT DISTINCT Source_Product_Classification FROM com.databricks.spark.avro`file-path`")

org.apache.spark.sql.catalyst.parser.ParseException:
extraneous input '.' expecting {<EOF>, ',', 'SELECT', 'FROM', 'ADD', 'AS', 'ALL', 'DISTINCT', 'WHERE', 'GROUP', 'BY', 'GROUPING', 'SETS', 'CUBE', 'ROLLUP', 'ORDER', 'HAVING', 'LIMIT', 'AT', 'OR', 'AND', 'IN', NOT, 'NO', 'EXISTS', 'BETWEEN', 'LIKE', RLIKE, 'IS', 'NULL', 'TRUE', 'FALSE', 'NULLS', 'ASC', 'DESC', 'FOR', 'INTERVAL', 'CASE', 'WHEN', 'THEN', 'ELSE', 'END', 'JOIN', 'CROSS', 'OUTER', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'NATURAL', 'LATERAL', 'WINDOW', 'OVER', 'PARTITION', 'RANGE', 'ROWS', 'UNBOUNDED', 'PRECEDING', 'FOLLOWING', 'CURRENT', 'FIRST', 'LAST', 'ROW', 'WITH', 'VALUES', 'CREATE', 'TABLE', 'VIEW', 'REPLACE', 'INSERT', 'DELETE', 'INTO', 'DESCRIBE', 'EXPLAIN', 'FORMAT', 'LOGICAL', 'CODEGEN', 'CAST', 'SHOW', 'TABLES', 'COLUMNS', 'COLUMN', 'USE', 'PARTITIONS', 'FUNCTIONS', 'DROP', 'UNION', 'EXCEPT', 'MINUS', 'INTERSECT', 'TO', 'TABLESAMPLE', 'STRATIFY', 'ALTER', 'RENAME', 'ARRAY', 'MAP', 'STRUCT', 'COMMENT', 'SET', 'RESET', 'DATA', 'START', 'TRANSACTION', 'COMMIT', 'ROLLBACK', 'MACRO', 'IF', 'DIV', 'PERCENT', 'BUCKET', 'OUT', 'OF', 'SORT', 'CLUSTER', 'DISTRIBUTE', 'OVERWRITE', 'TRANSFORM', 'REDUCE', 'USING', 'SERDE', 'SERDEPROPERTIES', 'RECORDREADER', 'RECORDWRITER', 'DELIMITED', 'FIELDS', 'TERMINATED', 'COLLECTION', 'ITEMS', 'KEYS', 'ESCAPED', 'LINES', 'SEPARATED', 'FUNCTION', 'EXTENDED', 'REFRESH', 'CLEAR', 'CACHE', 'UNCACHE', 'LAZY', 'FORMATTED', 'GLOBAL', TEMPORARY, 'OPTIONS', 'UNSET', 'TBLPROPERTIES', 'DBPROPERTIES', 'BUCKETS', 'SKEWED', 'STORED', 'DIRECTORIES', 'LOCATION', 'EXCHANGE', 'ARCHIVE', 'UNARCHIVE', 'FILEFORMAT', 'TOUCH', 'COMPACT', 'CONCATENATE', 'CHANGE', 'CASCADE', 'RESTRICT', 'CLUSTERED', 'SORTED', 'PURGE', 'INPUTFORMAT', 'OUTPUTFORMAT', DATABASE, DATABASES, 'DFS', 'TRUNCATE', 'ANALYZE', 'COMPUTE', 'LIST', 'STATISTICS', 'PARTITIONED', 'EXTERNAL', 'DEFINED', 'REVOKE', 'GRANT', 'LOCK', 'UNLOCK', 'MSCK', 'REPAIR', 'RECOVER', 'EXPORT', 'IMPORT', 'LOAD', 'ROLE', 'ROLES', 'COMPACTIONS', 'PRINCIPALS', 'TRANSACTIONS', 'INDEX', 'INDEXES', 'LOCKS', 'OPTION', 'ANTI', 'LOCAL', 'INPATH', 'CURRENT_DATE', 'CURRENT_TIMESTAMP', IDENTIFIER, BACKQUOTED_IDENTIFIER}(line 1, pos 65)

== SQL ==
SELECT DISTINCT Source_Product_Classification FROM com.databricks.spark.avro`/uat/myfile`
-----------------------------------------------------------------^^^

  at org.apache.spark.sql.catalyst.parser.ParseException.withCommand(ParseDriver.scala:197)
  at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parse(ParseDriver.scala:99)
  at org.apache.spark.sql.execution.SparkSqlParser.parse(SparkSqlParser.scala:45)
  at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parsePlan(ParseDriver.scala:53)
  at org.apache.spark.sql.SparkSession.sql(SparkSession.scala:592)
  ... 48 elided

3 个答案:

答案 0 :(得分:4)

Spark SQL通过单独的spark-avro模块支持avro格式。

  

用于从Spark SQL读取和编写Avro数据的库。

请注意spark-avro是一个seaprate模块,默认情况下不包含在Spark中。

您应该使用spark-submit --packages加载模块,例如

$ bin/spark-shell --packages com.databricks:spark-avro_2.11:3.2.0

请参阅With spark-shell or spark-submit

答案 1 :(得分:2)

Jaceks回答总体上是有效的,但是在我的环境中,由于晦涩的原因,它无法正常工作。而spark-shell --packages com.databricks:spark-avro_2.11:3.2.0挂了很长时间却没有任何结果。

我使用--jars选项和spark-shell来解决了这个问题

步骤:

1)转到https://mvnrepository.com/artifact/com.databricks/spark-avro_2.11/4.0.0 复制jar http://central.maven.org/maven2/com/databricks/spark-avro_2.11/4.0.0/spark-avro_2.11-4.0.0.jar的链接地址

2)wget http://central.maven.org/maven2/com/databricks/spark-avro_2.11/4.0.0/spark-avro_2.11-4.0.0.jar

3)spark-shell --jars <pathwhere you downloaded jar file>/spark-avro_2.11-4.0.0.jar

4)spark.read.format("com.databricks.spark.avro").load("s3://MYAVROLOCATION.avro")

已转换为数据框并能够打印。

就您而言,一旦获得数据框,就可以按自己的方式执行sql。

注意: :如果您不使用spark-shell,则可以使用sbt或ubuntu使用spark-avro_2.11-4.0.0.jar来制作uber jar。行家坐标。

<dependency>
    <groupId>com.databricks</groupId>
    <artifactId>spark-avro_2.11</artifactId>
    <version>4.0.0</version>
</dependency>
  

注意:Avro数据源是在病房的Spark 2.4中引入的。.SparkSPARK-24768 Have a built-in AVRO data source implementation

     

这意味着以上所有内容不再是必需的。   参见spark-release-2-4-0 release notes

答案 2 :(得分:1)

Spark Avro集成: 通过使用Spark,我们可以使用 spark-avro 模块集成 avro 格式。 spark-avro 库最初由databricks作为开放源代码库开发。 spark-avro 模块是外部模块,默认情况下不包含在 spark-submit spark-shell 中。因此,在外部,我们需要在提交Spark作业时指定。

在以下部分中,我将说明如何集成Spark和Avro数据格式。

火花版本> 2.4 从Spark 2.4版本开始,Spark SQL为读取和写入Apache Avro数据提供了内置支持。

Maven依赖项: https://mvnrepository.com/artifact/org.apache.spark/spark-avro

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-avro_2.12</artifactId>
    <version>2.4.5</version>
</dependency>

火花提交:

./bin/spark-submit --packages org.apache.spark:spark-avro_2.12:2.4.5 ...

SparkShell:

./bin/spark-shell --packages org.apache.spark:spark-avro_2.12:2.4.5 ...

示例:

SparkAvroWriteExample.scala

import org.apache.spark.SparkConf;
import org.apache.spark.sql.SparkSession;

case class Employee( id:Long, name:String, salary:Float, deptId: Int)

object SparkAvroWriteExample {
    def main(args: Array[String]): Unit = {

        val conf = new SparkConf().setIfMissing("spark.master", "local[*]").setAppName("Spark Avro Read Examples")
        val spark = SparkSession.builder().config(conf).getOrCreate();

        val employeeList = List(Employee(1, "Ranga", 10000, 1),
            Employee(2, "Vinod", 1000, 1),
            Employee(3, "Nishanth", 500000, 2),
            Employee(4, "Manoj", 25000, 1),
            Employee(5, "Yashu", 1600, 1),
            Employee(6, "Raja", 50000, 2)
        );

        val employeeDF = spark.createDataFrame(employeeList);
        employeeDF.coalesce(1).write.format("avro").mode("overwrite").save("employees.avro");
     spark.close();
    }
}

SparkAvroReadExample.scala

import org.apache.spark.SparkConf;
import org.apache.spark.sql.SparkSession;

object SparkAvroReadExample {
    def main(args: Array[String]): Unit = {

        val conf = new SparkConf().setIfMissing("spark.master", "local[*]").setAppName("Spark Avro Read Examples")
        val spark = SparkSession.builder().config(conf).getOrCreate();

        val employeeDF = spark.read.format("avro").load("employees.avro");
        employeeDF.printSchema();
        employeeDF.foreach(employee => {println(employee);});
        spark.close();
    }
}

Github链接 https://github.com/rangareddy/ranga-spark-poc/tree/master/spark-2.4/SparkAvro

火花版本<2.4 在Spark版本<2.4中,显式地,我们需要将avro格式指定为 com.databricks.spark.avro ,否则,我们将获得 org.apache.spark.sql.AnalysisException:无法找到数据源:avro。错误。

Maven依赖项:

Spark Version   Compatible version of Avro Data Source for Spark
1.2             0.2.0
1.3             1.0.0
1.4+            2.0.1
2.0 - 2.1       3.2.0
2.2 - 2.3       4.0.0

https://mvnrepository.com/artifact/com.databricks/spark-avro

<dependency>
    <groupId>com.databricks</groupId>
    <artifactId>spark-avro_2.11</artifactId>
    <version>4.0.0</version>
</dependency>

火花提交:

./bin/spark-submit --packages com.databricks:spark-avro_2.11:4.0.0 ...

SparkShell:

./bin/spark-shell --packages com.databricks:spark-avro_2.11:4.0.0 ...

示例

SparkAvroWriteExample.scala

import org.apache.spark.SparkConf;
import org.apache.spark.sql.SparkSession;

case class Employee( id:Long, name:String, salary:Float, deptId: Int)

object SparkAvroWriteExample {
    def main(args: Array[String]): Unit = {

        val conf = new SparkConf().setIfMissing("spark.master", "local[*]").setAppName("Spark Avro Read Examples")
        val spark = SparkSession.builder().config(conf).getOrCreate();

        val employeeList = List(Employee(1, "Ranga", 10000, 1),
            Employee(2, "Vinod", 1000, 1),
            Employee(3, "Nishanth", 500000, 2),
            Employee(4, "Manoj", 25000, 1),
            Employee(5, "Yashu", 1600, 1),
            Employee(6, "Raja", 50000, 2)
        );

        val employeeDF = spark.createDataFrame(employeeList);
        employeeDF.coalesce(1).write.format("com.databricks.spark.avro").mode("overwrite").save("employees.avro");
        spark.close();
    }
}

SparkAvroReadExample.scala

import org.apache.spark.SparkConf;
import org.apache.spark.sql.SparkSession;

object SparkAvroReadExample {
    def main(args: Array[String]): Unit = {

        val conf = new SparkConf().setIfMissing("spark.master", "local[*]").setAppName("Spark Avro Read Examples")
        val spark = SparkSession.builder().config(conf).getOrCreate();

        val employeeDF = spark.read.format("com.databricks.spark.avro").load("employees.avro");
        employeeDF.printSchema();
        employeeDF.foreach(employee => {println(employee);});
        spark.close();
    }
}

Github链接 https://github.com/rangareddy/ranga-spark-poc/tree/master/spark-2.3/SparkAvro

那是所有人!