Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
W
wanjia
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
kino
wanjia
Commits
1cae46b3
Commit
1cae46b3
authored
Nov 06, 2020
by
宋朋
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
3247ad3d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
27 deletions
+69
-27
Sources.py
spark/sources/Sources.py
+11
-0
unzip.py
spark/unzip.py
+58
-27
No files found.
spark/sources/Sources.py
View file @
1cae46b3
import
pymysql
import
pymysql
import
exception.MyErrorException
as
ex
import
exception.MyErrorException
as
ex
from
pyspark
import
SparkContext
def
getMySqlConnect
():
def
getMySqlConnect
():
try
:
try
:
...
@@ -28,11 +29,21 @@ def getRecordByIdResultT1(id):
...
@@ -28,11 +29,21 @@ def getRecordByIdResultT1(id):
record
=
cur
.
fetchone
()
record
=
cur
.
fetchone
()
except
ex
.
MyError
as
e
:
except
ex
.
MyError
as
e
:
print
(
"executor failed"
,
e
.
errorinfo
)
print
(
"executor failed"
,
e
.
errorinfo
)
conn
.
rollback
()
finally
:
finally
:
cur
.
close
()
# 关闭数据库连接
cur
.
close
()
# 关闭数据库连接
conn
.
close
()
conn
.
close
()
return
record
return
record
def
readCsv
(
spark
,
path
,
col
):
def
readCsv
(
spark
,
path
,
col
):
df
=
spark
.
read
.
format
(
'csv'
)
.
option
(
'inferSchema'
,
'true'
)
.
load
(
path
)
.
toDF
(
*
col
)
df
=
spark
.
read
.
format
(
'csv'
)
.
option
(
'inferSchema'
,
'true'
)
.
load
(
path
)
.
toDF
(
*
col
)
return
df
return
df
def
readTxt
(
spark
,
path
,
col
,
delimiter
):
rddFile
=
SparkContext
.
textFile
(
"1.txt"
)
rddMap
=
rddFile
.
map
(
lambda
x
:
x
.
split
(
delimiter
))
df
=
spark
.
createDataFrame
(
rddMap
,
col
)
return
df
spark/unzip.py
View file @
1cae46b3
...
@@ -3,16 +3,23 @@ import sys
...
@@ -3,16 +3,23 @@ import sys
import
os
import
os
import
shutil
import
shutil
import
zipfile
import
zipfile
import
exception.MyErrorException
as
ex
pyName
=
sys
.
argv
[
0
]
#sys.argv[0] 类似于shell中的$0, 但不是脚本名称,而是脚本的路径
zipPath
=
sys
.
argv
[
1
]
#sys.argv[1] 表示传入的第一个参数
# pyName = sys.argv[0] # sys.argv[0] 类似于shell中的$0, 但不是脚本名称,而是脚本的路径
inputPath
=
sys
.
argv
[
2
]
#sys.argv[2] 表示传入的第二个参数
# zipPath = sys.argv[1] # sys.argv[1] 表示传入的第一个参数
outputPath
=
sys
.
argv
[
3
]
# inputPath = sys.argv[2] # sys.argv[2] 表示传入的第二个参数
# outputPath = sys.argv[3]
#问题:解压的时候要考虑密码吗???
# os.system("hdfs dfs -put /srcPath /dstPath")
# os.system("hdfs dfs -put /srcPath /dstPath")
def
get_file
(
srcPath
,
dstPath
):
def
get_file
(
srcPath
,
dstPath
):
try
:
if
not
os
.
path
.
exists
(
srcPath
):
if
not
os
.
path
.
exists
(
srcPath
):
print
(
"srcPath not exist"
)
os
.
mkdir
(
srcPath
)
print
(
"srcPath mkdir success"
)
if
not
os
.
path
.
exists
(
dstPath
):
os
.
mkdir
(
srcPath
)
print
(
"dstPath mkdir success"
)
else
:
else
:
res
=
[]
res
=
[]
for
root
,
dirs
,
files
in
os
.
walk
(
srcPath
,
True
):
for
root
,
dirs
,
files
in
os
.
walk
(
srcPath
,
True
):
...
@@ -20,21 +27,45 @@ def get_file(srcPath, dstPath):
...
@@ -20,21 +27,45 @@ def get_file(srcPath, dstPath):
name
,
suf
=
os
.
path
.
splitext
(
fileName
)
name
,
suf
=
os
.
path
.
splitext
(
fileName
)
if
fileName
.
endswith
(
'.csv'
):
if
fileName
.
endswith
(
'.csv'
):
shutil
.
copy
(
os
.
path
.
join
(
root
,
fileName
),
dstPath
)
shutil
.
copy
(
os
.
path
.
join
(
root
,
fileName
),
dstPath
)
except
ex
.
MyError
as
e
:
print
(
"失败"
,
e
.
errorinfo
)
def
unzip_file
(
zip_file_name
,
destination_path
):
def
unzip_single
(
srcPath
,
dstPath
):
''' 解压单个文件到目标文件夹。
'''
zf
=
zipfile
.
ZipFile
(
srcPath
)
try
:
try
:
archive
=
zipfile
.
ZipFile
(
zip_file_name
,
mode
=
'r'
)
zf
.
extractall
(
path
=
dstPath
)
for
file
in
archive
.
namelist
():
except
RuntimeError
as
e
:
archive
.
extract
(
file
,
destination_path
)
print
(
e
)
except
ex
.
MyError
as
e
:
zf
.
close
()
print
(
"解压失败"
,
e
.
errorinfo
)
sys
.
exit
()
def
unzip_all
(
source_dir
,
dest_dir
):
if
not
os
.
path
.
isdir
(
source_dir
):
# 如果是单一文件
unzip_single
(
source_dir
,
dest_dir
)
else
:
it
=
os
.
scandir
(
source_dir
)
for
entry
in
it
:
if
entry
.
is_file
()
and
os
.
path
.
splitext
(
entry
.
name
)[
1
]
==
'.zip'
:
unzip_single
(
entry
.
path
,
dest_dir
)
# def unzip_file(zip_file_name, destination_path):
# archive = zipfile.ZipFile(zip_file_name, mode='r')
# for file in archive.namelist():
# print(file)
# name, suf = os.path.splitext(file)
# if file.endswith('.zip'):
# archive.extract(file, destination_path)
# print("解压失败")
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
# if len(sys.argv) == 3:
# source_dir, dest_dir = os.path.abspath(sys.argv[0].strip('"')), sys.argv[1]
print
(
'解压、复制中...'
)
print
(
'解压、复制中...'
)
unzip_file
(
zipPath
,
inputPath
)
# unzip_file(zipPath, inputPath)
get_file
(
inputPath
,
outputPath
)
# get_file(inputPath, outputPath)
unzip_all
(
"D:
\\
src"
,
"D:
\\
dst"
)
get_file
(
"D:
\\
src"
,
"D:
\\
dst"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment