forked from allisonwang-db/pyspark-data-sources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.py
More file actions
73 lines (55 loc) · 2.42 KB
/
github.py
File metadata and controls
73 lines (55 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import requests
from pyspark.sql import Row
from pyspark.sql.datasource import DataSource, DataSourceReader
class GithubDataSource(DataSource):
"""
A DataSource for reading pull requests data from Github.
Name: `github`
Schema: `id int, title string, author string, created_at string, updated_at string`
Examples
--------
Register the data source.
>>> from pyspark_datasources import GithubDataSource
>>> spark.dataSource.register(GithubDataSource)
Load pull requests data from a public Github repository.
>>> spark.read.format("github").load("apache/spark").show()
+---+--------------------+--------+--------------------+--------------------+
| id| title| author| created_at| updated_at|
+---+--------------------+--------+--------------------+--------------------+
| 1|Initial commit | matei |2014-02-03T18:47:...|2014-02-03T18:47:...|
|...| ...| ...| ...| ...|
+---+--------------------+--------+--------------------+--------------------+
Load pull requests data from a private Github repository.
>>> spark.read.format("github").option("token", "your-token").load("owner/repo").show()
"""
@classmethod
def name(self):
return "github"
def schema(self):
return "id int, title string, author string, created_at string, updated_at string"
def reader(self, schema):
return GithubPullRequestReader(self.options)
class GithubPullRequestReader(DataSourceReader):
def __init__(self, options):
self.token = options.get("token")
self.repo = options.get("path")
if self.repo is None:
raise Exception(f"Must specify a repo in `.load()` method.")
def read(self, partition):
header = {
"Accept": "application/vnd.github+json",
}
if self.token is not None:
header["Authorization"] = f"Bearer {self.token}"
url = f"https://api.github.com/repos/{self.repo}/pulls"
response = requests.get(url)
response.raise_for_status()
prs = response.json()
for pr in prs:
yield Row(
id = pr.get("number"),
title = pr.get("title"),
author = pr.get("user", {}).get("login"),
created_at = pr.get("created_at"),
updated_at = pr.get("updated_at")
)