hadoop hdfs的API调用,在mall商城代码中添加api的调用

news2025/2/26 9:33:25

在网上下载了现成的商城代码的源码

本次旨在熟悉hdfs的api调用,不关注前后端代码的编写,所以直接下载现成的代码,代码下载地址。我下载的是前后端在一起的代码,这样测试起来方便

GitHub - newbee-ltd/newbee-mall: 🔥 🎉newbee-mall是一套电商系统,包括基础版本(Spring Boot+Thymeleaf)、前后端分离版本(Spring Boot+Vue 3+Element-Plus+Vue-Router 4+Pinia+Vant 4) 、秒杀版本、Go语言版本、微服务版本(Spring Cloud Alibaba+Nacos+Sentinel+Seata+Spring Cloud Gateway+OpenFeign+ELK)。 前台商城系统包含首页门户、商品分类、新品上线、首页轮播、商品推荐、商品搜索、商品展示、购物车、订单结算、订单流程、个人订单管理、会员中心、帮助中心等模块。 后台管理系统包含数据面板、轮播图管理、商品管理、订单管理、会员管理、分类管理、设置等模块。

我选择的第一个 下载的,明显是前后端没有隔离的版本,而且最简单的,没有加微服务分布式等的

本机配置下hadoop的环境变量等的

否则idea连接hdfs的时候会报错,虽然报错不影响数据存储,但是还是配置下比较好。

官网下载地址:http://archive.apache.org/dist/hadoop/common/hadoop-2.9.2/hadoop-2.9.2.tar.gz

我本机win系统没有解压tar的工具,所以我在虚拟机上将hadoop-2.9.2.tar.gz的包解压缩后,用zip命令压缩了一遍下载到了win的本机

将压缩包解压到/home/admin目录下: tar -zxvf hadoop-2.9.2.tar.gz -C  /home/admin

到目录下打包:

cd /home/admin

zip -r hadoop-2.9.2-all.zip hadoop-2.9.2

如果没有zip的命令则安装一下

yum -y install zip*

下载到win本机,解压然后配置环境变量

不要放到有空格的目录文件夹下,

例如Program Files,我一开始放到这个目录下,配置完环境变量之后,读取不到hadoop的目录,报错D:Program 

下载 windows 环境下所需的winutils.exe文件
hadoop主要基于linux编写,这个winutil.exe主要用于模拟linux下的目录环境。
因此hadoop放在windows下运行的时候,需要这个辅助程序才能运行。

将winutils.exe放到你解压的 hadoop-2.9.2.tar.gz 的bin目录下

下载地址:https://raw.githubusercontent.com/cdarlint/winutils/master/hadoop-2.9.2/bin/winutils.exe

其他版本的winutils.exe下载:https://github.com/cdarlint/winutils

修改D:\hadoop-2.9.2\etc\hadoop\hadoop-env.cmd

配置成功,重启idea即可重新加载hadoop的配置了

下载下来已有的项目源代码之后,我们先将数据库的表导入准备好

先启动自己的虚拟机,在虚拟机上启动mysql

service mysqld start

自己本机的虚拟机记得关机之前将mysql退出,否则下次启动可能会出问题

service mysqld stop

用mysql的客户端连接

在mysql客户端运行商城的sql语句建表以及导入数据

D:\Program Files\IntelliJ IDEA 2023.2.2\workplace\newbee-mall-master\src\main\resources

打开后,添加两行代码,创建数据库以及使用当前创建的数据库

然后运行所有语句

表创建完成

更改mysql连接为自己安装了mysql虚拟机的ip地址,以及用户名和密码都要更改成自己的

虚拟机的安装、mysql在虚拟机上的安装、hadoop分布式安装请参考我前面的几篇文章

启动项目查看项目是否能够正常点页面等的,自己验证下注册登录等的是否能够正常使用,我的可以正常使用

到自己的虚拟机上启动hdfs,一定要记得启动,否则本机项目启动连接会报错,我的hadoop部署的是分布式集群,一台namenode,两台datanode,所以三台会都启动起来

添加hdfs配置以及dao层连接代码

package ltd.newbee.mall.dao;

import ltd.newbee.mall.util.SystemUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.springframework.stereotype.Component;

import java.net.URI;

@Component
public class HdfsDao {
    static Configuration configuration;
    static FileSystem fileSystem;

