hadoop大数据实践_刘锋的博客

hadoop大数据

一、hadoop连不上网解决:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

重启虚拟机即可。


二、Xshell6连接

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

连接成功!

三、启动服务

重新编辑本机的hosts文件

sudu vim /etc/hosts

在这里插入图片描述

按insert进入编辑模式

在这里插入图片描述

完成修改

在这里插入图片描述

按esc键,退出

:wq 保存

在这里插入图片描述

查看是否修改成功:cat /etc/hosts

在这里插入图片描述

cd ~ 回车

ls

pwd 当前目录

cd … 退回上一级目录

在这里插入图片描述

cd app

ls

在这里插入图片描述

cd hadoop-2.6.0-cdh5.7.0/

ls

在这里插入图片描述

cd sbin

ls

在这里插入图片描述

./start-dfs.sh

在这里插入图片描述

连接成功

在这里插入图片描述

jps 查看进程

在这里插入图片描述

测试连接

用浏览器访问:http://192.168.234.128:50070/

出现这个页面就表示成功

在这里插入图片描述

4、hdfs的shell操作

hadoop fs 设置环境变量

在这里插入图片描述

相关的命令:

hadoop fs -ls /

在这里插入图片描述

在这里插入图片描述

touch taigongyuan.txt 在本机创建一个文件

vim taigongyuan.txt 写文件内容

cat taigongyuan.txt 查看文件内容

hadoop fs -mkdir -p /hadooptaiyuan/test 在hadoop创建一个文件夹

在这里插入图片描述

将本地创建的文件传入Hadoop中

hadoop fs -put taigongyuan.txt /hadooptaiyuan/test

在这里插入图片描述

查看hadoop中的文件中的内容

hadoop fs -cat /hadooptaiyuan/test/taigongyuan.txt

在这里插入图片描述

将hadoop的文件的下载到本地,并且命名为haha.txt

hadoop fs -get /hadooptaiyuan/test/taigongyuan.txt haha.txt

在这里插入图片描述

将文件移动一个位置

hadoop fs -mv /hadooptaiyuan/test/taigongyuan.txt /user 我这里是移动到user目录下面

在这里插入图片描述

删除一个文件

hadoop fs -rm /user/taigongyuan.txt

在这里插入图片描述

五、使用java去操纵hdfs

首先新建一个maven项目

添加maven依赖

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.6.0</version>
</dependency>

新建测试类

private Configuration configuration = null;
private static final String HDFS_PATH = "hdfs://192.168.234.128:8020";

/**
 * java连接hdfs首先需要建立一个连接
 */
@Before
public void setUp() {
    System.out.println("开启连接");
    configuration = new Configuration();
}

/**
 * 释放资源
 */
