Spark读取mysql数据

5 篇文章 0 订阅
2 篇文章 0 订阅
public void getStudentName() throws IOException {
        Properties properties = new Properties();
        properties.put("driver", ApplicationYmlUtils.getDataSourceDriverClassName());
        properties.put("user", ApplicationYmlUtils.getDataSourceUsername());
        properties.put("password", ApplicationYmlUtils.getDataSourcePassword());
        Dataset<Row> loadData = sqlContext.read().jdbc(ApplicationYmlUtils.getDataSourceUrl(), "t_student", properties);
        /*
        DataFrameReader reader = sqlContext.read().format("jdbc")
                .option("url", ApplicationYmlUtils.getDataSourceUrl())
                .option("dbtable", sql)
                .option("driver", ApplicationYmlUtils.getDataSourceDriverClassName())
                .option("user", ApplicationYmlUtils.getDataSourceUsername())
                .option("password", ApplicationYmlUtils.getDataSourcePassword());
        Dataset<Row> loadData = reader.load();
         */
        System.out.println(loadData.count());
        loadData.write().mode(SaveMode.Overwrite).json("E:/11.json");

如果是百万级甚至千万级乙级数据呢,这里以120W数据为例,数据库确实存在120W数据,输出成JSON文件为例

一次性读取

  有OOM风险

public void getStudentName() throws IOException {
        long startTime = System.currentTimeMillis();
        Properties properties = new Properties();
        properties.put("driver", ApplicationYmlUtils.getDataSourceDriverClassName());
        properties.put("user", ApplicationYmlUtils.getDataSourceUsername());
        properties.put("password", ApplicationYmlUtils.getDataSourcePassword());
        Dataset<Row> loadData = sqlContext.read().jdbc(ApplicationYmlUtils.getDataSourceUrl(), "sys_operation_log", properties);
        System.out.println(loadData.count());
        loadData.write().mode(SaveMode.Overwrite).json("E:/11.json");
        long endTime = System.currentTimeMillis();
        System.err.println("120W数据大概用时"+(endTime-startTime));
    }

生成的JSON文件是只有一个JSON文件

 

分批次/分区

  无OOM风险,但总时长比较长

public void getStudentName() throws IOException {
        long startTime = System.currentTimeMillis();
        Properties properties = new Properties();
        properties.put("driver", ApplicationYmlUtils.getDataSourceDriverClassName());
        properties.put("user", ApplicationYmlUtils.getDataSourceUsername());
        properties.put("password", ApplicationYmlUtils.getDataSourcePassword());
        String[] predicates=new String[120];
        Integer num=120;
        Integer index=0;
        for (int i = 0; i <num ; i++) {
            predicates[i]=" 1=1 LIMIT "+index+","+10000;
            index+=10000;
        }
        Dataset<Row> loadData = sqlContext.read().jdbc(ApplicationYmlUtils.getDataSourceUrl(), "sys_operation_log",predicates ,properties);
        System.out.println(loadData.count());
        loadData.write().mode(SaveMode.Overwrite).json("E:/11.json");
        long endTime = System.currentTimeMillis();
        System.err.println("120W数据大概用时"+(endTime-startTime));
    }

下面是任务部分日志截图

足足用了二十多分钟

JSON文件也是按任务分散的,并不是一个文件

数据量大还不如传统的mysql查询,毕竟是先查询数据放入内存再做相应的操作,可以用sqoop将mysql数据放进HDFS等再使用Spark来进行计算

所以海量数据Spark还是比较适用HDFS/Hive/Hbase这些来做数据源

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值