阅读背景:

Hbase java 常见操作

来源:互联网 

 

 

 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.HashMap;
import java.util.List;
import java.util.Map; 

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;
import org.mortbay.log.Log; 




public class HbaseUtil implements IOperator
{
	private static Configuration conf = null; 
	private static String configFile = "hbase-site-test_bj.xml";
	private   Map<String, String> aMap = null;
	private   String mapTable = null; 
	private   String[] tableFamily = null;  
	
	public HbaseUtil() { 
		
	}
	 
	public HbaseUtil( String mapAppTable , String[] appTableFamily ) { 
		this.aMap =  new HashMap<String, String>();
		this.mapTable = mapAppTable; 
		this.tableFamily = appTableFamily;  
		
	}

	static
	{
		Configuration HBASE_CONFIG = new Configuration();
		HBASE_CONFIG.addResource(configFile);
		conf = HBaseConfiguration.create(HBASE_CONFIG);
		System.err.println(conf.get("hbase.zookeeper.property.dataDir"));
	}

	/**
	 * 创立表操作
	 * 
	 * @throws IOException
	 */
	public void createTable(String tablename, String[] cfs) throws IOException
	{
		HBaseAdmin admin  = new HBaseAdmin(conf);
			if (admin.tableExists(tablename))
			{
				System.out.println("表已存在!");
			}
			else
			{
				HTableDescriptor tableDesc = new HTableDescriptor(tablename);
				for (int i = 0; i < cfs.length; i++)
				{
					tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
				}
				admin.createTable(tableDesc);
				System.out.println("表创立胜利!");
			}
		  admin.close(); 
	}

	/**
	 * 删除表操作
	 * 
	 * @param tablename
	 * @throws IOException
	 */
	public void deleteTable(String tablename) throws IOException
	{
		HBaseAdmin admin = new HBaseAdmin(conf);
			if (!admin.tableExists(tablename))
			{
				System.out.println("table(" + tablename + ") not exists, won"t delete");
			}
			else
			{
				admin.disableTable(tablename);
				admin.deleteTable(tablename);
				System.out.println("table(" + tablename + ") delete success");
			} 
		 admin.close(); 
	}

	public void insertRow() throws IOException
	{
		HTable table = new HTable(conf, "test");
		Put put = new Put(Bytes.toBytes("row3"));
		put.add(Bytes.toBytes("cf"), Bytes.toBytes("444"), Bytes.toBytes("value444"));
		table.put(put);
		table.close();
	}

	/**
	 * 插入一行记载
	 * 
	 * @param tablename
	 * @param cfs
	 * @throws IOException 
	 */
	public void writeRow(String tablename, String[] cfs) throws IOException
	{
		HTable    table = new HTable(conf, tablename);
		Put put = new Put(Bytes.toBytes(cfs[0])); 
		put.add(Bytes.toBytes(cfs[1]), Bytes.toBytes(cfs[2]), Bytes.toBytes(cfs[3]));
		table.put(put);
		System.out.println("写入胜利!"); 
		table.close();
	}

	// 写多条记载
	public void writeMultRow(String tablename, String[][] cfs) throws IOException
	{
		List<Put> lists = new ArrayList<Put>();
		HTable table = new HTable(conf, tablename);
		for (int i = 0; i < cfs.length; i++)
		{
			Put put = new Put(Bytes.toBytes(cfs[i][0]));
			put.add(Bytes.toBytes(cfs[i][1]), Bytes.toBytes(cfs[i][2]), Bytes.toBytes(cfs[i][3]));
			lists.add(put);
		}
		table.put(lists);
		 table.close();
		 
	}

	// 写多条记载
	public void writeMultRowByDevice(HTable table, String tablename, String[][] cfs) throws IOException
	{
		 
		List<Put> lists = new ArrayList<Put>();
		// HTable table = new HTable(conf, tablename);
		for (int i = 0; i < cfs.length; i++)
		{
			Put put = new Put(Bytes.toBytes(cfs[i][0]));
			Log.info("writeMultRowByDevice  "+Bytes.toBytes(cfs[i][1])+"="+Bytes.toBytes(cfs[i][2])+"="+Bytes.toBytes(cfs[i][3]));
			put.add(Bytes.toBytes(cfs[i][1]), Bytes.toBytes(cfs[i][2]), Bytes.toBytes(cfs[i][3]));
			lists.add(put);
		}
		Log.info("push start");
		table.put(lists);
		Log.info("push end");
		 
	}

	/**
	 * 删除一行记载
	 * 
	 * @param tablename
	 * @param rowkey
	 * @throws IOException
	 */
	public void deleteRow(String tablename, String rowkey) throws IOException
	{
		HTable table = new HTable(conf, tablename);
		List<Delete> list = new ArrayList<Delete>();
		Delete d1 = new Delete(rowkey.getBytes());
		list.add(d1);
		table.delete(list);
		System.out.println("delete row(" + rowkey + ") sucess");
		table.close();
	}

	/**
	 * 查找一行记载
	 * 
	 * @param tablename
	 * @param rowkey
	 */
	public   void selectRow(String tablename, String rowKey) throws IOException
	{
		HTable table = new HTable(conf, tablename);
		Get g = new Get(rowKey.getBytes());
		// g.addColumn(Bytes.toBytes("cf:1"));
		Result rs = table.get(g);
		for (KeyValue kv : rs.raw())
		{
			System.out.print(new String(kv.getRow()) + "  ");
			System.out.print(new String(kv.getFamily()) + ":");
			System.out.print(new String(kv.getQualifier()) + "  ");
			System.out.print(kv.getTimestamp() + "  ");
			System.out.println(new String(kv.getValue()));
		}
	   table.close();
	   
	}

	/**
	 * 查询表中所有行
	 * 
	 * @param tablename
	 * @throws IOException 
	 */
	public void scaner(String tablename) throws IOException
	{
		 
			HTable table = new HTable(conf, tablename);
			Scan s = new Scan();
			ResultScanner rs = table.getScanner(s);
			for (Result r : rs)
			{
				KeyValue[] kv = r.raw();
				// for (int i = 0; i < kv.length; i++) {
				/*
				 * System.out.print(new String(kv[i].getRow()) + "  ");
				 * System.out.print(new String(kv[i].getFamily()) + ":");
				 * System.out.print(new String(kv[i].getQualifier()) + "  ");
				 * System.out.print(kv[i].getTimestamp() + "  ");
				 * System.out.println(new String(kv[i].getValue()));
				 */
				System.out.println(new String(kv[1].getValue()) + "==" + new String(kv[0].getValue()));
				// }
			}
		 rs.close();
		 table.close(); 
	}


	public void scanByTimestamp(String tablename, long maxtime) throws IOException
	{ 
			HTable table = new HTable(conf, tablename);
			Scan s = new Scan();
			// TODO 寄存所有的成果
			FilterList allInfo = new FilterList();
			// allInfo.addFilter();
			s.setFilter(allInfo);
			
	}

	public   Map<String, String> getMap()
	{
		Map<String, String> map = new HashMap<String, String>();
		try
		{
			HTable table = new HTable(conf, mapTable);
			Scan s = new Scan();
			ResultScanner rs = table.getScanner(s);
			for (Result r : rs)
			{
				KeyValue[] kv = r.raw();
				map.put(new String(kv[0].getRow()), new String(kv[0].getValue()));
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return map;
	}
 

	

}
import java.io.IOException; 
import java.ut




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: