zoukankan      html  css  js  c++  java
  • Hadoop脚本:重新初始化所有节点

    Hadoop脚本:重新初始化所有节点

    在Hadoop集群调试的初期,经常需要重新初始化HDFS,下面的脚本可以实现此点

    #!/usr/bin/php
    <?php

    require_once '/home/hadoop/bin/common/hadoop.inc';

    $masters = f2a(HADOOP_HOME . '/conf/masters');
    $slaves = f2a(HADOOP_HOME . '/conf/slaves');

    foreach($masters as $master)
    {
            echo "Resetting master $master...\n";
            rexec($master, "rm -rf ". HADOOP_NAME_DIR. "/* ");
    }

    foreach($slaves as $slave)
    {
            echo "Resetting slave $slave ...\n";
            rexec($slave, "rm -rf " . HADOOP_TMP_DIR. "/* ". HADOOP_DATA_DIR."/*");

            echo "Clearing $slave logs...\n";
            rexec($slave, "rm -rf " . HADOOP_HOME. "/logs/*");
    }

    ?>

    Hadoop的配置如下:
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

    <!-- Put site-specific property overrides in this file. -->

    <configuration>
    <property>
        <name>fs.default.name</name>
        <value>hdfs://master:9000</value>
    </property>
    <property>
        <name>mapred.job.tracker</name>
        <value>hdfs://master:9001</value>
    </property>
    <property>
        <name>dfs.name.dir</name>
        <value>/home/hadoop/name</value>
    </property>
    <property>
        <name>dfs.data.dir</name>
        <value>/home/hadoop/data</value>
    </property>
    <property>
        <name>dfs.replication</name>
        <value>2</value>
    </property>
    <property>
        <name>hadoop.tmp.dir</name>
        <value>/home/hadoop/tmp/</value>
    </property>
    <property>
        <name>dfs.block.size</name>
        <value>67108864</value>
    </property>
    </configuration>

    Hadoop.inc内容如下:
    <?php

    define('HADOOP_HOME', '/home/hadoop/hadoop-0.18.1');
    define('HADOOP_TMP_DIR','/home/hadoop/tmp');
    define('HADOOP_NAME_DIR','/home/hadoop/name');
    define('HADOOP_DATA_DIR','/home/hadoop/data');

    function rexec($host, $cmd)
    {
            $connection = ssh2_connect($host, 22);
            // ssh2_auth_password($connection, 'username', 'password');
            if (ssh2_auth_pubkey_file($connection, 'hadoop',
                              '/home/hadoop/.ssh/id_dsa.pub',
                              '/home/hadoop/.ssh/id_dsa')) {
                     // echo "Public Key Authentication Successful\n";
            } else {
                    echo("Accessing $host: Public Key Authentication Failed\n");
            return '';
            }
            $stream = ssh2_exec($connection, $cmd);
            /*
            while($line = fgets($stream)) {
                    flush();
                    echo $line."<br />";
            }
            */
            stream_set_blocking($stream, true);
            return stream_get_contents($stream);
    }

    function ping_host($host)
    {
        $connection = @ssh2_connect($host, 22);
            // ssh2_auth_password($connection, 'username', 'password');
        if( $connection === false)
            return false;
            if (@ssh2_auth_pubkey_file($connection, 'hadoop',
                              '/home/hadoop/.ssh/id_dsa.pub',
                              '/home/hadoop/.ssh/id_dsa')) {
                     // echo "Public Key Authentication Successful\n";
            return true;
            } else {
                    // echo("Accessing $host: Public Key Authentication Failed\n");
                    return false;
            }
    }

    function f2a( $filename )
    {
        return file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    }

    ?>     

  • 相关阅读:
    堆排序优化与几个排序算法时间复杂度
    冒泡排序的实现和优化及其与插入,选择排序的比较
    插入排序的实现与优化并和选择排序进行性能比较
    选择排序的实现以及性能测试
    MD5加密
    低功耗蓝牙BLE [学习笔记]
    反编译[学习笔记]
    Android 混淆[学习笔记]
    北大SQL数据库视频课程笔记
    repo/git Android/CyanogenMod srouce code
  • 原文地址:https://www.cnblogs.com/wycg1984/p/1722430.html
Copyright © 2011-2022 走看看