    static {
        try {
            configuration = new Configuration();
            configuration.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");
            configuration.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");
            fileSystem = FileSystem.get(URI.create(SystemUtil.getProperties().getProperty("spring.hdfs.url")), configuration, "admin");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Configuration getConfiguration() {
        return configuration;
    }

    public static FileSystem getFileSystem() {
        return fileSystem;
    }
}
package ltd.newbee.mall.util;

import org.apache.logging.log4j.util.PropertiesUtil;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * @author 13
 * @qq交流群 796794009
 * @email 2449207463@qq.com
 * @link https://github.com/newbee-ltd
 */
public class SystemUtil {

    static Properties properties;
    private SystemUtil() {
    }

    static {
        properties = new Properties();
        try {
            properties.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("application.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static Properties getProperties() {
        return properties;
    }

    public static String getToday(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(new Date());
    }
}

 添加HdfsService,service添加完成后,记得自测一下,看下hdfs是否连接成功

package ltd.newbee.mall.service;

public interface HdfsService {
    /**
     * json数据到hdfs的业务
     */
    void jsonToHDFS(String line);
}

package ltd.newbee.mall.service.impl;

import ltd.newbee.mall.dao.HdfsDao;
import ltd.newbee.mall.service.HdfsService;
import ltd.newbee.mall.util.SystemUtil;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

import static ltd.newbee.mall.util.SystemUtil.getToday;

@Service
public class HdfsServiceImpl implements HdfsService {
    @Autowired
    private HdfsDao hdfsDao;
    @Override
    public void jsonToHDFS(String line) {
        FileSystem fileSystem = hdfsDao.getFileSystem();
        FSDataOutputStream os = null;
        try {
            String path = SystemUtil.getProperties().getProperty("spring.hdfs.shoppingLog") + "/" + getToday() + ".log";
            if (fileSystem.exists(new Path(path))) {
                os = fileSystem.append(new Path(path));
            } else {
                os = fileSystem.create(new Path(path));
            }
            IOUtils.copyBytes(org.apache.commons.io.IOUtils.toInputStream(line + "\r\n"), os, HdfsDao.getConfiguration(), true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

 

添加hdfs entity

package ltd.newbee.mall.entity;

public class HdfsLog {
    private Long userId;

    private String nickName;

    private String loginName;

    private String address;
    private Long goodsId;

    private String goodsName;

    private String goodsIntro;

    private Long goodsCategoryId;

    private Integer originalPrice;

    private Integer sellingPrice;

    private Integer stockNum;

    private String tag;

    private Byte goodsSellStatus;
    public HdfsLog(){}
    public HdfsLog(Long userId, String nickName, String loginName, String address, Long goodsId, String goodsName, String goodsIntro, Long goodsCategoryId, Integer originalPrice, Integer sellingPrice, Integer stockNum, String tag, Byte goodsSellStatus) {
        this.userId = userId;
        this.nickName = nickName;
        this.loginName = loginName;
        this.address = address;
        this.goodsId = goodsId;
        this.goodsName = goodsName;
        this.goodsIntro = goodsIntro;
        this.goodsCategoryId = goodsCategoryId;
        this.originalPrice = originalPrice;
        this.sellingPrice = sellingPrice;
        this.stockNum = stockNum;
        this.tag = tag;
        this.goodsSellStatus = goodsSellStatus;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Long getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Long goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getGoodsIntro() {
        return goodsIntro;
    }

    public void setGoodsIntro(String goodsIntro) {
        this.goodsIntro = goodsIntro;
    }

    public Long getGoodsCategoryId() {
        return goodsCategoryId;
    }

    public void setGoodsCategoryId(Long goodsCategoryId) {
        this.goodsCategoryId = goodsCategoryId;
    }

    public Integer getOriginalPrice() {
        return originalPrice;
    }

    public void setOriginalPrice(Integer originalPrice) {
        this.originalPrice = originalPrice;
    }

    public Integer getSellingPrice() {
        return sellingPrice;
    }

    public void setSellingPrice(Integer sellingPrice) {
        this.sellingPrice = sellingPrice;
    }

    public Integer getStockNum() {
        return stockNum;
    }

    public void setStockNum(Integer stockNum) {
        this.stockNum = stockNum;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public Byte getGoodsSellStatus() {
        return goodsSellStatus;
    }

    public void setGoodsSellStatus(Byte goodsSellStatus) {
        this.goodsSellStatus = goodsSellStatus;
    }

}

controller中添加hdfs的调用,我添加到了购物车添加成功的接口中

ShoppingCartController
 @PostMapping("/shop-cart")
    @ResponseBody
    public Result saveNewBeeMallShoppingCartItem(@RequestBody NewBeeMallShoppingCartItem newBeeMallShoppingCartItem,
                                                 HttpSession httpSession) {
        NewBeeMallUserVO user = (NewBeeMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        newBeeMallShoppingCartItem.setUserId(user.getUserId());
        String saveResult = newBeeMallShoppingCartService.saveNewBeeMallCartItem(newBeeMallShoppingCartItem);
        //添加成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(saveResult)) {
            NewBeeMallGoods newBeeMallGoods = newBeeMallGoodsService.getNewBeeMallGoodsById(newBeeMallShoppingCartItem.getGoodsId());
            //添加hdfs日志信息
            HdfsLog hdfsLog = new HdfsLog();
            BeanUtils.copyProperties(user,hdfsLog);
            hdfsLog.setGoodsId(newBeeMallGoods.getGoodsId());
            hdfsLog.setGoodsIntro(newBeeMallGoods.getGoodsIntro());
            hdfsLog.setGoodsName(newBeeMallGoods.getGoodsName());
            hdfsLog.setGoodsCategoryId(newBeeMallGoods.getGoodsCategoryId());
            hdfsLog.setGoodsSellStatus(newBeeMallGoods.getGoodsSellStatus());
            Gson gson =  new GsonBuilder()
                    .serializeNulls()
                    .setDateFormat("yyyy-MM-dd HH:mm:ss")
                    .setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)
                    .create();
            hdfsService.jsonToHDFS(gson.toJson(hdfsLog));
            return ResultGenerator.genSuccessResult();
        }
        //添加失败
        return ResultGenerator.genFailResult(saveResult);
    }

 启动项目进行测试,点击一个商品,添加到购物车添加多个商品看下hadoop日志文件

http://192.168.137.16:50070/explorer.html#/logs/shoppingLog

查看一下是不是每次点击一次添加购物车,日志中都会追加一条

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1158656.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

VulnHub DC-2

一、信息收集 1.nmap扫描 扫描开放端口,发现只开启了80、7744端口 ┌──(root💀kali)-[~/桌面] └─# nmap --scriptvuln -p80,7744 192.168.103.190--scriptvuln 可以检查目标主机或网段是否存在常见的漏洞 2.添加hosts 浏览器访问http://192.168.…

ipad协议

逆向工程是一种强大的工具,可以帮助我们理解协议、识别漏洞,并开发兼容的应用程序或扩展。它使我们能够更深入地了解复杂系统并创建创新的解决方案。iPad协议是苹果生态系统不可或缺的一部分,它允许iPad和其他设备之间实现无缝通信。为了理解…

[PyTorch][chapter 58][强化学习-2-有模型学习]

前言: 在已知模型的环境里面学习,称为有模型学习(model-based learning). 此刻,下列参数是已知的: : 在状态x 下面,执行动作a ,转移到状态 的概率 : 在状态x 下面,执行动作a ,转移到 的奖赏 有模型强化学习的应用案例 …

App渗透测试有哪些测试方法?可进行移动app测试的公司推荐

App渗透测试是当前互联网时代中不可或缺的重要环节。随着智能手机的普及和App应用的广泛使用,App渗透测试帮助企业保障其移动应用的安全性和可靠性,确保用户的隐私和敏感信息不会被黑客窃取。那么,什么是App渗透测试呢?它有哪些测试方法?接…

java实现pdf文件添加水印,下载到浏览器

java实现pdf文件添加水印&#xff0c;下载到浏览器 添加itextpdf依赖 <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.8</version> </dependency>文件下载到浏览器和指定路径 …

第四章 数据结构与算法——树与二叉树

一、树的定义 ①&#xff1a;树是一种 非线性 的数据结构&#xff0c;它是由 n &#xff08; n>0 &#xff09;个有限结点组成一个具有层次关系的集合。 ②&#xff1a; 把它叫做树是因 为它看起来像一棵倒挂的树&#xff0c;也就是说它是根朝上&#xff0c;而叶朝下的 。 …

BetterDisplay Pro v1.4.15(显示器管理管理软件)

BetterDisplay Pro是一款屏幕显示优化工具&#xff0c;可用于Windows和Mac操作系统。它可以帮助用户调整屏幕的亮度、对比度、色彩等参数&#xff0c;以获得更好的视觉体验。此外&#xff0c;BetterDisplay Pro还提供了一些额外的功能&#xff0c;如屏幕分割、窗口管理、快捷键…

变量的作用域

在javascript中&#xff0c;var定义的变量实际是有作用域的。 1 假设在函数体内声明&#xff0c;但在函数体外不可以使用 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title> </…

HBase理论与实践-基操与实践

基操 启动&#xff1a; ./bin/start-hbase.sh 连接 ./bin/hbase shell help命令 输入 help 然后 <RETURN> 可以看到一列shell命令。这里的帮助很详细&#xff0c;要注意的是表名&#xff0c;行和列需要加引号。 建表&#xff0c;查看表&#xff0c;插入数据&#…

怎么检测开关电源质量的好坏?测试的方法是什么?

开关电源的工作原理 开关电源(简称SMPS)是常见的一种电源供应器&#xff0c;是高频化的电能转换装置&#xff0c;可以将电压透过不同形式的架构转换为用户端所需求的电压或电流。具有体积小、功耗小、效率高、高可靠性的特点&#xff0c;被广泛应用在工业、军工设备、科研设备、…

SHCTF 未出题目复盘

crypto week1 立正 逆序得到 :V Qook at you~ this is flag: EmxhE8tERKAfYAZ6S636dIWuEK46ZK4yRBdNdK4uRKd4 decode it 中间那串像b64 解码解不出什么 猜测还要rotate Emxh 和 flag编码后的前几位比较接近 考虑大写字母需要继续偏移 对大写字母处理完后得到 :Q Look at you…

当贝PadGO闺蜜机?多的是你不知道的玩法

一、当贝PadGO性能强在哪? 1、金属机身 当贝PadGO独有CD型底盘更有设计风格、后扶手设计更稳,且采用全金属的材质更有质感。并且在配色上还有熊猫白和唱片黑两种可以选择。屏幕采用AG磨砂类纸屏,自带纸张柔和效果,防眩光。并且拥有德国莱茵低蓝光、无频闪双重护眼认证,还可以…

食堂系统登录报错

因为数据库没有任何用户数据&#xff0c;所以会报错&#xff0c;需要添加admin用户 D:\env\jdk1.8.0_341\bin\java.exe -XX:TieredStopAtLevel1 -noverify -Dspring.output.ansi.enabledalways -Dcom.sun.management.jmxremote -Dspring.jmx.enabledtrue -Dspring.liveBeansVie…

“知了杯”网络安全竞赛(南充赛区)圆满落幕

为积极响应网络空间安全人才战略&#xff0c;加快攻防兼备网络创新人才培养步伐&#xff0c;实现以赛促学、以赛促教、以赛促用&#xff0c;推动网络空间安全人才培养和产学研用生态发展&#xff0c;成都知了汇智科技有限公司作为产教融合服务型平台机构&#xff0c;在四川省工…

LeetCode----84. 柱状图中最大的矩形

题目 给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。 求在该柱状图中,能够勾勒出来的矩形的最大面积。 示例 1: 输入:heights = [2,1,5,6,2,3] 输出:10 解释:最大的矩形为图中红色区域,面积为 10 示例 2: 输入: heights =…

中期科技:智慧公厕打造智能化城市设施,提升公共厕所管理与服务体验

智慧公厕是利用先进的技术和创新的解决方案来改进公厕的设施和管理。借助物联网、互联网、5G/4G通信、人工智能、大数据、云计算等新兴技术的集成&#xff0c;智慧公厕具备了一系列令人惊叹的应用功能。从监测公厕内部人体活动状态、人体存在状态&#xff0c;到空气质量情况、环…

【LVS实战】03 LVS负载均衡-DR模式实验

本文介绍&#xff0c;如何通过ipvsadm配置负载均衡&#xff0c;并且转发模式为DR直接路由模式 一、网络拓扑 如下图&#xff1a; LVS负载均衡的机器&#xff0c;配置了一个VIP&#xff1a;10.1.0.7 二、前置准备 每台机器关闭防火墙 systemctl stop firewalldRS装好nginx…

01【保姆级】-GO语言特点和安装使用和hello

01-GO语言基本概念和安装使用 一、概念1.1 Go语言的诞生1.2 GO语言的特点&#xff1a; 二、安装go2.1 安装2.2 安装环境变量 三、下载&安装goland3.1 官网下载3.2 下载后&#xff0c;进行安装&#xff1a; 四、编写Hello&#xff08;详解&#xff09; 如何学习&#xff1a;…

AI视频监控汇聚平台EasyCVR增加算法功能小tips

安防视频监控/视频集中存储/云存储/磁盘阵列EasyCVR平台可拓展性强、视频能力灵活、部署轻快&#xff0c;可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等&#xff0c;以及支持厂家私有协议与SDK接入&#xff0c;包括海康Ehome、海大宇等设备的SDK等&#xff0c;能对外…

【原理】循环水系统【组件介绍】

目录 前言一、工作原理二、主要组件2-1、 保温水箱2-2、 热水泵2-3、 电加热器2-4、 管式换热器2-5、 开关阀、流量阀和调节阀2-6、 冷却塔2-7、 冷水泵 总结 前言 循环水系统是一种用于维持恒定温度或温度范围的工业或建筑设施中的热力控制系统。该系统通过循环水来传递热量&a…