HBASE的安装配置

HBASE的安装配置

安装

1、下载压缩包
地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/stable/
选择稳定版hbase-1.4.9-bin.tar.gz

2、将压缩包通过xftp传到xshell中解压到/opt/module目录下

tar -zxvf ~/hbase-1.4.9-bin.tar.gz -C /opt/modle

3、配置环境变量
在/etc/profile文件里添加HBase安装路径的配置信息,之后用source命令使配置生效。

export HBASE_HOME=/opt/module/hbase-1.4.9
export PATH=$HBASE_HOME/bin:$PATH

4、执行命令hbase测试是否安装成功

配置(伪分布式模式)

配置文件位于HBase安装路径的conf目录(/opt/module/hbase/conf)下面
1、配置hbase-env.sh
①、设置Java安装路径
②、设置HBase的配置文件路径
③、采用HBase自带Zookeeper,设置参数true

2、配置hbase-site.xml




        hbase.rootdir
        hdfs://bigdata128:9000/hbase



        hbase.cluster.distributed
        true




        hbase.zookeeper.quorum
        localhost

3、启动并运行HBase(之前启动Hadoop)
启动HBase,并jps查看
命令:start-hbase.sh
HBASE的安装配置_第1张图片

进入HBASE数据库

进入HBase的shell命令行模式
完成数据库基本操作(建表,增删改数据,删除表格…)

编程调试数据表操作的Java程序

在eclipse运行Java程序打包至xshell运行
程序代码如下:

package hbase.tables;

