• 本地eclipse java api连接远程虚拟机HBase


    1.本地与远程连通

    无论是域名或者ip都可以,另外需保证HBase在虚拟机集群上正常运行。

    2.本地要有一个跟远程相同的hadoop环境

    当然不相同,只要兼容也可以,现采用hadoop-2.5.0-cdh5.3.6,此版本如果在Windows系统中。在java api调用时候会报错Could not locate executable D:Hu_softwarehadoop-2.5.0-cdh5.3.6inwinutils.exe in the Hadoop bina
    此时可以从别的拷贝一个winutils.exe到此bin来。可从winutls.exe下载,提取吗wt9x。经测试用hadoop-2.7.7也可以测试跑通,下载解压即可。

    3.代码运行前准备

    首先需要HADOOP_HOME的环境变量,可以配置到系统环境变量里面,也可以如下:

    Eclipse->Debug/Run Configurations -> Environment (tab) -> and added
    
    variable: HADOOP_HOME
    
    Value: C:hadoop-2.5.0-cdh5.3.6
    

    最好配以log4j.properties到src目录下面,地址。

    4.启动执行

    (1) pom.xml

     <dependency>
    	<groupId>org.apache.hbase</groupId>
    	<artifactId>hbase-server</artifactId>
    	<version>0.98.6-hadoop2</version>
     </dependency>
    
     <dependency>
    	<groupId>org.apache.hbase</groupId>
    	<artifactId>hbase-client</artifactId>
    	<version>0.98.6-hadoop2</version>
     </dependency>
    

    (2)java 代码 HBaseDemo.java

    package com.hoult.hbase;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    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.MasterNotRunningException;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.ZooKeeperConnectionException;
    import org.apache.hadoop.hbase.client.Delete;
    import org.apache.hadoop.hbase.client.HBaseAdmin;
    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.client.ResultScanner;
    import org.apache.hadoop.hbase.client.Scan;
    import org.apache.hadoop.hbase.util.Bytes;
    
    public class HBaseDemo {
    	//创建Hadoop以及HBased管理配置对象
    	public static Configuration conf;
    	
    	static{
    		//使用HBaseConfiguration的单例方法实例化
    		conf = HBaseConfiguration.create();
    		conf.set("hbase.rootdir","hdfs://hadoop-senior01.hoult.com:9000/hbase");//可以吧hadoop改成你的域名或Ip
    		conf.set("hbase.zookeeper.quorum","hadoop-senior01.hoult.com,hadoop-senior02.hoult.com,hadoop-senior03.hoult.com");  //hbase 服务地址
    		conf.set("hbase.zookeeper.property.clientPort","2181"); //端口号默认2128不用改
    	}
    	
    	/**
    	 * 判断表是否已存在
    	 * @param args
    	 * @throws IOException 
    	 * @throws ZooKeeperConnectionException 
    	 * @throws MasterNotRunningException 
    	 */
    	public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
    		//在HBase中管理、访问表需要先创建HBaseAdmin对象
    		HBaseAdmin admin = new HBaseAdmin(conf);
    		return admin.tableExists(tableName);
    	}
    	
    	/**
    	 * 创建表
    	 * @param args
    	 * @throws IOException 
    	 * @throws ZooKeeperConnectionException 
    	 * @throws MasterNotRunningException 
    	 */
    	public static void createTable(String tableName, String... columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
    		HBaseAdmin admin = new HBaseAdmin(conf);
    		//判断表是否存在
    		if(isTableExist(tableName)){
    			System.out.println("表" + tableName + "已存在");
    			//System.exit(0);
    		}else{
    			//创建表属性对象,表名需要转字节
    			HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
    			
    			//创建多个列族
    			for(String cf : columnFamily){
    				descriptor.addFamily(new HColumnDescriptor(cf));
    			}
    			
    			//根据对表的配置,创建表
    			admin.createTable(descriptor);
    			System.out.println("表" + tableName + "创建成功!");
    		}
    	}
    	
    	/**
    	 * 删除表
    	 * @param args
    	 * @throws IOException 
    	 * @throws ZooKeeperConnectionException 
    	 * @throws MasterNotRunningException 
    	 */
    	public static void dropTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
    		HBaseAdmin admin = new HBaseAdmin(conf);
    		if(isTableExist(tableName)){
    			admin.disableTable(tableName);
    			admin.deleteTable(tableName);
    			System.out.println("表" + tableName + "删除成功!");
    		}else{
    			System.out.println("表" + tableName + "不存在!");
    		}
    		
    	}
    	
    	/**
    	 * 向表中插入数据
    	 * @param tableName
    	 * @param rowKey
    	 * @param columnFamily
    	 * @param column
    	 * @param value
    	 * @throws IOException 
    	 */
    	public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException{
    		//创建HTable对象
    		HTable hTable = new HTable(conf, tableName);
    		//向表中插入数据
    		Put put = new Put(Bytes.toBytes(rowKey));
    		//向Put对象中组装数据
    		put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
    		hTable.put(put);
    		hTable.close();
    		System.out.println("插入数据成功");
    	}
    	
    	/**
    	 * 删除多行数据
    	 * @param tableName
    	 * @param rows
    	 * @throws IOException 
    	 */
    	public static void deleteMultiRow(String tableName, String... rows) throws IOException{
    		HTable hTable = new HTable(conf, tableName);
    		List<Delete> deleteList = new ArrayList<Delete>();
    		for(String row : rows){
    			Delete delete = new Delete(Bytes.toBytes(row));
    			deleteList.add(delete);
    		}
    		hTable.delete(deleteList);
    		hTable.close();
    	}
    	
    	/**
    	 * 得到所有的数据
    	 * @param tableName
    	 * @throws IOException 
    	 */
    	public static void getAllRows(String tableName) throws IOException{
    		HTable hTable = new HTable(conf, tableName);
    		//得到用于扫描region的对象
    		Scan scan = new Scan();
    		//使用HTable得到resultcanner实现类的对象
    		ResultScanner resultScanner = hTable.getScanner(scan);
    		for(Result result : resultScanner){
    			Cell[] cells = result.rawCells();
    			for(Cell cell : cells){
    				//得到rowkey
    				System.out.print(Bytes.toString(CellUtil.cloneRow(cell)) + "	");
    				//得到列族
    				System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+ "	");
    				System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell))+ "	");
    				System.out.println(Bytes.toString(CellUtil.cloneValue(cell))+ "	");
    			}
    		}
    		
    	}
    	
    	public static void main(String[] args) {
    			try {
    //				System.out.println(isTableExist("student"));
    //				createTable("person", "basic_info", "job", "heathy");
    //				dropTable("person");
    				addRowData("person", "1002", "basic_info", "name", "Hoult");
    				addRowData("person", "1003", "basic_info", "sex", "Male");
    				addRowData("person", "1002", "basic_info", "age", "22");
    				addRowData("person", "1004", "job", "dept_no", "123");
    //				
    //				deleteMultiRow("person", "person");
    //				getAllRows("person");
    				
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    	}
    
    }
    
    
  • 相关阅读:
    JAVA 注解的几大作用及使用方法详解
    内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
    关于Hash集合以及Java中的内存泄漏
    ifconfig命令详情
    route命令详情
    ping命令详解
    scp命令详解
    ssh命令详解
    telnet命令详解
    sudo命令详解
  • 原文地址:https://www.cnblogs.com/hulichao/p/bigdata_hbase.html
Copyright © 2020-2023  润新知