实验3

Posted zyaiyj-www-2003

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验3相关的知识,希望对你有一定的参考价值。

1. 实验任务1
task1.py

源码:

 import random
 
 print(\'用列表存储随机整数: \')
 lst = [random.randint(0, 100) for i in range(5)]
 print(lst)
 
 print(\'\\n用集合存储随机整数: \')
 s1 = random.randint(0, 100) for i in range(5)
 print(s1)
 
 print(\'\\n用集合存储随机整数: \')
 s2 = set()
 while len(s2) < 5:
     s2.add(random.randint(0, 100))
 print(s2)

运行截图:

 问题1:random.randint(0,100) 生成的随机整数范围是 0到100,包括100
问题2:利用 list(range(5)) 生成的有序序列范围是 [0, 1, 2, 3, 4],不包括5;利用 list(range(1,5)) 生成的有序序列范围是 [1, 2, 3, 4],不包括5
问题3:使用line8代码生成的集合s1len(s1)不一定 是5,因为集合会自动去除重复的元素
问题4:使用line12-14生成的集合s2len(s2) 一定是5,因为line13的代码设置的循环条件就是当len(s2)<  5时,循环才会继续执行。

 

2. 实验任务

task2_1.py
源码:

 lst = [55, 92, 88, 79, 96]
 # 遍历方式1: 使用while + 索引
 i = 0
 while i < len(lst):
     print(lst[i], end = \' \')
     i += 1
 print()
 # 遍历方式2:使用for + 索引
 for i in range(len(lst)):
     print(lst[i], end = \' \')
 print()
 # 遍历方式3: 使用for...in
 for i in lst:
     print(i , end = \' \')
 print()

运行截图:

 

task2_2.py
源码:

 # 字典遍历
 book_info = \'isbn\': \'978-7-5356-8297-0\',
              \'书名\': \'白鲸记\',
              \'作者\': \'克里斯多夫.夏布特\',
              \'译者\': \'高文婧\',
              \'出版社\': \'湖南美术出版社\',
              \'售价\': 82
              
 
 # 遍历key-value对: 实现方式1
 for key, value in book_info.items():
     print(f\'key: value\')
 print()
 
 # 遍历key-value对: 实现方法2
 for item in book_info.items():
     print(f\'item[0]: item[1]\')
 print()
 
 # 遍历值: 实现方法1
 for value in book_info.values():
     print(value, end = \' \')
 print()
 book_infos = [\'书名\': \'昨日的世界\', \'作者\': \'斯蒂芬.茨威格\',
               \'书名\': \'局外人\', \'作者\': \'阿尔贝.加缪\',
               \'书名\': \'设计中的设计\', \'作者\': \'原研哉\',
               \'书名\': \'万历十五年\', \'作者\': \'黄仁宇\',
               \'书名\': \'刀锋\', \'作者\': \'毛姆\'
              ]
 n=1
 for i in book_infos:
         print(n,\'.\'\'\'+i[\'书名\']+\'\',i[\'作者\'])
         n=n+1

 

运行截图:

 

 

 

3. 实验任务3
源码:

 

 text  = \'\'\'The Zen of Python, by Tim Peters
 
 Beautiful is better than ugly.
 Explicit is better than implicit.
 Simple is better than complex.
 Complex is better than complicated.
 Flat is better than nested.
 Sparse is better than dense.
 Readability counts.
 Special cases aren\'t special enough to break the rules.
 Although practicality beats purity.
 Errors should never pass silently.
 Unless explicitly silenced.
 In the face of ambiguity, refuse the temptation to guess.
 There should be one-- and preferably only one --obvious way to do it.
 Although that way may not be obvious at first unless you\'re Dutch.
 Now is better than never.
 Although never is often better than *right* now.
 If the implementation is hard to explain, it\'s a bad idea.
 If the implementation is easy to explain, it may be a good idea.
 Namespaces are one honking great idea -- let\'s do more of those!
 \'\'\'
 
 text = text.lower()
 freq = 
 
 for char in text:
 
     if char.isalpha():
 
         if char in freq:
 
             freq[char] += 1
 
         else:
 
             freq[char] = 1
 
 for char, count in sorted(freq.items(), key=lambda x: x[1], reverse=True):
 
     print(char, count)

运行截图:

 4.实验任务4

源码:

 title=\'专业学号信息\'
 print(title.center(50,\'-\'))
 code_majors=8326:\'地信类\',8329:\'计算机类\',8330:\'气科类\',8336:\'防灾工程\',8345:\'海洋科学\',8382:\'气象工程\'
 for key,value in code_majors.items():
     print(f\'key:value\')
 
 while True:
     id = input(\'请输入学号:\')
     if id==\'#\':
         print(\'查询结束...\')
         break
     major = int(id[4:8])
     if major in code_majors.keys():
         print(f\'专业是:code_majors[major]\')
     else:
         print(\'不在这些专业中...\')

运行截图:

 5.实验任务5

 源码:

 import random
 x0 = random.randint(1,31)
 print(\'猜猜2023年5月哪一天会是你的lucky day:~\')
 x  = int(input(\'你有三次机会,猜吧(1-31):\'))
 if x == x0:
     print(\'哇,中了\')
 else:
     for i in range(2):
         if x > x0:
             print(\'猜晚了,你的lucky day 已经过啦\')
             x  = int(input(\'再猜(1-31):\'))
         elif x < x0:
             print(\'猜早了,你的lucky day还没到呢\')
             x  = int(input(\'再猜(1-31):\'))
         else:
             print(\'哇,猜中了~\')
             break
     else:
         print(\'哇哦,次数用光了\')
         print(\'偷偷告诉你,五月你的lucky day 是\',x0,\'\'\'good luck\')

运行截图:

 6.实验任务6

源码:

 datas = \'2049777001\': [\'篮球\', \'羽毛球\', \'美食\', \'漫画\'],
          \'2049777002\': [\'音乐\', \'旅行\'],
          \'2049777003\': [\'马拉松\', \'健身\', \'游戏\'],
          \'2049777004\': [],
          \'2049777005\': [\'足球\', \'阅读\'],
          \'2049777006\': [\'发呆\', \'闲逛\'],
          \'2049777007\': [],
          \'2049777008\': [\'书法\', \'电影\'],
          \'2049777009\': [\'音乐\', \'阅读\', \'电影\', \'漫画\'],
          \'2049777010\': [\'数学\', \'推理\', \'音乐\', \'旅行\']
          
 dict=
 for k in datas.values():
     for i in k:
        dict[i] = dict.get(i, 0) + 1
 a=sorted(dict.items(),key=lambda x:x[1],reverse=True)
 for key,value in a:
     print(f\'key:value\')

运行截图:

 7.实验任务7

task7-1

#欢迎信息
print(\'欢迎使用家用电器销售系统!\')
#商品数据初始化
products=[
    [\'0001\',\'电视机\',\'海尔\',5999.00,20],
    [\'0002\',\'冰箱\',\'西门子\',6998.00,15],
    [\'0003\',\'洗衣机\',\'小天鹅\',1999.00,10],
    [\'0004\',\'空调\',\'格力\',3900.00,0],
    [\'0005\',\'热水器\',\'格力\',688.00,30],
    [\'0006\',\'笔记本\',\'联想\',5699.00,10],
    [\'0007\',\'微波炉\',\'苏泊尔\',480.00,33],
    [\'0008\',\'投影仪\',\'松下\',1250.00,12],
    [\'0009\',\'吸尘器\',\'飞利浦\',999.00,9],
]


#初始化用户购物车
products_cart = []

option = input(\'请选择您的操作:1-查看商品;2-购物;3—查看购物车;其他-结账\')

while option in[\'1\',\'2\',\'3\']:
    if option == \'1\':
        #产品信息列表
        print(\'产品和价格信息如下:\')
        print(\'************************************************\')
        print(\'%-10s\'%\'编号\',\'%-10s\'%\'名称\',\'%-10s\'%\'品牌\',\'%-10s\'%\'价格\',\'%-10s\'%\'库存数量\')
        print(\'-------------------------------------------------\')
        for i in range(len(products)):
            print(\'%-10s\'%products[i][0],\'%-10s\'%products[i][1],\'%-10s\'
            %products[i][2],
            \'%10.2f\'%products[i][3],
            \'%10d\'%products[i][4])
        print(\'-------------------------------------------------\')
    elif option == \'2\':
        product_id = input(\'请输入您要购买的产品编号:\')
        while product_id not in [item[0] for item in products]:
            product_id = input(\'编号不存在,请重新输入您要购买的产品编号:\')

        count = int(input(\'请输入您要购买的产品数量:\'))
        while count>products[int(product_id)-1][4]:
            count=int(input(\'数量超出库存,请重新输入您要购买的产品数量:\'))
        #将所购买的商品加入购物车
        if product_id not in [item[0] for item in products_cart]:
            products_cart.append([product_id,count])
        else:
            for i in range(len(products_cart)):
                if products_cart[i][0] == product_id:
                    products_cart[i][1]+=count
        #更新商品列表
        for i in range(len(products)):
            if products[i][0] == product_id:
                products[i][4] -= count
    else:
        print(\'购物车信息如下:\')
        print(\'*************************************\')
        print(\'%-10s\'%\'编号\',\'%-10s\'%\'购买数量\')
        print(\'-------------------------------------\')
        for i in range(len(products_cart)):
            print(\'%-10s\'%products_cart[i][0],\'%6d\'%products_cart[i][1])
        print(\'-------------------------------------\')
    option = input(\'操作成功!请选择你的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\')
#计算金额
if len(products_cart)>0:
    amount = 0
    for i in range(len(products_cart)):
        product_index = 0
        for j in range(len(products)):
            if products[j][0] == products_cart[i][0]:
                product_index = j
                break
        price = products[product_index][3]
        count = products_cart[i][1]
        amount+=price*count

    if 5000<amount<=10000:
        amount = amount * 0.95
    elif 10000<amount<=20000:
        amount = amount * 0.90
    elif amount > 20000:
        amount = amount*0.85
    else:
        amount = amount*1
    print(\'购买成功,您需要支付%8.2f元\'%amount)

#退出系统
print(\'谢谢您的光临,下次再见!\')

运行截图:

 task7-2

源码:

 #欢迎信息
 print(\'欢迎使用家用电器销售系统!\')
 #商品数据初始化
 products=[
     [\'0001\',\'电视机\',\'海尔\',5999.00,20],
     [\'0002\',\'冰箱\',\'西门子\',6998.00,15],
     [\'0003\',\'洗衣机\',\'小天鹅\',1999.00,10],
     [\'0004\',\'空调\',\'格力\',3900.00,0],
     [\'0005\',\'热水器\',\'格力\',688.00,30],
     [\'0006\',\'笔记本\',\'联想\',5699.00,10],
     [\'0007\',\'微波炉\',\'苏泊尔\',480.00,33],
     [\'0008\',\'投影仪\',\'松下\',1250.00,12],
     [\'0009\',\'吸尘器\',\'飞利浦\',999.00,9],
 ]
 
 
 #初始化用户购物车
 products_cart = []
 
 option = input(\'请选择您的操作:1-查看商品;2-购物;3—查看购物车;其他-结账\')
 
 while option in[\'1\',\'2\',\'3\']:
     if option == \'1\':
         #产品信息列表
         print(\'产品和价格信息如下:\')
         print(\'************************************************\')
         print(print(\':<10\'.format(\'编号\'),\':<10\'.format(\'名称\'),\':<10\'.format(\'品牌\'),\':<10\'.format(\'价格\'),\':<10\'.format(\'库存数量\')))
         print(\'-------------------------------------------------\')
         for i in range(len(products)):
             print(\':<10\'.format(products[i][0]),\':<10\'.format(products[i][1]),\':<10\'.format
             (products[i][2]),
             \':<10\'.format(products[i][3]),
             \':<10\'.format(products[i][4]))
         print(\'-------------------------------------------------\')
     elif option == \'2\':
         product_id = input(\'请输入您要购买的产品编号:\')
         while product_id not in [item[0] for item in products]:
             product_id = input(\'编号不存在,请重新输入您要购买的产品编号:\')
 
         count = int(input(\'请输入您要购买的产品数量:\'))
         while count>products[int(product_id)-1][4]:
             count=int(input(\'数量超出库存,请重新输入您要购买的产品数量:\'))
         #将所购买的商品加入购物车
         if product_id not in [item[0] for item in products_cart]:
             products_cart.append([product_id,count])
         else:
             for i in range(len(products_cart)):
                 if products_cart[i][0] == product_id:
                     products_cart[i][1]+=count
         #更新商品列表
         for i in range(len(products)):
             if products[i][0] == product_id:
                 products[i][4] -= count
     else:
         print(\'购物车信息如下:\')
         print(\'*************************************\')
         print(\':<10\'.format(\'编号\'),\':<10\'.format(\'购买数量\'))
         print(\'-------------------------------------\')
         for i in range(len(products_cart)):
             print(\':<10\'.format(products_cart[i][0]),\':<6\'.format(products_cart[i][1]))
         print(\'-------------------------------------\')
     option = input(\'操作成功!请选择你的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\')
 #计算金额
 if len(products_cart)>0:
     amount = 0
     for i in range(len(products_cart)):
         product_index = 0
         for j in range(len(products)):
             if products[j][0] == products_cart[i][0]:
                 product_index = j
                 break
         price = products[product_index][3]
         count = products_cart[i][1]
         amount+=price*count
 
     if 5000<amount<=10000:
         amount = amount * 0.95
     elif 10000<amount<=20000:
         amount = amount * 0.90
     elif amount > 20000:
         amount = amount*0.85
     else:
         amount = amount*1
     print(\'购买成功,您需要支付:.2f元\'.format(amount))
 
 #退出系统
 print(\'谢谢您的光临,下次再见!\')

运行截图:

 8.实验任务8

 task8-1

源码:

 #欢迎信息
 print(\'欢迎使用家用电器销售系统!\')
 #商品数据初始化
 products=[
     \'id\':\'0001\',\'name\':\'电视机\',\'brand\':\'海尔\',\'price\':5999.00,\'count\':20,
     \'id\':\'0002\',\'name\':\'冰箱\',\'brand\':\'西门子\',\'price\':6998.00,\'count\':15,
     \'id\':\'0003\',\'name\':\'洗衣机\',\'brand\':\'小天鹅\',\'price\':1999.00,\'count\':10,
     \'id\':\'0004\',\'name\':\'空调\',\'brand\':\'格力\',\'price\':3900.00,\'count\':0,
     \'id\':\'0005\',\'name\':\'热水器\',\'brand\':\'格力\',\'price\':688.00,\'count\':30,
     \'id\':\'0006\',\'name\':\'笔记本\',\'brand\':\'联想\',\'price\':5699.00,\'count\':10,
     \'id\':\'0007\',\'name\':\'微波炉\',\'brand\':\'苏泊尔\',\'price\':480.00,\'count\':33,
     \'id\':\'0008\',\'name\':\'投影仪\',\'brand\':\'松下\',\'price\':1250.00,\'count\':12,
     \'id\':\'0009\',\'name\':\'吸尘器\',\'brand\':\'飞利浦\',\'price\':999.00,\'count\':9,
 ]
 
 
 #初始化用户购物车
 products_cart = []
 
 option = input(\'请选择您的操作:1-查看商品;2-购物;3—查看购物车;其他-结账\')
 
 while option in[\'1\',\'2\',\'3\']:
     if option == \'1\':
         #产品信息列表
         print(\'产品和价格信息如下:\')
         print(\'************************************************\')
         print(\'%-10s\'%\'编号\',\'%-10s\'%\'名称\',\'%-10s\'%\'品牌\',\'%-10s\'%\'价格\',\'%-10s\'%\'库存数量\')
         print(\'-------------------------------------------------\')
         for i in range(len(products)):
             print(\'%-10s\'%products[i][\'id\'],\'%-10s\'%products[i][\'name\'],
             (products[i][\'brand\']),
             \'%-10.2f\'%products[i][\'price\'],
             \'%-10d\'%products[i][\'count\'])
         print(\'-------------------------------------------------\')
     elif option == \'2\':
         product_id = input(\'请输入您要购买的产品编号:\')
         while product_id not in [item[\'id\'] for item in products]:
             product_id = input(\'编号不存在,请重新输入您要购买的产品编号:\')
 
         count = int(input(\'请输入您要购买的产品数量:\'))
         while count>products[int(product_id)-1][\'count\']:
             count=int(input(\'数量超出库存,请重新输入您要购买的产品数量:\'))
         #将所购买的商品加入购物车
         if product_id not in [item[\'id\'] for item in products_cart]:
             products_cart.append(\'id\':product_id,\'count\':count)
         else:
             for i in range(len(products_cart)):
                 if products_cart[i].get(\'id\') == product_id:
                     products_cart[i][\'count\']+=count
         #更新商品列表
         for i in range(len(products)):
             if products[i][\'id\'] == product_id:
                 products[i][\'count\'] -= count
     else:
         print(\'购物车信息如下:\')
         print(\'*************************************\')
         print(\'%-10s\'%\'编号\',\'%-10s\'%\'购买数量\')
         print(\'-------------------------------------\')
         for i in range(len(products_cart)):
             print(\'%-10s\'%products_cart[i][\'id\'],\'%6d\'%products_cart[i][\'count\'])
         print(\'-------------------------------------\')
     option = input(\'操作成功!请选择你的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\')
 #计算金额
 if len(products_cart)>0:
     amount = 0
     for i in range(len(products_cart)):
         product_index = 0
         for j in range(len(products)):
             if products[j][\'id\'] == products_cart[i][\'id\']:
                 product_index = j
                 break
         price = products[product_index][\'price\']
         count = products_cart[i][\'count\']
         amount+=price*count
 
     if 5000<amount<=10000:
         amount = amount * 0.95
     elif 10000<amount<=20000:
         amount = amount * 0.90
     elif amount > 20000:
         amount = amount*0.85
     else:
         amount = amount*1
     print(\'购买成功,您需要支付:.2f元\'.format(amount))
 
 #退出系统
 print(\'谢谢您的光临,下次再见!\')

运行截图:

 task8-2

源码:

 #欢迎信息
 print(\'欢迎使用家用电器销售系统!\')
 #商品数据初始化
 products=[
     \'id\':\'0001\',\'name\':\'电视机\',\'brand\':\'海尔\',\'price\':5999.00,\'count\':20,
     \'id\':\'0002\',\'name\':\'冰箱\',\'brand\':\'西门子\',\'price\':6998.00,\'count\':15,
     \'id\':\'0003\',\'name\':\'洗衣机\',\'brand\':\'小天鹅\',\'price\':1999.00,\'count\':10,
     \'id\':\'0004\',\'name\':\'空调\',\'brand\':\'格力\',\'price\':3900.00,\'count\':0,
     \'id\':\'0005\',\'name\':\'热水器\',\'brand\':\'格力\',\'price\':688.00,\'count\':30,
     \'id\':\'0006\',\'name\':\'笔记本\',\'brand\':\'联想\',\'price\':5699.00,\'count\':10,
     \'id\':\'0007\',\'name\':\'微波炉\',\'brand\':\'苏泊尔\',\'price\':480.00,\'count\':33,
     \'id\':\'0008\',\'name\':\'投影仪\',\'brand\':\'松下\',\'price\':1250.00,\'count\':12,
     \'id\':\'0009\',\'name\':\'吸尘器\',\'brand\':\'飞利浦\',\'price\':999.00,\'count\':9,
 ]
 
 
 #初始化用户购物车
 products_cart = []
 
 option = input(\'请选择您的操作:1-查看商品;2-购物;3—查看购物车;其他-结账\')
 
 while option in[\'1\',\'2\',\'3\']:
     if option == \'1\':
         #产品信息列表
         print(\'产品和价格信息如下:\')
         print(\'************************************************\')
         print(print(\':<10\'.format(\'编号\'),\':<10\'.format(\'名称\'),\':<10\'.format(\'品牌\'),\':<10\'.format(\'价格\'),\':<10\'.format(\'库存数量\')))
         print(\'-------------------------------------------------\')
         for i in range(len(products)):
             print(\':<10\'.format(products[i][\'id\']),\':<10\'.format(products[i][\'name\']),\':<10\'.format
             (products[i][\'brand\']),
             \':<10\'.format(products[i][\'price\']),
             \':<10\'.format(products[i][\'count\']))
         print(\'-------------------------------------------------\')
     elif option == \'2\':
         product_id = input(\'请输入您要购买的产品编号:\')
         while product_id not in [item[\'id\'] for item in products]:
             product_id = input(\'编号不存在,请重新输入您要购买的产品编号:\')
 
         count = int(input(\'请输入您要购买的产品数量:\'))
         while count>products[int(product_id)-1][\'count\']:
             count=int(input(\'数量超出库存,请重新输入您要购买的产品数量:\'))
         #将所购买的商品加入购物车
         if product_id not in [item[\'id\'] for item in products_cart]:
             products_cart.append(\'id\':product_id,\'count\':count)
         else:
             for i in range(len(products_cart)):
                 if products_cart[i].get(\'id\') == product_id:
                     products_cart[i][\'count\']+=count
         #更新商品列表
         for i in range(len(products)):
             if products[i][\'id\'] == product_id:
                 products[i][\'count\'] -= count
     else:
         print(\'购物车信息如下:\')
         print(\'*************************************\')
         print(\':<10\'.format(\'编号\'),\':<10\'.format(\'购买数量\'))
         print(\'-------------------------------------\')
         for i in range(len(products_cart)):
             print(\':<10\'.format(products_cart[i][\'id\']),\':<6\'.format(products_cart[i][\'count\']))
         print(\'-------------------------------------\')
     option = input(\'操作成功!请选择你的操作:1-查看商品;2-购物;3-查看购物车;其他-结账\')
 #计算金额
 if len(products_cart)>0:
     amount = 0
     for i in range(len(products_cart)):
         product_index = 0
         for j in range(len(products)):
             if products[j][\'id\'] == products_cart[i][\'id\']:
                 product_index = j
                 break
         price = products[product_index][\'price\']
         count = products_cart[i][\'count\']
         amount+=price*count
 
     if 5000<amount<=10000:
         amount = amount * 0.95
     elif 10000<amount<=20000:
         amount = amount * 0.90
     elif amount > 20000:
         amount = amount*0.85
     else:
         amount = amount*1
     print(\'购买成功,您需要支付:.2f元\'.format(amount))
 
 #退出系统
 print(\'谢谢您的光临,下次再见!\')

运行截图

 

 

大数据Hadoop实验报告

实验一 熟悉常用的Linux操作和Hadoop操作

1.实验目的

Hadoop运行在Linux系统上,因此,需要学习实践一些常用的Linux命令。本实验旨在熟悉常用的Linux操作和Hadoop操作,为顺利开展后续其他实验奠定基础。

2.实验平台

  • 操作系统:Linux;
  • Hadoop版本:2.7.1。

3.实验内容和要求

(一)熟悉常用的Linux操作
请按要求上机实践如下linux基本命令。

cd命令:切换目录

(1)切换到目录 /usr/local
在这里插入图片描述

(2)切换到当前目录的上一级目录
在这里插入图片描述

(3)切换到当前登录Linux系统的用户的自己的主文件夹
在这里插入图片描述

ls命令:查看文件与目录

(4)查看目录/usr下所有的文件
在这里插入图片描述

mkdir命令:新建新目录

(5)进入“/tmp”目录,创建一个名为“a”的目录,并查看“/tmp”目录下已经存在哪些目录
在这里插入图片描述

(6)进入“/tmp”目录,创建目录“a1/a2/a3/a4”
在这里插入图片描述

rmdir命令:删除空的目录
(7)将上面创建的目录a(在“/tmp”目录下面)删除
在这里插入图片描述

(8)删除上面创建的目录“a1/a2/a3/a4” (在“/tmp”目录下面),然后查看“/tmp”目录下面存在哪些目录
在这里插入图片描述

cp命令:复制文件或目录

(9)将当前用户的主文件夹下的文件.bashrc复制到目录“/usr”下,并重命名为bashrc1
在这里插入图片描述

(10)在目录“/tmp”下新建目录test,再把这个目录复制到“/usr”目录下
在这里插入图片描述

mv命令:移动文件与目录,或更名

(11)将“/usr”目录下的文件bashrc1移动到“/usr/test”目录下
在这里插入图片描述

(12)将“/usr”目录下的test目录重命名为test2
在这里插入图片描述

rm命令:移除文件或目录

(13)将“/usr/test2”目录下的bashrc1文件删除
$ sudo rm /usr/test2/bashrc1
在这里插入图片描述

(14)将“/usr”目录下的test2目录删除
$ sudo rm –r /usr/test2
在这里插入图片描述

cat命令:查看文件内容

(15)查看当前用户主文件夹下的.bashrc文件内容
在这里插入图片描述

tac命令:反向查看文件内容

(16)反向查看当前用户主文件夹下的.bashrc文件的内容
在这里插入图片描述

more命令:一页一页翻动查看

(17)翻页查看当前用户主文件夹下的.bashrc文件的内容
在这里插入图片描述

head命令:取出前面几行

(18)查看当前用户主文件夹下.bashrc文件内容前20行
在这里插入图片描述

(19)查看当前用户主文件夹下.bashrc文件内容,后面50行不显示,只显示前面几行
在这里插入图片描述

tail命令:取出后面几行

(20)查看当前用户主文件夹下.bashrc文件内容最后20行
在这里插入图片描述

(21) 查看当前用户主文件夹下.bashrc文件内容,并且只列出50行以后的数据
在这里插入图片描述

touch命令:修改文件时间或创建新文件

(22)在“/tmp”目录下创建一个空文件hello,并查看文件时间
在这里插入图片描述

(23)修改hello文件,将文件时间整为5天前

chown命令:修改文件所有者权限

在这里插入图片描述

(24)将hello文件所有者改为root帐号,并查看属性
在这里插入图片描述

find命令:文件查找

(25)找出主文件夹下文件名为.bashrc的文件
在这里插入图片描述

tar命令:压缩命令

(26)在根目录“/”下新建文件夹test,然后在根目录“/”下打包成test.tar.gz
在这里插入图片描述

(27)把上面的test.tar.gz压缩包,解压缩到“/tmp”目录
$ sudo tar -zxv -f /test.tar.gz -C /tmp
在这里插入图片描述

grep命令:查找字符串

(28)从“~/.bashrc”文件中查找字符串’examples’
在这里插入图片描述

(29)请在“~/.bashrc”中设置,配置Java环境变量
在这里插入图片描述

(30)查看JAVA_HOME变量的值
在这里插入图片描述

(二)熟悉常用的Hadoop操作
(31)使用hadoop用户登录Linux系统,启动Hadoop(Hadoop的安装目录为“/usr/local/hadoop”),为hadoop用户在HDFS中创建用户目录“/user/hadoop”
在这里插入图片描述

(32)接着在HDFS的目录“/user/hadoop”下,创建test文件夹,并查看文件列表
在这里插入图片描述

(33)将Linux系统本地的“~/.bashrc”文件上传到HDFS的test文件夹中,并查看test
在这里插入图片描述

(34)将HDFS文件夹test复制到Linux系统本地文件系统的“/usr/local/hadoop”目录下
在这里插入图片描述

实验二 熟悉常用的HDFS操作

1.实验目的

  • 理解HDFS在Hadoop体系结构中的角色;
  • 熟练使用HDFS操作常用的Shell命令;

2.实验平台

  • 操作系统:Linux(建议Ubuntu16.04);
  • Hadoop版本:2.7.1;
  • JDK版本:1.7或以上版本;
  • Java IDE:Eclipse。

3.实验步骤

(一)编程实现以下功能,并利用Hadoop提供的Shell命令完成相同任务:
(1)向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,则由用户来指定是追加到原有文件末尾还是覆盖原有的文件;

Shell命令:
追加到末尾

hadoop fs -appendToFile /usr/local/hadoop/test.txt /user/text.txt

覆盖原文件

hadoop fs -copyFromLocal -f /usr/local/hadoop/test.txt /user/text.txt

Java代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*; 
public class HDFSApi { 
      public static boolean test(Configuration conf, String path) throws IOException {         FileSystem fs = FileSystem.get(conf);       
          return fs.exists(new Path(path)); 
    } 
        public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException { 
        FileSystem fs = FileSystem.get(conf); 
        Path localPath = new Path(localFilePath); 
        Path remotePath = new Path(remoteFilePath);             fs.copyFromLocalFile(false, true, localPath, remotePath);      
   fs.close(); 
    } 
  public static void appendToFile(Configuration conf, String  localFilePath, String remoteFilePath) throws IOException { 
        FileSystem fs = FileSystem.get(conf); 
        Path remotePath = new Path(remoteFilePath); 
        FileInputStream in = new FileInputStream(localFilePath); 
        FSDataOutputStream out = fs.append(remotePath); 
   byte[] data = new byte[1024];      
   int read = -1;         
while ( (read = in.read(data)) > 0 ) {          	out.write(data, 0, read); 
        }         
out.close();         in.close();         fs.close(); 
    } 
public static void main(String[] args) { 
 	 	Configuration conf = new Configuration();     conf.set("fs.default.name","hdfs://localhost:9000"); 
 	 	String localFilePath = "/home/hadoop/text.txt";   
 	 	String remoteFilePath = "/user/hadoop/text.txt";    
 	 	String choice = "append";
	 	String choice = "overwrite";    	 
 	 	try {  	 	 	 	 	
Boolean fileExists = false;  	 	 	
if (HDFSApi.test(conf, remoteFilePath)) {  	 	 	 	
fileExists = true; 
 System.out.println(remoteFilePath + " 已存在.");  	
 	 } else { 
 	 	 	 	System.out.println(remoteFilePath + " 不存在."); 
 	 	 	} 
 	 	 	if ( !fileExists) { // 文件不存在,则上传 
 	 	 	 	HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); 
 	 	 	 	System.out.println(localFilePath + " 已上传至 " + remoteFilePath); 
 	 	 	} else if ( choice.equals("overwrite") ) {    // 选择覆盖 
 	 	 	 	HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); 
 	 	 	 	System.out.println(localFilePath + " 已覆盖 " + remoteFilePath); 
	 	} else if ( choice.equals("append") ) {   // 选择追加 
 	 	 	 	HDFSApi.appendToFile(conf, localFilePath, remoteFilePath); 
 	 	 	 	System.out.println(localFilePath + " 已追加至 " + remoteFilePath); 
 	 	 	} 
 	 	} catch (Exception e) { 
 	 	 	e.printStackTrace(); 
 	 	} 
 	} 
} 

(2)从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;

Shell命令:

if $(hadoop fs -test -e /usr/local/hadoop/test.txt);
then $(hadoop fs -copyToLocal /user/test.txt /usr/local/hadoop/test.txt); 
else $(hadoop fs -copyToLocal /user/test.txt /usr/local/hadoop/test2.txt); 

Java代码:

Import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.*; 
import java.io.*; 
public class HDFSApi { 
public 	static 	void 	copyToLocal(Configuration 	conf, 	String 	remoteFilePath, localFilePath) throws IOException { 
      FileSystem fs = FileSystem.get(conf); 
        Path remotePath = new Path(remoteFilePath); 
        File f = new File(localFilePath); 
if(f.exists()) { 
         	System.out.println(localFilePath + " 已存在.");          	
Integer i = 0;          	
while (true) {          	 	
f = new File(localFilePath + "_" + i.toString());          	 	
if (!f.exists()) {          	 	 	
localFilePath = localFilePath + "_" + i.toString(); 
         break; 
        }
} 
System.out.println("将重新命名为: " + localFilePath); ()); 
 }        Path localPath = new Path(localFilePath);       
 fs.copyToLocalFile(remotePath, localPath);       
 fs.close(); 
    } 
 	public static void main(String[] args) { 
 	 	Configuration conf = new Configuration();     conf.set("fs.default.name","hdfs://localhost:9000"); 
 	 String localFilePath = "/home/hadoop/text.txt";    
String remoteFilePath = "/user/hadoop/text.txt";   
try { 
 	HDFSApi.copyToLocal(conf, remoteFilePath, localFilePath); 
 	System.out.println("下载完成"); 
} catch (Exception e) { 
 	e.printStackTrace(); 
}
}
}

(3)将HDFS中指定文件的内容输出到终端中;

Shell命令:

hadoop fs -cat text.txt

Java代码:

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*; 
 
public class HDFSApi { 
    /** 
     * 读取文件内容 
     */ 
    public static void cat(Configuration conf, String remoteFilePath) throws IOException { 
        FileSystem fs = FileSystem.get(conf); 
        Path remotePath = new Path(remoteFilePath); 
        FSDataInputStream in = fs.open(remotePath); 
        BufferedReader d = new BufferedReader(new InputStreamReader(in));         String line = null; 
        while ( (line = d.readLine()) != null ) { 
         	System.out.println(line); 
        } 
       d.close();        in.close();        fs.close(); 
    } 
 	public static void main(String[] args) { 
 	 	Configuration conf = new Configuration();     conf.set("fs.default.name","hdfs://localhost:9000");
String remoteFilePath = "/user/local/hadoop/text.txt";    // HDFS 路径
try { 
 	System.out.println("读取文件: " + remoteFilePath); 
 	HDFSApi.cat(conf, remoteFilePath); 
 	System.out.println("\\n 读取完成"); 
} catch (Exception e) { 
 	e.printStackTrace(); 
}
}
}

(4)显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;

Shell命令:

hadoop fs -ls -h /user/test.txt

Java代码:

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*; import java.text.SimpleDateFormat; 
public class HDFSApi { 
    public static void ls(Configuration conf, String remoteFilePath) throws IOException { 
        FileSystem fs = FileSystem.get(conf); 
        Path remotePath = new Path(remoteFilePath);         FileStatus[] fileStatuses = fs.listStatus(remotePath);         for (FileStatus s : fileStatuses) { 
         	System.out.println("路径: " + s.getPath().toString()); 
         	System.out.println("权限: " + s.getPermission().toString()); 
         	System.out.println("大小: " + s.getLen()); 
         	/* 返回的是时间戳,转化为时间日期格式 */ 
         	Long timeStamp = s.getModificationTime(); 
         	SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
         	String date = format.format(timeStamp);   
   	System.outprintln("时间: " + date); }         fs.close(); 
    }
public static void main(String[] args) {  	 	Configuration conf = new Configuration();     conf.set("fs.default.name","hdfs://localhost:9000"); 
 	 	String remoteFilePath = "/user/hadoop/text.txt";     
 	 	try { 
 	 	 	System.out.println("读取文件信息: " + remoteFilePath); 
	 	 	HDFSApi.ls(conf, remoteFilePath); 
	                          System.out.println("\\n 读取完成"); 
} catch (Exception e) { 
 	 	 	e.printStackTrace(); 
}
}
} 

(5)给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;

Shell命令:

hadoop fs -ls -R -h /user

Java代码

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*; 
import java.text.SimpleDateFormat; 
 
public class HDFSApi { 
    /** 
     * 显示指定文件夹下所有文件的信息(递归) 
     */ 
    public static void lsDir(Configuration conf, String remoteDir) throws IOException { 
        FileSystem fs = FileSystem.get(conf); 
        Path dirPath = new Path(remoteDir); 
        /* 递归获取目录下的所有文件 */ 
        RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(dirPath, true); 
        /* 输出每个文件的信息 */ 
        while (remoteIterator.hasNext()) { 
         	FileStatus s = remoteIterator.next(); 
            System.out.println("路径: " + s.getPath().toString()); 
            System.out.println("权限: " + s.getPermission().toString());
            System.out.println("大小: " + s.getLen()); 
         	/* 返回的是时间戳,转化为时间日期格式 */ 
         	Long timeStamp = s.getModificationTime(); 
         	SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
         	String date = format.format(timeStamp);   
         	System.out.println("时间: " + date); 
         	System.out.println(); 
        }         fs.close(); 
    }     
     
 	哈工大 计算机系统 实验六

实验3正篇——用户进程

实验3正篇——用户进程

实验3

实验二

大数据Hadoop实验报告