IT技術互動交流平台

HBase偽分布式集群安裝及代碼測試

來源_-118彩票是不是正规的:IT165收集  發布日期|_|艾尔之光狂心武者:2016-07-21 21:37:03

一|_雷霆扫毒谁是黑警、HBase偽分布式集群安裝

1|__中央电视台少儿节目、安裝包解壓

$ cd app/

$ tar -xvfhbase-1.2.0-cdh5.7.1.tar.gz

$ rmhbase-1.2.0-cdh5.7.1.tar.gz

 

2|--张檬陈楚河、添加環境變量

$ cd ~

$ vim .bashrc

exportHBASE_HOME=/home/developer/app/hbase-1.2.0-cdh5.7.1

exportPATH=$PATH:$HBASE_HOME/bin

$ source .bashrc

 

3|_盈彩靠谱吗?、編輯hbase-env.sh文件

$ cdapp/hbase-1.2.0-cdh5.7.1/conf/

$ vim hbase-env.sh

exportJAVA_HOME=/home/developer/app/jdk1.7.0_79

export HBASE_CLASSPATH=/home/developer/app/hadoop-2.6.0-cdh5.7.1/etc/hadoop

 

4_|雨采、編輯hbase-site.xml 文件

$ vim hbase-site.xml

<configuration>
 <property>
    <name>hbase.rootdir</name>
    <value>hdfs://localhost:9000/hbase</value>
  </property>

  <property>
    <name>hbase.cluster.distributed</name>
    <value>true</value>
  </property>

 <property>
    <name>hbase.tmp.dir</name>
    <value>/home/developer/app/hbase-1.2.0-cdh5.7.1/tmp</value>
  </property>

  <property>
    <name>dfs.replication</name>
    <value>1</value>
  </property>

  <property>
    <name>hbase.zookeeper.quorum</name>
    <value>localhost</value>
  </property>

  <property>
    <name>hbase.zookeeper.property.clientPort</name>
    <value>2222</value>
  </property>
</configuration>

5||优乐彩彩票官网、啟動

$ start-hbase.sh

 

6谷子林、關閉

$ stop-hbase.sh

 

7|_腾讯a股大赛2013、Web UI

http://localhost:60010

 

8_-_双线盗毒蛾、解決HBase中SLF4J的jar包與Hadoop衝突

問題描述304 不锈钢价格:

SLF4J: Class path containsmultiple SLF4J bindings.

SLF4J: Found binding in[jar:file:/home/developer/app/hbase-1.2.0-cdh5.7.1/lib /slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in[jar:file:/home/developer/app/hadoop-2.6.0-cdh5.7.1/share/hadoop/ common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Seehttp://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is oftype [org.slf4j.impl.Log4jLoggerFactory]

解決方法_|永久免费网游排行榜:

$ cdapp/hbase-1.2.0-cdh5.7.1/lib/

$ rm slf4j-log4j12-1.7.5.jar

二__234彩票安全吗、代碼測試

1||盈发彩票介绍、maven依賴

    <repositories>
        <repository>
          <id>cloudera</id>
          <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>3.8.1</version>
           <scope>test</scope>
        </dependency>
        <dependency> 
           <groupId>org.apache.hadoop</groupId> 
           <artifactId>hadoop-common</artifactId> 
           <version>2.6.0-cdh5.7.1</version> 
        </dependency> 
        <dependency> 
           <groupId>org.apache.hadoop</groupId> 
           <artifactId>hadoop-hdfs</artifactId> 
           <version>2.6.0-cdh5.7.1</version> 
        </dependency>
        <dependency> 
            <groupId>org.apache.hbase</groupId> 
            <artifactId>hbase-client</artifactId> 
            <version>1.2.0-cdh5.7.1</version> 
        </dependency>
    </dependencies>

2--_云顶娱乐怎样注册账号、測試代碼

package com.hbase.demo;

import java.io.IOException;

public class HBaseTest {
    
    private static final String TABLE_NAME = "students";
    private static final String FAMILY_COL_SCORE = "score";
    private static final String FAMILY_COL_INFO = "info";
    
    private Configuration conf;
    
    public static void main(String[] args) {
        
        HBaseTest test = new HBaseTest();
        test.init();
        test.createTable();
        test.insertData();
        test.scanTable();
        test.queryByRowKey();
//        test.deleteRow();
//        test.deleteFamily();
//        test.deleteTable();
    }
    
    /**
     * 初始化hbase的配置信息,也可以將hbase的配置文件hbase-site.xml引入項目_亿博网赌,則不需要在代碼中填寫配置信息
     */
    public void init() {
        
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "localhost");
        conf.set("hbase.zookeeper.property.clientPort", "2222");
    }
    
    /**
     * 創建表
     */
    public void createTable() {
        
        Connection conn = null;
        
        try {
            conn = ConnectionFactory.createConnection(conf);
            HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
            HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
            desc.addFamily(new HColumnDescriptor(FAMILY_COL_SCORE));
            desc.addFamily(new HColumnDescriptor(FAMILY_COL_INFO));
            if (admin.tableExists(TABLE_NAME)) {
                System.out.println("table " + TABLE_NAME + " is exists !");
                System.exit(0);
            } else{
                admin.createTable(desc);
                System.out.println("table " + TABLE_NAME + " created successfully.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ( conn != null) {
                try {
                    conn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 在指定表中插入數據
     */
    public void insertData() {

        Connection conn = null;
        HTable table = null;
        
        try {
            
            conn = ConnectionFactory.createConnection(conf);
            table = (HTable) conn.getTable(TableName.valueOf(TABLE_NAME));
            List<Put> puts = new ArrayList<Put>();
            // 添加數據|||搞笑考卷,一個Put代表一行|--诺基亚7610软件下载,構造函數傳入的是RowKey
            Put put1 = new Put(Bytes.toBytes("001:carl"));
            put1.addColumn(Bytes.toBytes(FAMILY_COL_SCORE), Bytes.toBytes("english"), Bytes.toBytes("97"));
            put1.addColumn(Bytes.toBytes(FAMILY_COL_SCORE), Bytes.toBytes("math"), Bytes.toBytes("99"));
            put1.addColumn(Bytes.toBytes(FAMILY_COL_INFO), Bytes.toBytes("weight"), Bytes.toBytes("130"));
            puts.add(put1);
            Put put2 = new Put(Bytes.toBytes("002:sophie"));
            put2.addColumn(Bytes.toBytes(FAMILY_COL_SCORE), Bytes.toBytes("english"), Bytes.toBytes("100"));
            put2.addColumn(Bytes.toBytes(FAMILY_COL_SCORE), Bytes.toBytes("math"), Bytes.toBytes("92"));
            put2.addColumn(Bytes.toBytes(FAMILY_COL_INFO), Bytes.toBytes("weight"), Bytes.toBytes("102"));
            puts.add(put2);
            // 將數據加入表
            table.put(puts);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (table != null) {
                try {
                    table.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 在指定表中查詢所有數據(全表掃描)
     */
    public void scanTable() {
        
        Connection conn = null;
        HTable table = null;
        ResultScanner scann = null;
        
        try {
            
            conn = ConnectionFactory.createConnection(conf);
            table = (HTable) conn.getTable(TableName.valueOf(TABLE_NAME));
            scann = table.getScanner(new Scan());
            for (Result rs : scann) {
                System.out.println("該行數據的RowKey為|_365彩票平台:"+new String(rs.getRow()));
                for (Cell cell : rs.rawCells()) {
                    System.out.println("列族:" + new String(CellUtil.cloneFamily(cell)) + "	" + 
                                       "列修飾符||库尔勒暴乱:" + new String(CellUtil.cloneQualifier(cell)) + "	" + 
                                       "值:" + new String(CellUtil.cloneValue(cell)) + "	" +
                                       "時間戳:" + cell.getTimestamp());
                }
                System.out.println("-----------------------------------------------");
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (table != null) {
                try {
                    table.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 通過rowkey在指定表中查詢一行數據
     */
    public void queryByRowKey() {
        
        Connection conn = null;
        HTable table = null;
        
        try {
            conn = ConnectionFactory.createConnection(conf);
            table = (HTable) conn.getTable(TableName.valueOf(TABLE_NAME));
            Get get = new Get("001:carl".getBytes());
//            get.setMaxVersions(2);
//            get.addColumn(FAMILY_COL_SCORE.getBytes(), "english".getBytes());
            Result rs = table.get(get);
            System.out.println("表" + TABLE_NAME + "中RowKey為001:carl的行數據如下");
            
            for (Cell cell : rs.rawCells()) {
                System.out.println("列族:" + new String(CellUtil.cloneFamily(cell)) + "	" + 
                                   "列修飾符___pc蛋蛋幸运28助赢软件:" + new String(CellUtil.cloneQualifier(cell)) + "	" + 
                                   "值:" + new String(CellUtil.cloneValue(cell)) + "	" +
                                   "時間戳:" + cell.getTimestamp());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (table != null) {
                try {
                    table.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 刪除表中指定RowKey的行
     */
    public void deleteRow() {

        Connection conn = null;
        HTable table = null;
        try {
            conn = ConnectionFactory.createConnection(conf);
            table = (HTable) conn.getTable(TableName.valueOf(TABLE_NAME));
            table.delete(new Delete("001-||苏州联建科技:carl".getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    
    /**
     * 刪除指定名稱的列族
     */
    public void deleteFamily() {

        Connection conn = null;
        HBaseAdmin admin = null;
        try {
            conn = ConnectionFactory.createConnection(conf);
            admin = (HBaseAdmin) conn.getAdmin();
            admin.deleteColumn(TABLE_NAME.getBytes(), FAMILY_COL_INFO);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != conn) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 刪除指定表
     */
    public void deleteTable() {

        Connection conn = null;
        HBaseAdmin admin = null;
        try {
            conn = ConnectionFactory.createConnection(conf);
            admin = (HBaseAdmin) conn.getAdmin();
            // 在刪除一張表前-_|公安县新闻网,要先使其失效
            admin.disableTable(TABLE_NAME);
            admin.deleteTable(TABLE_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    
}

3|__本溪张雷、Eclipse運行結果

 

4--_苗条梅、HBase的shell終端查看結果



延伸閱讀||-2m彩票永久来资料全年:

Tag標簽|_-兴安证券大智慧: 分布式   集群   代碼  
  • 專題推薦

About IT165 - 廣告服務 - 隱私聲明 - 版權申明 - 免責條款 - 網站地圖 - 網友投稿 - 聯係方式
本站內容來自於互聯網,僅供用於網絡技術學習,學習中請遵循相關法律法規
天空彩票秒速快3幸运飞艇平安彩票双赢彩票众购彩票

免责声明: 本站资料及图片来源互联网文章,本网不承担任何由内容信息所引起的争议和法律责任。所有作品版权归原创作者所有,与本站立场无关,如用户分享不慎侵犯了您的权益,请联系我们告知,我们将做删除处理!