IT技術(shu)互動交(jiao)流(liu)平台

速盈彩票官网

來源(yuan)︰IT165收集  發布(bu)日期(qi)︰2020-02-20 02:45:03

一、HBase偽分(fen)布(bu)式集群安裝(zhuang)

1、安裝(zhuang)包解壓

$ cd app/

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

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

 

2、添(tian)加環(huan)境(jing)變量

$ 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、Web UI

http://localhost:60010

 

8、解決HBase中(zhong)SLF4J的jar包與Hadoop沖突

問題(ti)描(miao)述︰

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]

解決方(fang)法(fa)︰

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

$ rm slf4j-log4j12-1.7.5.jar

二(er)、代碼測試

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、測試yuan)/p>

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(); } /** * 初始(shi)化hbase的配置信(xin)息,也可以(yi)將(jiang)hbase的配置文件hbase-site.xml引入項目,則不需要在代碼中(zhong)填寫配置信(xin)息 */ 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();  }  } } } /** * 在指(zhi)定表中(zhong)插入數據 */ 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>();  // 添(tian)加數據,一個Put代表一行,構(gou)造函數傳入的是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);  // 將(jiang)數據加入表  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();  }  } } } /** * 在指(zhi)定表中(zhong)查(cha)詢所有數據(全表掃(sao)描(miao)) */ 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為︰"+new String(rs.getRow()));  for (Cell cell : rs.rawCells()) {   System.out.println("列(lie)族(zu):" + new String(CellUtil.cloneFamily(cell)) + "" +     "列(lie)修飾符︰" + 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在指(zhi)定表中(zhong)查(cha)詢一行數據 */ 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 + "中(zhong)RowKey為001:carl的行數據如(ru)下");   for (Cell cell : rs.rawCells()) {  System.out.println("列(lie)族(zu):" + new String(CellUtil.cloneFamily(cell)) + "" +     "列(lie)修飾符︰" + 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();  }  } } } /** * 刪jing)碇zhong)指(zhi)定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();  } } } /** * 刪jing)zhi)定名(ming)稱的列(lie)族(zu) */ 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();  } } } /** * 刪jing)zhi)定表 */ public void deleteTable() { Connection conn = null; HBaseAdmin admin = null; try {  conn = ConnectionFactory.createConnection(conf);  admin = (HBaseAdmin) conn.getAdmin();  // 在刪jing)徽瘧砬埃  仁蠱涫  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運行結果(guo)

 

4、HBase的shell終端查(cha)看結果(guo)



Tag標(biao)簽︰分(fen)布(bu)式  集群  代碼  
  • 速盈彩票官网

About IT165 - 廣告服務 - 隱私聲明 - 版(ban)權申明 - 免責條款 - 網(wang)站(zhan)地圖 - 網(wang)友投稿(gao) - 聯系方(fang)式
本站(zhan)內容來自于(yu)互聯網(wang),僅供用于(yu)網(wang)絡技術(shu)學習,學習中(zhong)請遵循相關法(fa)律法(fa)規
速盈彩票官网 | 下一页