SparkSqlキャッシュデータはpgxl Alluxioの再計算を読みます

著作権:転載リンクしてくださいhttps://blog.csdn.net/DPnice/article/details/85329765

http://www.alluxio.com/blog/effective-spark-rdds-with-alluxio

RDDデータ自体のスパークは、メモリに格納されていますが、大量のデータがキャッシュメモリを刺激する場合は大幅なパフォーマンスの低下を引き起こす可能性があります。

Alluxioは、あなたがより速くスパークアプリケーションを有効にする、メモリ内の大きなデータセットを保存することができます。Alluxioは、同じデータセットをメモリ内の共有をサポートし、クラスタ全体のパフォーマンスを向上させることができ、複数のアプリケーションを、スパーク。

AlluxioでメモリRDDに保存されているだけでRDD Alluxioにファイルを保存するために、非常に簡単です。RDDSする2つの一般的な方法は、AlluxioとsaveAsTextFileとsaveAsObjectFileを使用することができ、ファイルとして保存しました。あなたは(メモリから)RDD sc.objectFileで使用または再読み込み保存sc.textFile Alluxioすることができます。

デモ:

package com.dhhy.spark.demo;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.rdd.RDD;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import scala.Tuple2;

import java.util.Map;
import java.util.Properties;

/**
 * @author DPn!ce
 * @date 2018/12/28.
 */
public class SparkPostgreSQLJdbcWc {
    public static void main(String[] args) {

        SparkConf conf = new SparkConf();
        conf.setAppName("SparkPostgreSQLJdbcWc");
        conf.setMaster("local[*]");
        SparkSession spark = SparkSession
                .builder()
                .config(conf)
                .getOrCreate();

        // jdbc
        Properties connectionProperties = new Properties();

        //增加数据库的用户名(user)密码(password),指定postgresql驱动(driver)
        connectionProperties.put("user", "");
        connectionProperties.put("password", "");
        connectionProperties.put("driver", "org.postgresql.Driver");
        connectionProperties.put("fetchsize", "10000");

        String persistPath = "alluxio://ip:19998/spark/persist";
        String url = "jdbc:postgresql://ip:15432/dscdb";
        // 必须是表中的数字列
        String columnName = "cast(origin_id AS NUMERIC)";
//        String table = "iot.dhhsh_m_fire_control";
        String table = "(SELECT * FROM iot.dhhsh_m_fire_control limit 10 ) dhhsh_m_fire_control";
        // 小于下界的分到一个分区 origin_id < lowerBound
        Long lowerBound = 1568L;
        // 大于上界的分到一个分区 origin_id > upperBound
        Long upperBound = 6709L;
        // 这里分四个分区
        int numPartitions = 4;
        /*
        分区1:origin_id < lowerBound
        分区2:lowerBound < origin_id <= (upperBound -lowerBound)/(numPartitions -2)
        分区3:(upperBound -lowerBound)/(numPartitions -2) < origin_id <= upperBound
        分区4:origin_id > upperBound
         */

        //SparkJdbc读取PostgreSQL 表内容
        Dataset<Row> jdbcDS = spark.read()
                //table 需要加上模式
                .jdbc(url, table, columnName, lowerBound, upperBound, numPartitions, connectionProperties);

        long readTimeStart = System.currentTimeMillis();

        //显示jdbcDF数据内容
        jdbcDS.show(1);

        System.out.println(System.currentTimeMillis() - readTimeStart + " ms");

        // 查看分区数
//        int size = jdbcDS.toJavaRDD().partitions().size();
//        System.out.println(size);


        JavaRDD<Row> rowJavaRDD = jdbcDS.toJavaRDD();

        long saveTimeStart = System.currentTimeMillis();
        // 保存到alluxio
        rowJavaRDD.saveAsTextFile(persistPath);
//        rowJavaRDD.saveAsTextFile("C:\\Users\\Lenovo\\Desktop\\p");

        System.out.println(System.currentTimeMillis() - saveTimeStart + " ms");

        long countTimeStart = System.currentTimeMillis();

        // 读取alluxio的数据
        RDD<String> stringRDD = spark.sparkContext().textFile(persistPath, numPartitions);
//        RDD<String> stringRDD = spark.sparkContext().textFile("C:\\Users\\Lenovo\\Desktop\\p", numPartitions);

        Map<String, Long> stringLongMap = stringRDD.toJavaRDD().mapToPair(
                str -> {
                    String[] split = str.split(",");
                    return new Tuple2<>(split[3], split[0]);
                })
                .countByKey();
        System.out.println("分组计数用时:"+(System.currentTimeMillis() - countTimeStart) + " ms");

        stringLongMap.forEach((k, v) -> System.out.println(k + "\t" + v));
    }
}

おすすめ

転載: blog.csdn.net/DPnice/article/details/85329765