HBase javaAPI 操作 ---- 工具类(2.2.0)

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.yc</groupId>
  <artifactId>HBaseDome</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>HBaseDome</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
 	<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>2.2.0</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>2.2.0</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-common -->
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-common</artifactId>
    <version>2.2.0</version>
</dependency>
    <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
        </dependency>
  </dependencies>
</project>

HBaseUtil

package com.yc.HBaseDome;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
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.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.client.coprocessor.Batch.Call;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hbase.thirdparty.io.netty.util.ResourceLeakTracker;

public class HBaseUtil {
	private static Connection conn=null;
	/**
	 * 建立连接
	 * */
	static{
		Configuration conf=HBaseConfiguration.create();
		conf.set("hbase.zookeeper.property.clientPort", "2181");
		conf.set("hbase.zookeeper.quorum", "master,slave2,slave1");
		conf.set("hbase.master", "master:60000");
		try {
			conn=ConnectionFactory.createConnection(conf);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 创建表
	 * @throws IOException 
	 * */
	public void  createTable(String name,String ...ColumnFamilys) throws IOException{
		Admin admin=conn.getAdmin();
		TableName tname=TableName.valueOf(name);
		if(admin.tableExists(tname)){
			
			System.out.println(name+"表已经存在");
		}else{
			TableDescriptorBuilder tdesc=TableDescriptorBuilder.newBuilder(tname);
			
			for(String s: ColumnFamilys){
				ColumnFamilyDescriptor cfd=ColumnFamilyDescriptorBuilder.of(s);
				tdesc.setColumnFamily(cfd);
			}
			TableDescriptor desc=tdesc.build();
			admin.createTable(desc);
		}
		
	}
	
	/**
	 * 删除表
	 * @throws IOException 
	 * */
	public void deleteTable(String name) throws IOException{
		Admin admin=conn.getAdmin();
		TableName tableName=TableName.valueOf(name);
		admin.disableTable(tableName);
		admin.deleteTable(tableName);
	}
	
	/**
	 * 批量创建表
	 * @throws IOException 
	 * */
	public void createTables(String[] names,List<List<String>> ColumnFamilys) throws IOException{
		Admin admin=conn.getAdmin();
		if(names.length==ColumnFamilys.size()){
			for(int i =0;i<names.length; i++){
				TableName tname=TableName.valueOf(names[i]);
				if(admin.tableExists(tname)){
					
					System.out.println(names[i]+"表已经存在");
				}else{
					TableDescriptorBuilder tdesc=TableDescriptorBuilder.newBuilder(tname);
					
					for(String s: ColumnFamilys.get(i)){
						ColumnFamilyDescriptor cfd=ColumnFamilyDescriptorBuilder.of(s);
						tdesc.setColumnFamily(cfd);
					}
					TableDescriptor desc=tdesc.build();
					admin.createTable(desc);
				}
			}
		}else{
			System.out.println("没张表必须要有列族");
		}
	}
	/**
	 * 批量删除表
	 * @throws IOException 
	 * */
	public void deleteTables(String ...name) throws IOException{
		Admin admin=conn.getAdmin();
		for(String s:name){
			TableName tableName=TableName.valueOf(s);
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
		}
		
	}
	/**
	 * 添加单row数据
	 * @throws IOException 
	 * @throws TableNotFoundException 
	 * */
	public void addrow(String tname,String family,Map<String,Object> params) throws TableNotFoundException, IOException{
		TableName tableName=TableName.valueOf(tname);
		Table table=conn.getTable(tableName);
		Put put=new Put(params.get("row").toString().getBytes());
		for(Map.Entry<String, Object> m:params.entrySet()){
			if(m.getKey().equals("row")){
				continue;
			}
			put.addColumn(family.getBytes(), m.getKey().getBytes(), m.getValue().toString().getBytes());
		}
		table.put(put);
	}
	/**
	 * 批量添加数据
	 * @throws IOException 
	 * params 数据结构为:             {columnName:{family,Value},columnName:{family,Value}....}
	 * */
	public void addrows(String tname,Map<String,Map<String,Object>> params) throws IOException{
		TableName tableName=TableName.valueOf(tname);
		Table table=conn.getTable(tableName);
		List<Put> listput=new ArrayList<Put>();
		for(Map.Entry<String, Map<String,Object>> map:params.entrySet()){
			Put put=new Put(map.getKey().getBytes());
			String family=map.getValue().get("family").toString();
			for(Map.Entry<String, Object> m:map.getValue().entrySet()){
				if(m.getKey().equals("row")){
					continue;
				}
				put.addColumn(family.getBytes(), m.getKey().getBytes(), m.getValue().toString().getBytes());
			}
			listput.add(put);
		}
		table.put(listput);
	}
	/**
	 * 删除单row数据
	 * @throws IOException 
	 * */
	public void deleteRow(String tname,String row,Map<String,Object> params) throws IOException{
		TableName tableName=TableName.valueOf(tname);
		Table table=conn.getTable(tableName);
		Delete delete=new Delete(row.getBytes());
		if(params!=null){
			for(Map.Entry<String, Object> m:params.entrySet()){
				
				delete.addColumn(m.getKey().getBytes(), m.getValue().toString().getBytes());
			}
		}
		
		table.delete(delete);
	}
	/**
	 * 批量删除数据
	 * */
	public void deleteRows(String tname,Map<String,Object> params,String ...rows) throws IOException{
		TableName tableName=TableName.valueOf(tname);
		Table table=conn.getTable(tableName);
		List<Delete> deletes=new ArrayList<Delete>();
		for(String row:rows){
			Delete delete=new Delete(row.getBytes());
			if(params!=null){
				for(Map.Entry<String, Object> m:params.entrySet()){
					delete.addColumn(m.getKey().getBytes(), m.getValue().toString().getBytes());
				}
			}
			
			deletes.add(delete);
		}
		
		table.delete(deletes);
	}
	/**
	 * 得到所有数据
	 * @throws IOException 
	 * */
	public String getAllDate(String tname) throws IOException{
		
		TableName tableName=TableName.valueOf(tname);
		Table table=conn.getTable(tableName);
	    Set<byte []> familyNames=table.getDescriptor().getColumnFamilyNames();
	    String result="";
		for(byte[] familuName:familyNames){
			ResultScanner rs= table.getScanner(familuName);
			Iterator<Result> iterator=rs.iterator();
			while(iterator.hasNext()){
				Result r=iterator.next();
				for (Cell cell:r.rawCells()){
					String family=Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
					String qualifier=Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
					String row=Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
					String value=Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
					result+=row+","+family+","+qualifier+","+value+"\n";
				}
			}
		}
		return result;
	}
	
	/**
	 * 得到指定列数据
	 * @throws IOException 
	 * */
	public List<String> get(String tname,String family,String qualifier) throws IOException{
		TableName tableName=TableName.valueOf(tname);
		Table table=conn.getTable(tableName);
		ResultScanner rs= table.getScanner(family.getBytes(), qualifier.getBytes());
		Iterator<Result> iterator=rs.iterator();
		List<String> list=new ArrayList<String>();
		while(iterator.hasNext()){
			Result r=iterator.next();
			for (Cell cell:r.rawCells()){
				
				String value=Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
				list.add(value);
			}
		}
		return list;
	}
	
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值