import java.io.IOException;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseTables {

	public static Configuration conf;
	public static Connection con;
	public static Admin adm;
	
	@SuppressWarnings("all")
	public static void init() throws IOException {
		  conf=HBaseConfiguration.create();
		  conf.set("hbase.rootdir", "hdfs://hadoop:9000/hbase");
		  con= ConnectionFactory.createConnection(conf);
		  adm = con.getAdmin();
		  System.out.println(adm);
	} 
	
	public static void createTable(String myTableName, String[] colFamily) throws IOException {
		 init();       
		 TableName tableName = TableName .valueOf(myTableName);
		 if (adm.tableExists(tableName)) {
			 System.out.println("table is exists!"); }else {
				 HTableDescriptor htd=new HTableDescriptor(tableName);
				 for(String str:colFamily) {     
					 HColumnDescriptor hcd =new HColumnDescriptor(str);
					 htd.addFamily(hcd); 
				 }        
				 adm.createTable(htd);
			 }
			   	close();
	}

	public static void close() {
		try {
			if (adm != null) { adm.close();
			    	}
			    	if (con != null) { con.close();
			    	}
			  	}catch (IOException e) {
			  		e.printStackTrace();}
	}

	public static void deleteTable(String myTableName) throws IOException {
		init();
		TableName tableName = TableName .valueOf(myTableName);
		if (adm.tableExists(tableName)) {
			adm.disableTable(tableName);
			adm.deleteTable(tableName);
			 }
		close();       
	}

	public static void listTables() throws IOException {
		init();     
		HTableDescriptor htds[] =adm.listTables();
		for(HTableDescriptor  htd : htds) {
			System.out.println(htd.getNameAsString());
		}
		close();
	}

	public static void insertRow(String myTableName, String rowKey, String colFamily, String col, String val) throws IOException {
		init();   
		TableName tableName =  TableName .valueOf(myTableName);
		@SuppressWarnings("deprecation")
		HTable table = new HTable(conf,tableName);
		Put put=new Put(rowKey.getBytes());
		put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
		table.put(put);   
		table.close();
		close();
	}
	
	private static void deleteRow(String myTableName, String rowKey, String colFamily, String col) throws IOException {
		init();
		TableName tableName =TableName .valueOf(myTableName);
		@SuppressWarnings("deprecation")
		HTable table = new HTable(conf, tableName);
		Delete delete=new Delete(rowKey.getBytes());
		delete.addFamily(Bytes.toBytes(colFamily));
		delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));  
		table.delete(delete);
		table.close();
		close();
	}

	public static void getData(String myTableName, String rowKey, String colFamily, String col) throws IOException {   
		init();
		TableName tableName = TableName .valueOf(myTableName);
		@SuppressWarnings("deprecation")
		HTable table = new HTable(conf, tableName);
		Get get= new Get(rowKey.getBytes());
		Result result = table.get(get);
		showCell(result);
		table.close();
		close();
	}

	private static void showCell(Result result) {
		
		Cell[] cells = result.rawCells();
		for (Cell cell : cells) {
			System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
			System.out.println("Timetamp:" + cell.getTimestamp() + " ");
			System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
			System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
			System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
	        }

	}

	public static void main(String[] args) throws IOException {

		System.out.println("*****Please enter the number:1.createtable/2.insertRow/3.getData/4.deleteRow/5.listTables/6.deleteTable*****");
		for(int j=0;j<7;j++) {
		int i = 0;
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		i = scan.nextInt();
		switch (i) {
		case 1: 
			System.out.println("please enter tablename:");
			String tbn = scan.next();
			String[] cf = {"cf1,cf2"}; 
			HbaseTables.createTable(tbn, cf);
			System.out.println("createTable success!!!");
			break;
		case 2:
			System.out.println("please enter tablename:");
			String tbn1 = scan.next();
			System.out.println("please enter rowkey:");
			String rk1 = scan.next();
			System.out.println("please enter column:");
			String clm1 = scan.next();
			System.out.println("please enter colname:");
			String cn1 = scan.next();
			System.out.println("please enter colvalue:");
			String cv1 = scan.next();
			HbaseTables.insertRow(tbn1, rk1, clm1, cn1, cv1);
			System.out.println("insertRow success!!!");
			break;
		case 3: 
			System.out.println("please enter tablename:");
			String tbn2 = scan.next();
			System.out.println("please enter rowkey:");
			String rk2 = scan.next();
			System.out.println("please enter colname:");
			String cn2 = scan.next();
			System.out.println("please enter colvalue:");
			String cv2 = scan.next();
			HbaseTables.getData(tbn2, rk2, cn2, cv2);
			System.out.println("getData success!!!");
			break;
		case 4:
			System.out.println("please enter tablename:");
			String tbn3 = scan.next();
			System.out.println("please enter rowkey:");
			String rk3 = scan.next();
			System.out.println("please enter column:");
			String clm3 = scan.next();
			System.out.println("please enter colname:");
			String cn3 = scan.next();
			HbaseTables.deleteRow(tbn3, rk3, clm3, cn3);
			System.out.println("deleteRow success!!!");
			break;
		case 5: 
			HbaseTables.listTables();
			System.out.println("listTables success!!!");
			break;
		case 6: 
			System.out.println("please enter tablename:");
			String tbn4 = scan.next();
			HbaseTables.deleteTable(tbn4);
			System.out.println("deleteTable success!!!");
			break;

		default:
			System.out.println("input error!!!");
			break;
		}
		}
	}

}

pom.xml代码:


  4.0.0
  com.bla
  HBASE
  0.0.1-SNAPSHOT
  
   
  	
	
    	org.apache.hadoop
    	hadoop-client
    	2.7.7
	
  	
 	
      org.apache.hbase
      hbase-it
      1.4.9
    
  
  

        
            
            	org.apache.maven.plugins
                maven-compiler-plugin
                3.1
                
                    1.8
                    1.8
                
            
            
                    maven-assembly-plugin 
                   
                        
                        jar-with-dependencies
                        
                        
                             
                                  hbase.tables.HbaseTables
                             
                        
                   
                    
                        
                             make-assembly
                             package
                             
                                  assembly
                             
                        
                   
              
        
    

运行程序后根据提示进行相应操作即可完成数据库操作
网页显示:IP地址:60010

你可能感兴趣的:(HBASE的安装配置)