代码-第2章 实验环境搭建-林子雨编著《数据采集与预处理》

大数据学习路线图

林子雨编著《数据采集与预处理》教材配套代码(教材官网
查看所有章节代码

第2章 实验环境搭建

# two_number.py
a,b,c = 4,5,0
if a>b:
    c = b
if a<b:
    c = a
print("两个数的较小值是:",c)
# odd_even.py
a = 5
if a % 2 == 0:
    print("这是一个偶数。")
else:
    print("这是一个奇数。")
# lesson.py
day = int(input("请输入第几天课程:"))
if day == 1:
    print("第1天上数学课")
elif day == 2:
    print("第2天上语文课")
else:
    print("其他时间上计算机课")
# int_sum.py
n = 1
sum = 0
while(n <= 99):
    sum += n
    n += 1
print("1~99的整数和是:",sum)
# int_sum_for.py
sum=0
for n in range(1,100):   #range(1,100)用于生成1到100(不包括100)的整数
    sum+=n
print("1到99的整数和是:",sum)
# i_like.py
# 定义带有参数的函数
def like(language):
    '''打印喜欢的编程语言!'''
    print("我喜欢{}语言!".format(language))
    return
# 调用函数
like("C")
like("C#")
like("Python")
mysql> CREATE DATABASE school;
mysql> SHOW DATABASES;
mysql> USE school;
mysql>CREATE TABLE student(
    -> sno char(5),
    -> sname char(10),
    -> ssex char(2),
    -> sage int);
mysql> SHOW TABLES;
mysql> INSERT INTO student VALUES('95001','王小明','男',21);
mysql> INSERT INTO student VALUES('95002','张梅梅','女',20);
mysql> SELECT * FROM student;
mysql> UPDATE student SET age =21 WHERE sno=’95001’;
mysql> DROP TABLE student;
mysql> SHOW TABLES;
mysql> DROP DATABASE school;
mysql> SHOW DATABASES;
> pip install PyMySQL
# mysql1.py
import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host='localhost',
    port=3306,
    user='root',  # 数据库用户名
    passwd='123456',  # 密码
    db='school',
    charset='utf8'
)

# 获取游标
cursor = connect.cursor()

# 执行SQL查询
cursor.execute("SELECT VERSION()")

# 获取单条数据
version = cursor.fetchone()

# 打印输出
print("MySQL数据库版本是:%s" % version)

# 关闭数据库连接
connect.close()
# mysql2.py
import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host='localhost',
    port=3306,
    user='root',  # 数据库用户名
    passwd='123456',  # 密码
    db='school',
    charset='utf8'
)

# 获取游标
cursor = connect.cursor()

# 如果表存在,则先删除
cursor.execute("DROP TABLE IF EXISTS student")

# 设定SQL语句

sql = """
create table student(
    sno char(5),
    sname char(10),
    ssex char(2),
    sage int);
"""

# 执行SQL语句
cursor.execute(sql)

# 关闭数据库连接
connect.close()
# mysql3.py
import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host='localhost',
    port=3306,
    user='root',  # 数据库用户名
    passwd='123456',  # 密码
    db='school',
    charset='utf8'
)

# 获取游标
cursor = connect.cursor()

# 插入数据
sql = "INSERT INTO student(sno,sname,ssex,sage) VALUES ('%s', '%s', '%s', %d)"
data1 = ('95001','王小明','男',21)
data2 = ('95002','张梅梅','女',20)
cursor.execute(sql % data1)
cursor.execute(sql % data2)
connect.commit()
print('成功插入数据')

# 关闭数据库连接
connect.close()
# mysql4.py
import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host='localhost',
    port=3306,
    user='root',  # 数据库用户名
    passwd='123456',  # 密码
    db='school',
    charset='utf8'
)

# 获取游标
cursor = connect.cursor()

# 修改数据
sql = "UPDATE student SET sage = %d WHERE sno = '%s' "
data = (21, '95002')
cursor.execute(sql % data)
connect.commit()
print('成功修改数据')

# 关闭数据库连接
connect.close()
# mysql5.py
import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host='localhost',
    port=3306,
    user='root',  # 数据库用户名
    passwd='123456',  # 密码
    db='school',
    charset='utf8'
)

# 获取游标
cursor = connect.cursor()

# 查询数据
sql = "SELECT sno,sname,ssex,sage FROM student WHERE sno = '%s' "
data = ('95001',)    #元组中只有一个元素的时候需要加一个逗号
cursor.execute(sql % data)
for row in cursor.fetchall():
    print("学号:%s\t姓名:%s\t性别:%s\t年龄:%d" % row)
print('共查找出', cursor.rowcount, '条数据')

# 关闭数据库连接
connect.close()
# mysql6.py
import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host='localhost',
    port=3306,
    user='root',  # 数据库用户名
    passwd='123456',  # 密码
    db='school',
    charset='utf8'
)

# 获取游标
cursor = connect.cursor()

# 删除数据
sql = "DELETE FROM student WHERE sno = '%s'"
data = ('95002',)  #元组中只有一个元素的时候需要加一个逗号
cursor.execute(sql % data)
connect.commit()
print('成功删除', cursor.rowcount, '条数据')

# 关闭数据库连接
connect.close()

core-site.xml文件

<configuration>
     <property>
        <name>fs.default.name</name>
        <value>hdfs://localhost:9000</value>
    </property>
 </configuration>

hdfs-site.xml文件

<configuration>
     <property>
         <name>dfs.replication</name>
         <value>1</value>
     </property>
     <property> 
      <name>dfs.permissions</name> 
      <value>false</value> 
     </property>
    <property>
        <name>dfs.namenode.name.dir</name>
        <value>/C:/hadoop-3.1.3/tmp/namenode</value>
    </property>
    <property>
        <name>dfs.datanode.data.dir</name>
        <value>/C:/hadoop-3.1.3/tmp/datanode</value>
    </property>
</configuration>
set JAVA_HOME=%JAVA_HOME%
set JAVA_HOME=C:\ Java\jdk1.8.0_111
C:\PROGRA~1\Java\jdk1.8.0_111
“C:\Program Files”\Java\jdk1.8.0_111
> cd c:\hadoop-3.1.3\bin
> hdfs namenode -format
> cd c:\hadoop-3.1.3\sbin
> start-dfs.cmd
> cd c:\hadoop-3.1.3\sbin
> stop-dfs.cmd
> cd c:\hadoop-3.1.3\bin
> hadoop fs -mkdir hdfs://localhost:9000/user/
> hadoop fs -mkdir hdfs://localhost:9000/user/xiaoming
> hadoop fs -put C:\test.txt hdfs://localhost:9000/user/xiaoming
> hadoop fs -ls hdfs://localhost:9000/user/xiaoming
> hadoop fs -cat hdfs://localhost:9000/user/xiaoming/test.txt
> hadoop fs -get hdfs://localhost:9000/user/xiaoming/test.txt  C:\test1.txt
> hadoop fs -rm hdfs://localhost:9000/user/xiaoming/test.txt
> hadoop fs -rm -r hdfs://localhost:9000/user/xiaoming