• JavaSpark-sparkSQL


    入门 
    起点:SparkSession 
    Spark中所有功能的入口点就是这个SparkSession类。要创建一个基本的SparkSession,只需使用SparkSession.builder():

    1.  
      import org.apache.spark.sql.SparkSession;
    2.  
       
    3.  
      SparkSession spark = SparkSession
    4.  
      .builder()
    5.  
      .appName("Java Spark SQL basic example")
    6.  
      .config("spark.some.config.option", "some-value")
    7.  
      .getOrCreate();

    SparkSession在Spark 2.0中为Hive特性提供内置支持,包括使用HiveQL编写查询,访问Hive UDF以及从Hive表读取数据的能力。要使用这些功能,您不需要有现有的Hive安装程序。 
    创建数据框 
    使用一个 SparkSession,应用程序可以从现有的RDD,Hive表或Spark数据源创建DataFrame 。

    1.  
       
    2.  
      Dataset<Row> df = spark.read().json("examples/src/main/resources/people.json");
    3.  
       
    4.  
      // Displays the content of the DataFrame to stdout
    5.  
      df.show();
    6.  
      // +----+-------+
    7.  
      // | age| name|
    8.  
      // +----+-------+
    9.  
      // |null|Michael|
    10.  
      // | 30| Andy|
    11.  
      // | 19| Justin|
    12.  
      // +----+-------+

    无类型数据集操作(又名DataFrame操作) 
    DataFrames为Scala,Java,Python和R中的结构化数据操作提供了一个特定领域的语言。 
    在Spark 2.0中,DataFrames只是RowScala和Java API中的数据集。这些操作也被称为“无类型转换”,与强类型的Scala / Java数据集中的“类型转换”不同。 
    一些使用数据集的结构化数据处理的基本示例:

    1.  
       
    2.  
      Dataset<Row> df = spark.read().json("examples/src/main/resources/people.json");
    3.  
       
    4.  
      // Displays the content of the DataFrame to stdout
    5.  
      df.show();
    6.  
      // +----+-------+
    7.  
      // | age| name|
    8.  
      // +----+-------+
    9.  
      // |null|Michael|
    10.  
      // | 30| Andy|
    11.  
      // | 19| Justin|
    12.  
      // +----+-------+

    除了简单的列引用和表达式之外,数据集还具有丰富的函数库,包括字符串操作,日期算术,通用数学运算等等。DataFrame函数参考中提供了完整的列表。 
    以编程方式运行SQL查询 
    SparkSession上的sql函数允许应用程序能以编程方式运行SQL查询,并将结果返回为Dataset<Row>

    1.  
       
    2.  
       
    3.  
      // Register the DataFrame as a SQL temporary view
    4.  
      df.createOrReplaceTempView("people");
    5.  
       
    6.  
      Dataset<Row> sqlDF = spark.sql("SELECT * FROM people");
    7.  
      sqlDF.show();
    8.  
      // +----+-------+
    9.  
      // | age| name|
    10.  
      // +----+-------+
    11.  
      // |null|Michael|
    12.  
      // | 30| Andy|
    13.  
      // | 19| Justin|
    14.  
      // +----+-------+
    15.  
       

    全局临时视图 
    Spark SQL中的临时视图是会话范围的,如果创建它的会话终止,将会消失。如果您希望在所有会话之间共享一个临时视图并保持活动状态,直到Spark应用程序终止,则可以创建一个全局临时视图。全局临时视图与系统保存的数据库绑定global_temp,我们必须使用限定的名称来引用它,例如SELECT * FROM global_temp.view1

    1.  
      // Register the DataFrame as a global temporary view
    2.  
      df.createGlobalTempView("people");
    3.  
       
    4.  
      // Global temporary view is tied to a system preserved database `global_temp`
    5.  
      spark.sql("SELECT * FROM global_temp.people").show();
    6.  
      // +----+-------+
    7.  
      // | age| name|
    8.  
      // +----+-------+
    9.  
      // |null|Michael|
    10.  
      // | 30| Andy|
    11.  
      // | 19| Justin|
    12.  
      // +----+-------+
    13.  
       
    14.  
      // Global temporary view is cross-session跨会话
    15.  
      spark.newSession().sql("SELECT * FROM global_temp.people").show();
    16.  
      // +----+-------+
    17.  
      // | age| name|
    18.  
      // +----+-------+
    19.  
      // |null|Michael|
    20.  
      // | 30| Andy|
    21.  
      // | 19| Justin|
    22.  
      // +----+-------+

    创建数据集 
    数据集类似于RDD,但是,不使用Java序列化或Kryo,而是使用专门的编码器对对象进行序列化以便通过网络进行处理或传输。虽然编码器和标准序列化都负责将对象转换为字节,但编码器是动态生成的代码,并且使用Spark运行执行的操作(如过滤,排序和散列)格式,而无需将字节反序列化回对象。

    1.  
       
    2.  
       
    3.  
      public static class Person implements Serializable {
    4.  
      private String name;
    5.  
      private int age;
    6.  
       
    7.  
      public String getName() {
    8.  
      return name;
    9.  
      }
    10.  
       
    11.  
      public void setName(String name) {
    12.  
      this.name = name;
    13.  
      }
    14.  
       
    15.  
      public int getAge() {
    16.  
      return age;
    17.  
      }
    18.  
       
    19.  
      public void setAge(int age) {
    20.  
      this.age = age;
    21.  
      }
    22.  
      }
    23.  
       
    24.  
      // Create an instance of a Bean class
    25.  
      Person person = new Person();
    26.  
      person.setName("Andy");
    27.  
      person.setAge(32);
    28.  
       
    29.  
      // Encoders are created for Java beans
    30.  
      Encoder<Person> personEncoder = Encoders.bean(Person.class);
    31.  
      Dataset<Person> javaBeanDS = spark.createDataset(
    32.  
      Collections.singletonList(person),
    33.  
      personEncoder
    34.  
      );
    35.  
      javaBeanDS.show();
    36.  
      // +---+----+
    37.  
      // |age|name|
    38.  
      // +---+----+
    39.  
      // | 32|Andy|
    40.  
      // +---+----+
    41.  
       
    42.  
      // Encoders for most common types are provided in class Encoders
    43.  
      //编码器编码
    44.  
       
    45.  
      Encoder<Integer> integerEncoder = Encoders.INT();
    46.  
      Dataset<Integer> primitiveDS = spark.createDataset(Arrays.asList(1, 2, 3), integerEncoder);
    47.  
      Dataset<Integer> transformedDS = primitiveDS.map(
    48.  
      (MapFunction<Integer, Integer>) value -> value + 1,
    49.  
      integerEncoder);
    50.  
      transformedDS.collect(); // Returns [2, 3, 4]
    51.  
       
    52.  
      // DataFrames can be converted to a Dataset by providing a class. Mapping based on name
    53.  
      String path = "examples/src/main/resources/people.json";
    54.  
      Dataset<Person> peopleDS = spark.read().json(path).as(personEncoder);
    55.  
      peopleDS.show();
    56.  
      // +----+-------+
    57.  
      // | age| name|
    58.  
      // +----+-------+
    59.  
      // |null|Michael|
    60.  
      // | 30| Andy|
    61.  
      // | 19| Justin|
    62.  
      // +----+-------+
    63.  
       
    64.  
       

    与RDD进行互操作 
    Spark SQL支持将现有RDD转换为Datasets的两种不同方法。第一种方法使用反射来推断包含特定类型对象的RDD的模式。 
    创建数据集的第二种方法是通过编程接口,允许您构建模式,然后将其应用于现有的RDD。虽然这个方法比较冗长,但是它允许你在构造数据集的时候直到运行时才知道列和它们的类型。

    Spark SQL支持自动将JavaBean的RDD 转换为DataFrame 
    Spark SQL不支持包含Map字段的JavaBean 。不过嵌套的JavaBean和Listor Array 字段是受支持的。您可以通过创建一个实现Serializable的类来创建JavaBean,并为其所有字段设置getter和setter。

    1.  
      // Create an RDD of Person objects from a text file
    2.  
      JavaRDD<Person> peopleRDD = spark.read()
    3.  
      .textFile("examples/src/main/resources/people.txt")
    4.  
      .javaRDD()
    5.  
      .map(line -> {
    6.  
      String[] parts = line.split(",");
    7.  
      Person person = new Person();
    8.  
      person.setName(parts[0]);
    9.  
      person.setAge(Integer.parseInt(parts[1].trim()));
    10.  
      return person;
    11.  
      });
    12.  
       
    13.  
      // Apply a schema to an RDD of JavaBeans to get a DataFrame
    14.  
      Dataset<Row> peopleDF = spark.createDataFrame(peopleRDD, Person.class);
    15.  
      // Register the DataFrame as a temporary view
    16.  
      peopleDF.createOrReplaceTempView("people");
    17.  
       
    18.  
      // SQL statements can be run by using the sql methods provided by spark
    19.  
      Dataset<Row> teenagersDF = spark.sql("SELECT name FROM people WHERE age BETWEEN 13 AND 19");
    20.  
       
    21.  
      // The columns of a row in the result can be accessed by field index
    22.  
      Encoder<String> stringEncoder = Encoders.STRING();
    23.  
      Dataset<String> teenagerNamesByIndexDF = teenagersDF.map(
    24.  
      (MapFunction<Row, String>) row -> "Name: " + row.getString(0),
    25.  
      stringEncoder);
    26.  
      teenagerNamesByIndexDF.show();
    27.  
      // +------------+
    28.  
      // | value|
    29.  
      // +------------+
    30.  
      // |Name: Justin|
    31.  
      // +------------+
    32.  
       
    33.  
      // or by field name
    34.  
      Dataset<String> teenagerNamesByFieldDF = teenagersDF.map(
    35.  
      (MapFunction<Row, String>) row -> "Name: " + row.<String>getAs("name"),
    36.  
      stringEncoder);
    37.  
      teenagerNamesByFieldDF.show();
    38.  
      // +------------+
    39.  
      // | value|
    40.  
      // +------------+
    41.  
      // |Name: Justin|
    42.  
      // +------------+

    以编程方式指定模式 
    当不能提前定义JavaBean类时(例如,记录的结构是用字符串编码的,或者文本数据集将被解析,字段对于不同的用户来说投影会不同),Dataset<Row>可以用三个步骤以编程方式创建 。

    Row从原RDD 创建一个RDD; 
    在步骤1创建的RDD中StructType与Rows 结构匹配 的模式。 
    Row通过SparkSession中的createDataFrame提供的方法将模式应用于 的RDD

    1.  
      // Create an RDD
    2.  
      JavaRDD<String> peopleRDD = spark.sparkContext()
    3.  
      .textFile("examples/src/main/resources/people.txt", 1)
    4.  
      .toJavaRDD();
    5.  
       
    6.  
      // The schema is encoded in a string
    7.  
      String schemaString = "name age";
    8.  
       
    9.  
      // Generate the schema based on the string of schema
    10.  
      List<StructField> fields = new ArrayList<>();
    11.  
      for (String fieldName : schemaString.split(" ")) {
    12.  
      StructField field = DataTypes.createStructField(fieldName, DataTypes.StringType, true);
    13.  
      fields.add(field);
    14.  
      }
    15.  
      StructType schema = DataTypes.createStructType(fields);
    16.  
       
    17.  
      // Convert records of the RDD (people) to Rows
    18.  
      JavaRDD<Row> rowRDD = peopleRDD.map((Function<String, Row>) record -> {
    19.  
      String[] attributes = record.split(",");
    20.  
      return RowFactory.create(attributes[0], attributes[1].trim());
    21.  
      });
    22.  
       
    23.  
      // Apply the schema to the RDD
    24.  
      Dataset<Row> peopleDataFrame = spark.createDataFrame(rowRDD, schema);
    25.  
       
    26.  
      // Creates a temporary view using the DataFrame
    27.  
      peopleDataFrame.createOrReplaceTempView("people");
    28.  
       
    29.  
      // SQL can be run over a temporary view created using DataFrames
    30.  
      Dataset<Row> results = spark.sql("SELECT name FROM people");
    31.  
       
    32.  
      // The results of SQL queries are DataFrames and support all the normal RDD operations
    33.  
      // The columns of a row in the result can be accessed by field index or by field name
    34.  
      Dataset<String> namesDS = results.map(
    35.  
      (MapFunction<Row, String>) row -> "Name: " + row.getString(0),
    36.  
      Encoders.STRING());
    37.  
      namesDS.show();
    38.  
      // +-------------+
    39.  
      // | value|
    40.  
      // +-------------+
    41.  
      // |Name: Michael|
    42.  
      // | Name: Andy|
    43.  
      // | Name: Justin|
    44.  
      // +-------------+

    聚合 
    该内置功能DataFrames提供聚合,例如count(),countDistinct(),avg(),max(),min(),等。这些功能是专为DataFrames设计 
    非类型化的用户定义的聚合函数

    1.  
      public static class MyAverage extends UserDefinedAggregateFunction {
    2.  
       
    3.  
      private StructType inputSchema;
    4.  
      private StructType bufferSchema;
    5.  
       
    6.  
      public MyAverage() {
    7.  
      List<StructField> inputFields = new ArrayList<>();
    8.  
      inputFields.add(DataTypes.createStructField("inputColumn", DataTypes.LongType, true));
    9.  
      inputSchema = DataTypes.createStructType(inputFields);
    10.  
       
    11.  
      List<StructField> bufferFields = new ArrayList<>();
    12.  
      bufferFields.add(DataTypes.createStructField("sum", DataTypes.LongType, true));
    13.  
      bufferFields.add(DataTypes.createStructField("count", DataTypes.LongType, true));
    14.  
      bufferSchema = DataTypes.createStructType(bufferFields);
    15.  
      }
    16.  
      // Data types of input arguments of this aggregate function
    17.  
      public StructType inputSchema() {
    18.  
      return inputSchema;
    19.  
      }
    20.  
      // Data types of values in the aggregation buffer
    21.  
      public StructType bufferSchema() {
    22.  
      return bufferSchema;
    23.  
      }
    24.  
      // The data type of the returned value
    25.  
      public DataType dataType() {
    26.  
      return DataTypes.DoubleType;
    27.  
      }
    28.  
      // Whether this function always returns the same output on the identical input
    29.  
      public boolean deterministic() {
    30.  
      return true;
    31.  
      }
    32.  
      // Initializes the given aggregation buffer. The buffer itself is a `Row` that in addition to
    33.  
      // standard methods like retrieving a value at an index (e.g., get(), getBoolean()), provides
    34.  
      // the opportunity to update its values. Note that arrays and maps inside the buffer are still
    35.  
      // immutable.
    36.  
      public void initialize(MutableAggregationBuffer buffer) {
    37.  
      buffer.update(0, 0L);
    38.  
      buffer.update(1, 0L);
    39.  
      }
    40.  
      // Updates the given aggregation buffer `buffer` with new input data from `input`
    41.  
      public void update(MutableAggregationBuffer buffer, Row input) {
    42.  
      if (!input.isNullAt(0)) {
    43.  
      long updatedSum = buffer.getLong(0) + input.getLong(0);
    44.  
      long updatedCount = buffer.getLong(1) + 1;
    45.  
      buffer.update(0, updatedSum);
    46.  
      buffer.update(1, updatedCount);
    47.  
      }
    48.  
      }
    49.  
      // Merges two aggregation buffers and stores the updated buffer values back to `buffer1`
    50.  
      public void merge(MutableAggregationBuffer buffer1, Row buffer2) {
    51.  
      long mergedSum = buffer1.getLong(0) + buffer2.getLong(0);
    52.  
      long mergedCount = buffer1.getLong(1) + buffer2.getLong(1);
    53.  
      buffer1.update(0, mergedSum);
    54.  
      buffer1.update(1, mergedCount);
    55.  
      }
    56.  
      // Calculates the final result
    57.  
      public Double evaluate(Row buffer) {
    58.  
      return ((double) buffer.getLong(0)) / buffer.getLong(1);
    59.  
      }
    60.  
      }
    61.  
       
    62.  
      // Register the function to access it
    63.  
      spark.udf().register("myAverage", new MyAverage());
    64.  
       
    65.  
      Dataset<Row> df = spark.read().json("examples/src/main/resources/employees.json");
    66.  
      df.createOrReplaceTempView("employees");
    67.  
      df.show();
    68.  
      // +-------+------+
    69.  
      // | name|salary|
    70.  
      // +-------+------+
    71.  
      // |Michael| 3000|
    72.  
      // | Andy| 4500|
    73.  
      // | Justin| 3500|
    74.  
      // | Berta| 4000|
    75.  
      // +-------+------+
    76.  
       
    77.  
      Dataset<Row> result = spark.sql("SELECT myAverage(salary) as average_salary FROM employees");
    78.  
      result.show();
    79.  
      // +--------------+
    80.  
      // |average_salary|
    81.  
      // +--------------+
    82.  
      // | 3750.0|
    83.  
      // +--------------+

    类型安全的用户定义的聚合函数 
    用于强类型数据集的用户定义聚合关联Aggregator抽象类。例如,类型安全的用户定义的平均值可能如下所示:

    1.  
      public static class Employee implements Serializable {
    2.  
      private String name;
    3.  
      private long salary;
    4.  
       
    5.  
      // Constructors, getters, setters...
    6.  
       
    7.  
      }
    8.  
       
    9.  
      public static class Average implements Serializable {
    10.  
      private long sum;
    11.  
      private long count;
    12.  
       
    13.  
      // Constructors, getters, setters...
    14.  
       
    15.  
      }
    16.  
       
    17.  
      public static class MyAverage extends Aggregator<Employee, Average, Double> {
    18.  
      // A zero value for this aggregation. Should satisfy the property that any b + zero = b
    19.  
      public Average zero() {
    20.  
      return new Average(0L, 0L);
    21.  
      }
    22.  
      // Combine two values to produce a new value. For performance, the function may modify `buffer`
    23.  
      // and return it instead of constructing a new object
    24.  
      public Average reduce(Average buffer, Employee employee) {
    25.  
      long newSum = buffer.getSum() + employee.getSalary();
    26.  
      long newCount = buffer.getCount() + 1;
    27.  
      buffer.setSum(newSum);
    28.  
      buffer.setCount(newCount);
    29.  
      return buffer;
    30.  
      }
    31.  
      // Merge two intermediate values
    32.  
      public Average merge(Average b1, Average b2) {
    33.  
      long mergedSum = b1.getSum() + b2.getSum();
    34.  
      long mergedCount = b1.getCount() + b2.getCount();
    35.  
      b1.setSum(mergedSum);
    36.  
      b1.setCount(mergedCount);
    37.  
      return b1;
    38.  
      }
    39.  
      // Transform the output of the reduction
    40.  
      public Double finish(Average reduction) {
    41.  
      return ((double) reduction.getSum()) / reduction.getCount();
    42.  
      }
    43.  
      // Specifies the Encoder for the intermediate value type
    44.  
      public Encoder<Average> bufferEncoder() {
    45.  
      return Encoders.bean(Average.class);
    46.  
      }
    47.  
      // Specifies the Encoder for the final output value type
    48.  
      public Encoder<Double> outputEncoder() {
    49.  
      return Encoders.DOUBLE();
    50.  
      }
    51.  
      }
    52.  
       
    53.  
      Encoder<Employee> employeeEncoder = Encoders.bean(Employee.class);
    54.  
      String path = "examples/src/main/resources/employees.json";
    55.  
      Dataset<Employee> ds = spark.read().json(path).as(employeeEncoder);
    56.  
      ds.show();
    57.  
      // +-------+------+
    58.  
      // | name|salary|
    59.  
      // +-------+------+
    60.  
      // |Michael| 3000|
    61.  
      // | Andy| 4500|
    62.  
      // | Justin| 3500|
    63.  
      // | Berta| 4000|
    64.  
      // +-------+------+
    65.  
       
    66.  
      MyAverage myAverage = new MyAverage();
    67.  
      // Convert the function to a `TypedColumn` and give it a name
    68.  
      TypedColumn<Employee, Double> averageSalary = myAverage.toColumn().name("average_salary");
    69.  
      Dataset<Double> result = ds.select(averageSalary);
    70.  
      result.show();
    71.  
      // +--------------+
    72.  
      // |average_salary|
    73.  
      // +--------------+
    74.  
      // | 3750.0|
    75.  
      // +--------------+

    数据源 
    Spark SQL支持通过DataFrame接口在各种数据源上进行操作。DataFrame可以使用关系变换进行操作,也可以用来创建临时视图。将DataFrame注册为临时视图允许您对其数据运行SQL查询。本节介绍使用Spark Data Sources加载和保存数据的一般方法,然后介绍可用于内置数据源的特定选项。

    通用加载/保存功能

    1.  
      Dataset<Row> usersDF = spark.read().load("examples/src/main/resources/users.parquet");
    2.  
      usersDF.select("name", "favorite_color").write().save("namesAndFavColors.parquet");

    手动指定选项 
    您也可以手动指定将要使用的数据源以及您想要传递给数据源的其他选项。数据源通过其全名指定(即org.apache.spark.sql.parquet),但内置的来源,你也可以使用自己的短名称(json,parquet,jdbc,orc,libsvm,csv,text)。从任何数据源类型加载的数据框可以使用此语法转换为其他类型。

    1.  
      Dataset<Row> peopleDF =
    2.  
      spark.read().format("json").load("examples/src/main/resources/people.json");
    3.  
      peopleDF.select("name", "age").write().format("parquet").save("namesAndAges.parquet");

    直接在文件上运行SQL 
    您可以使用SQL直接查询该文件,而不是使用读取API将文件加载到DataFrame中并进行查询。

    1.  
      Dataset<Row> sqlDF =
    2.  
      spark.sql("SELECT * FROM parquet.`examples/src/main/resources/users.parquet`");

    保存模式 
    保存操作可以选择一个SaveMode,指定如何处理现有的数据(如果存在)。认识到这些保存模式不使用任何锁定而不是原子是很重要的。另外,执行时Overwrite,数据在写出新数据之前将被删除。

    1.  
      SaveMode.ErrorIfExists (默认) "error" (默认) 将DataFrame保存到数据源时,如果数据已经存在,则预计会抛出异常。
    2.  
      SaveMode.Append "append" 将DataFrame保存到数据源时,如果data / table已经存在,则DataFrame的内容将被追加到现有数据中。
    3.  
      SaveMode.Overwrite "overwrite" 覆盖模式意味着将DataFrame保存到数据源时,如果data / table已经存在,则现有数据将被DataFrame的内容覆盖。
    4.  
      SaveMode.Ignore "ignore" 忽略模式意味着,当将DataFrame保存到数据源时,如果数据已经存在,保存操作将不会保存DataFrame的内容,也不会更改现有数据。这与CREATE TABLE IF NOT EXISTSSQL中的类似。

    保存到持久表 
    DataFrames也可以使用该saveAsTable 命令将其作为持久表保存到Hive Metastore中。请注意,现有的Hive部署对于使用此功能不是必需的。Spark将为您创建一个默认的本地Hive Metastore(使用Derby)。与createOrReplaceTempView命令不同的是, saveAsTable将实现DataFrame的内容并创建指向Hive Metastore中的数据的指针。即使您的Spark程序重新启动后,持久性表格仍然存在,只要您保持与同一Metastore的连接。用于持久表的DataFrame可以通过使用表的名称调用tablea方法来创建SparkSession。

    对于基于文件的数据源,例如文本,parquet,json等,您可以通过path选项指定自定义表格路径 ,例如df.write.option(“path”, “/some/path”).saveAsTable(“t”)。当表被删除时,自定义表路径将不会被删除,表数据仍然存在。如果没有指定自定义表格路径,则Spark将把数据写入仓库目录下的默认表格路径。当表被删除时,默认的表路径也将被删除。 
    持久数据源表具有存储在Hive Metastore中的每个分区元数据。这带来了几个好处:

    由于Metastore只能返回查询所需的分区,因此不再需要发现第一个查询的所有分区。 
    Hive DDL如ALTER TABLE PARTITION … SET LOCATION现在可用于使用Datasource API创建的表。 
    请注意,创建外部数据源表(具有path选项的那些表)时,默认情况下不会收集分区信息。要同步Metastore中的分区信息,可以调用MSCK REPAIR TABLE。 
    分段,分类和分区 
    对于基于文件的数据源,也可以对输出进行分类和分类。分段和排序仅适用于持久表

    peopleDF.write().bucketBy(42, "name").sortBy("age").saveAsTable("people_bucketed");
    •  

    而分区则可以同时使用save和saveAsTable使用数据集API。

    1.  
      usersDF
    2.  
      .write()
    3.  
      .partitionBy("favorite_color")
    4.  
      .format("parquet")
    5.  
      .save("namesPartByColor.parquet");

    可以对单个表使用分区和分区:

    1.  
      peopleDF
    2.  
      .write()
    3.  
      .partitionBy("favorite_color")
    4.  
      .bucketBy(42, "name")
    5.  
      .saveAsTable("people_partitioned_bucketed");

    partitionBy创建一个目录结构,如“ 分区发现”部分所述。因此,对基数高的柱子的适用性有限。相比之下 bucketBy,通过固定数量的桶分配数据,并且可以在大量唯一值无界时使用。 
    分区发现 
    表分区是像Hive这样的系统中常用的优化方法。在分区表中,数据通常存储在不同的目录中,分区列值在每个分区目录的路径中编码。现在,Parquet数据源能够自动发现和推断分区信息。例如,我们可以使用以下目录结构,两个额外的列gender和country分区列将所有先前使用的人口数据存储到分区表中:

    1.  
      path
    2.  
      └── to
    3.  
      └── table
    4.  
      ├── gender=male
    5.  
      │ ├── ...
    6.  
      │ │
    7.  
      │ ├── country=US
    8.  
      │ │ └── data.parquet
    9.  
      │ ├── country=CN
    10.  
      │ │ └── data.parquet
    11.  
      │ └── ...
    12.  
      └── gender=female
    13.  
      ├── ...
    14.  
    15.  
      ├── country=US
    16.  
      │ └── data.parquet
    17.  
      ├── country=CN
    18.  
      │ └── data.parquet
    19.  
      └── ...

    通过传递path/to/table给SparkSession.read.parquet或者SparkSession.read.load,Spark SQL将自动从路径中提取分区信息。现在,返回的DataFrame的模式变成:

    1.  
      root
    2.  
      |-- name: string (nullable = true)
    3.  
      |-- age: long (nullable = true)
    4.  
      |-- gender: string (nullable = true)
    5.  
      |-- country: string (nullable = true)

    请注意,分区列的数据类型是自动推断的。目前支持数字数据类型和字符串类型。有时用户可能不希望自动推断分区列的数据类型。对于这些用例,可以使用spark.sql.sources.partitionColumnTypeInference.enabled默认 的自动类型推断来配置true。当禁用类型推断时,字符串类型将用于分区列。 
    JSON数据集 
    Spark SQL可以自动推断JSON数据集的模式,并将其作为一个Dataset。这个转换可以SparkSession.read().json()在一个Dataset或者一个JSON文件上完成。

    请注意,作为json文件提供的文件不是典型的JSON文件。每行必须包含一个单独的,独立的有效JSON对象。有关更多信息,请参阅 JSON行文本格式,也称为换行符分隔的JSON。

    对于常规的多行JSON文件,请将该multiLine选项设置为true。

    1.  
      // A JSON dataset is pointed to by path.
    2.  
      // The path can be either a single text file or a directory storing text files
    3.  
      Dataset<Row> people = spark.read().json("examples/src/main/resources/people.json");
    4.  
       
    5.  
      // The inferred schema can be visualized using the printSchema() method
    6.  
      people.printSchema();
    7.  
      // root
    8.  
      // |-- age: long (nullable = true)
    9.  
      // |-- name: string (nullable = true)
    10.  
       
    11.  
      // Creates a temporary view using the DataFrame
    12.  
      people.createOrReplaceTempView("people");
    13.  
       
    14.  
      // SQL statements can be run by using the sql methods provided by spark
    15.  
      Dataset<Row> namesDF = spark.sql("SELECT name FROM people WHERE age BETWEEN 13 AND 19");
    16.  
      namesDF.show();
    17.  
      // +------+
    18.  
      // | name|
    19.  
      // +------+
    20.  
      // |Justin|
    21.  
      // +------+
    22.  
       
    23.  
      // Alternatively, a DataFrame can be created for a JSON dataset represented by
    24.  
      // a Dataset<String> storing one JSON object per string.
    25.  
      List<String> jsonData = Arrays.asList(
    26.  
      "{"name":"Yin","address":{"city":"Columbus","state":"Ohio"}}");
    27.  
      Dataset<String> anotherPeopleDataset = spark.createDataset(jsonData, Encoders.STRING());
    28.  
      Dataset<Row> anotherPeople = spark.read().json(anotherPeopleDataset);
    29.  
      anotherPeople.show();
    30.  
      // +---------------+----+
    31.  
      // | address|name|
    32.  
      // +---------------+----+
    33.  
      // |[Columbus,Ohio]| Yin|
    34.  
      // +---------------+----+

    JDBC到其他数据库

    故障排除

    性能调整 
    对于某些工作负载,可以通过在内存中缓存数据或打开一些实验选项来提高性能。

    在内存中缓存数据

    其他配置选项

    分布式SQL引擎

    运行Thrift JDBC / ODBC服务器

    运行Spark SQL CLI

    详情参见 
    http://spark.apache.org/docs/latest/sql-programming-guide.html

     
     
  • 相关阅读:
    springcloud2.0 添加配置中心遇到的坑
    redis 字符串和集合操作
    Linux
    流畅的python 符合python风格的对象
    流畅的python 对象引用 可变性和垃圾回收
    流畅的python 闭包
    流畅的python 使用一等函数实现设计模式
    互联网协议入门
    流畅的python 字典和集合
    流畅的python python数据模型
  • 原文地址:https://www.cnblogs.com/javalinux/p/15065523.html
Copyright © 2020-2023  润新知