@After
public void tearDown() {
    System.out.println("关闭连接");
    configuration = null;
}
1、新建一个文件夹
	/**
     * 新建文件夹
     */
    @Test
    public void mkdir() {
        FileSystem fileSystem = null;
        try {
            fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "hadoop");
            fileSystem.mkdirs(new Path("/liufeng666/test1"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileSystem != null) {
                try {
                    fileSystem.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

在这里插入图片描述

2、创建文件
@Test
public void create() {
    FileSystem fileSystem = null;
    FSDataOutputStream fsDataOutputStream = null;
    try {
        fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "hadoop");
        fsDataOutputStream = fileSystem.create(new Path("/liufeng666/test1/hello.txt"));
        fsDataOutputStream.write("hello liufeng jisuanji".getBytes());
        fsDataOutputStream.flush();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fileSystem != null) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fsDataOutputStream != null) {
            try {
                fsDataOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3、重命名文件
/**
 * 重命名文件
 */
@Test
public void rename() {
    FileSystem fileSystem = null;
    Path oldPath = new Path("/liufeng666/test1/hello.txt");
    Path newPath = new Path("/liufeng666/test1/hehe.txt");
    try {
        fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "hadoop");
        fileSystem.rename(oldPath, newPath);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fileSystem != null) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
4、查看文件
/**
 * 查看文件
 */
@Test
public void cat() {
    FileSystem fileSystem = null;
    Path path = new Path("/liufeng666/test1/hehe.txt");
    try {
        fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "hadoop");
        FSDataInputStream is = fileSystem.open(path);
        IOUtils.copyBytes(is, System.out, 1024);
        is.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fileSystem != null) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
5、上传文件
/**
 * 上传文件
 */
@Test
public void uplode() {
    FileSystem fileSystem = null;
    Path localPath = new Path("src/jiachenxia.pdf");
    Path path = new Path("/liufeng666/test1");
    try {
        fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "hadoop");
        fileSystem.copyFromLocalFile(localPath, path);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fileSystem != null) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
6、下载文件
/**
 * 下载文件
 */
@Test
public void download() {
    FileSystem fileSystem = null;
    Path hdfspath = new Path("/liufeng666/test1/jiachenxia.pdf");
    Path localPath = new Path("src/download/jiachenxia.pdf");

    try {
        fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "hadoop");
        fileSystem.copyToLocalFile(false, hdfspath, localPath, true);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fileSystem != null) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

六、可视化yarn和MapReauce

词频统计案例

1、先停止hadoop

./stop-dfs.sh

2、启动所有

./start-all.sh

在这里插入图片描述

3、输入访问8088端口

在这里插入图片描述

实现代码
 /**
     * map阶段
     */
    public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
        LongWritable one = new LongWritable(1);

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            // 分
            String line = value.toString();
            // 拆分
            String[] s = line.split(" ");
            for (String word : s) {
                // 输出
                context.write(new Text(word), one);
            }
        }
    }
/**
     * reduce阶段
     */

    public static class MyReduce extends Reducer<Text, LongWritable, Text, LongWritable> {
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            long sum = 0;
            // 合并统计
            for (LongWritable value : values) {
                sum += value.get();
            }
            context.write(key, new LongWritable(sum));
        }
    }
 public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration, "wordcount");
        job.setJarByClass(WordCountApp.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setReducerClass(MyReduce.class);

        Path outPath = new Path(args[1]);
        FileSystem fileSystem = FileSystem.get(configuration);

        if (fileSystem.exists(outPath)) {
            // 删除文件
            fileSystem.delete(outPath, true);
            System.out.println("输出路径存在,已经被我删除了");
        }

        FileOutputFormat.setOutputPath(job, outPath);


        // 控制台输出详细的信息
        System.out.println(job.waitForCompletion(true) ? 0 : 1);
    }
打包

在这里插入图片描述

放进hadoop中

在这里插入图片描述

运行jar

hadoop jar Desktop/bigdate-1.0-SNAPSHOT.jar neusoft.WordCountApp hdfs://hadoop000:8020/liufeng.txt hdfs://hadoop000:8020/output/wc

在这里插入图片描述

在这里插入图片描述

查看文件内容

hadoop fs -cat /output/wc/part-r-00000

在这里插入图片描述

七、ECharts

首先创建一个html

引入ECharts的js文件

<script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>

为ECharts准备一个具备大小(宽高)的Dom

<div id="main" style="width: 600px;height:400px;">

</div>

柱状图

<script>
    var main = document.getElementById("main");
    var myChart = echarts.init(main);
    // 绘制图表
    var option = myChart.setOption({
        title: {
            text: 'ECharts '
        },
        tooltip: {},
        xAxis: {
            data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [{
            name: '销量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
        }]
    });

    myChart.setOption(option);
</script>

在这里插入图片描述

饼图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
    <!-- 引入主题 -->
    <script src="https://www.runoob.com/static/js/wonderland.js"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 600px;height:400px;">

</div>
<script>
    var main = document.getElementById("main");
    var myChart = echarts.init(main, 'dark');
    // 绘制图表
    var option = myChart.setOption({
            title: {
                text: '饼图'
            }
            ,
            // 工具箱

            show: true,
            legend: {
                orient: 'vertical',
                left: 'right',
                data: ['视频广告', '联盟广告', '邮件营销', '直接访问', '搜索引擎']
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },

            series: [
                {
                    name: '访问来源',
                    type: 'pie',    // 设置图表类型为饼图
                    radius: '75%',  // 饼图的半径,外半径为可视区尺寸(容器高宽中较小一项)的 55% 长度。
                    data: [          // 数据数组,name 为数据项名称,value 为数据项值
                        {value: 235, name: '视频广告'},
                        {value: 274, name: '联盟广告'},
                        {value: 310, name: '邮件营销'},
                        {value: 335, name: '直接访问'},
                        {value: 400, name: '搜索引擎'}
                    ]
                }
            ]

        })
    ;
</script>
</body>
</html>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值