pyspark连接mysql
from pyspark import SparkContext
from pyspark.sql import SQLContext if __name__=="__main__":
sc = SparkContext(appName="local")
sqlContext = SQLContext(sc)
df = sqlContext.read.format("jdbc").options(url="jdbc:mysql://localhost:3306/test?user=root&password=root",dbtable="test_customer").load()
df.show()
sc.stop()
如果报错 no suitable driver
需要把连接mysql的jdbc的jar包拷到spark文件夹里的jars文件夹里。
附:使用SQL查询的代码
from pyspark import SparkContext
from pyspark.sql import SQLContext if __name__=="__main__":
sc = SparkContext(appName="local")
sqlContext = SQLContext(sc)
df = sqlContext.read.format("jdbc").options(url="jdbc:mysql://localhost:3306/test?user=root&password=root",dbtable="test_customer").load()
df.registerTempTable("test1");
ls = sqlContext.sql("select * from test1 where did = 1").collect()
for it in ls:
print("")
sc.stop()
再附,HiveContext的使用及RDD转DataFrame:
from pyspark import SparkContext
from pyspark.sql import HiveContext,SQLContext,Row if __name__=="__main__":
sc = SparkContext(appName="local")
hc = HiveContext(sc) #HiveContext
sqlContext = SQLContext(sc) #SqlContext
datas = ["1 a 28","2 b 29", "3 c 30"]
source = sc.parallelize(datas) #加载数组
splits = source.map(lambda line: line.split(" ")) #map方法返回的RDD格式的数据
rows = splits.map(lambda words: Row(id = words[0], name = words[1], age = words[2]))
structType = hc._inferSchema(rows); #获得StructType
people = sqlContext.createDataFrame(rows, structType) #通过StructType和查询出来的数据转换成DataFrame
people.registerTempTable("people") #注册表
results = hc.sql("select name from people").collect()
#results1 = results.map(lambda row: row.name.upper()).collect()
for result in results:
print("name:"+result.name)
sc.stop()
pyspark连接mysql的更多相关文章
- nodejs进阶(6)—连接MySQL数据库
1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...
- 【初学python】使用python连接mysql数据查询结果并显示
因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...
- Node.js Express连接mysql完整的登陆注册系统(windows)
windows学习环境: node 版本: v0.10.35 express版本:4.10.0 mysql版本:5.6.21-log 第一部分:安装node .Express(win8系统 需要&qu ...
- PDO连接mysql数据库
1.PDO简介 PDO(PHP Data Object) 是PHP 5 中加入的东西,是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接 ...
- 使用ABP EntityFramework连接MySQL数据库
ASP.NET Boilerplate(简称ABP)是在.Net平台下一个很流行的DDD框架,该框架已经为我们提供了大量的函数,非常方便与搭建企业应用. 关于这个框架的介绍我就不多说,有兴趣的可以参见 ...
- jmeter之连接mysql和SQL Server配置
下载jdbc驱动 在使用jmeter做性能或自动化测试的时候,往往需要直接对数据库施加压力,或者某些参数只能从数据库获取,这时候就必须使用jmeter连接数据库. 1.下载对应的驱动包 mysql驱动 ...
- Windows操作系统下远程连接MySQL数据库
用Eclipse做一个后台项目,但是数据库不想放在本地电脑,于是买了一个腾讯云服务器(学生有优惠,挺便宜的),装上MySQL数据库,但是测试连接的时候,发现总是连接不是上,但是本地数据库可以连接,于是 ...
- Hibernate连接mysql数据库的配置
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...
- C# 3种方法连接MySql
转 http://wenku.baidu.com/view/d0cf34708e9951e79b8927c7.html C# 连接MYSQL数据库的方法及示例 连接MYSQL数据库的方法及示例 方 ...
随机推荐
- Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树
E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...
- php 数值数组遍历
<?php $cars=array("Volvo","BMW","Toyota"); $arrlength=count($cars); ...
- 不能访问虚拟机上的nginx网站
VMware虚拟机上配置nginx后,本机无法访问问题 转自:http://www.server110.com/nginx/201407/10794.html 把nginx装在CentOS上,用本机访 ...
- RoR unobtrusive scripting adapter--UJS(一些Javascript的语法糖)
Learn how the new Rails UJS library works and compares with the old version of jquery_ujs that it re ...
- c++ 中的 set
set (集合) 中的元素是排序好的,而且是不重复的. 例题:hdu 4989 题目大意:求一组数列中任意两个不重复元素和,再求不重复和的和. #include <bits/stdc++.h&g ...
- 124. Binary Tree Maximum Path Sum *HARD* -- 二叉树中节点和最大的路径的节点和
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 038——VUE中组件之WEB开发中组件使用场景与定义组件的方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【DEV GridControl】怎样使GridView中满足某个条件的行可编辑,其余行不可编辑?
DXperience控件包,使用起来非常方便,但有时候某些功能的实现在文档中不太容易找到解决方案,比如下面要提到的这个功能我就在文档中找了很久也没找到,最后还是在官方论坛上找到的. 具体问题是这样的: ...
- windows update失败还原更改,无法开机
转自 http://jingyan.baidu.com/article/59a015e34c5a73f794886520.html?tj=exp_relate_3&st=2&from ...
- HDU2870 最大窗口面积(单调队列优化)
HDU1506 HDU2870 HDU4328 以前做的是单调队列的方法,现在试一试DP 注意的是对于i,向左延伸的L[i]到i不一定是单调的,比如1 3 2 1 2,对于i=4,L[i]=1而是2所 ...