java客户端连接操作Hbase示例代码

前提是hbase已经可以正常使用了。

maven依赖:

  	<dependency>
	  <groupId>org.apache.hbase</groupId>
	  <artifactId>hbase-client</artifactId>
	  <version>1.2.0</version>
	</dependency>
	<dependency>
	    <groupId>jdk.tools</groupId>
	    <artifactId>jdk.tools</artifactId>
	    <version>1.6</version>
	    <scope>system</scope>
	    <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
	</dependency>

main class

package hbase;

import java.io.File;
import java.io.IOException;
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.TableName;
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.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseClientTest extends ConnectionFactory {

	public static void main(String[] args) throws IOException {
		/*
		 * 这个并没有什么用处,只是可以避免日志里出现一个错误(winutil.exe找不到)。而那个错误并不影响正常的hbase操作。
		 */
		String hadoopHome=System.getenv("HADOOP_HOME");
		String devDefault=new File("client/").getAbsolutePath();
		System.setProperty("hadoop.home.dir", hadoopHome==null?devDefault:hadoopHome);
		long a=System.currentTimeMillis();
		/*
		 *hbase-site.xml放置到classpath里。
		 */
		Configuration config =  HBaseConfiguration.create();
		System.out.println("get config ed:"+(System.currentTimeMillis()-a)/1000.0);
		a=System.currentTimeMillis();
		
//		  config.set("hbase.zookeeper.quorum","host10.ticloud");  
//        config.set("hbase.zookeeper.property.clientPort", "2222"); 
//        config.addResource(new Path("D:\\hadoop\\hadoop_cfg\\", "hbase-site.xml"));
//        config.addResource(new Path("D:\\hadoop\\hbase_cfg\\", "core-site.xml"));
		Connection connection = ConnectionFactory.createConnection(config);
		System.out.println("get cnn ed:"+(System.currentTimeMillis()-a)/1000.0);
		a=System.currentTimeMillis();
		Table table=null;
		try {
			table = connection.getTable(TableName.valueOf("t2"));

			System.out.println("get table ed:"+(System.currentTimeMillis()-a)/1000.0);
			a=System.currentTimeMillis();
			// Use the table as needed, for a single operation and a single thread
			Put put=new Put(Bytes.toBytes("rowjava"));
			put.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("col1"),Bytes.toBytes("value3_6"));
//			put.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("col2"),Bytes.toBytes("value3_2"));
//			put.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("col3"),Bytes.toBytes("value3_3"));
//			put.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("col4"),Bytes.toBytes("value3_4"));
			System.out.println("get add ed:"+(System.currentTimeMillis()-a)/1000.0);
			a=System.currentTimeMillis();
			table.put(put);
			System.out.println("get put ed:"+(System.currentTimeMillis()-a)/1000.0);
			a=System.currentTimeMillis();
			long v=table.incrementColumnValue(Bytes.toBytes("rowjava"),Bytes.toBytes("c2"),Bytes.toBytes("cnt1"),1);//put incr不可混用
			System.out.println(v);
			System.out.println("get incr ed:"+(System.currentTimeMillis()-a)/1000.0);
			a=System.currentTimeMillis();
			
			Get get=new Get(Bytes.toBytes("rowjava"));
			Result r=table.get(get);
			Cell c=r.getColumnLatestCell(Bytes.toBytes("c2"),Bytes.toBytes("cnt1"));
			byte[] bs=CellUtil.cloneValue(c);
			System.out.println(Bytes.toLong(bs));
			
			c=r.getColumnLatestCell(Bytes.toBytes("c2"),Bytes.toBytes("col1"));
			bs=CellUtil.cloneValue(c);
			System.out.println(Bytes.toString(bs));
			
			Put p=new Put(Bytes.toBytes("rowjava"));
			p.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("1"));
			boolean suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),null, p);
			System.out.println("checkPut:"+suc);//true ,因为之前colCheck是不存在的

			suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),null, p);
			System.out.println("checkPut:"+suc);//false ,因为之前colCheck是存在的
			
			Put p2=new Put(Bytes.toBytes("rowjava"));
			p2.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("2"));
			suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("1"), p2);
			System.out.println("checkPut:"+suc);//true ,因为之前colCheck是1
			
			Delete del=new Delete(Bytes.toBytes("rowjava"));
			del.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"));//删除最新版本,删除后colCheck值2变成了1.
			table.delete(del);

			p2=new Put(Bytes.toBytes("rowjava"));
			p2.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("2"));
			suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("1"), p2);
			System.out.println("checkPut:"+suc);//true ,因为之前colCheck 2被删了


			p2=new Put(Bytes.toBytes("rowjava"));
			p2.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("3"));
			suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("1"), p2);
			System.out.println("checkPut:"+suc);//false ,因为之前colCheck又是2了
			
			del=new Delete(Bytes.toBytes("rowjava"));
			//不进行add,就是删除整行,全部单元格的全部版本。
			del.addColumns(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"));//删除colCheck单元格。全部的版本都删除
			table.delete(del);
			
			
			
		} finally {
			if(table!=null)table.close();//逻辑处理完毕即可关闭
			connection.close();//程序关闭时关闭,此对象可多线程共享使用
		}
	}

}

附件是没什么用的winutil.exe,可以避免一个日志提示:

Could not locate executable null\bin\winutils.exe in the Hadoop binaries. 

猜你喜欢

转载自bnmnba.iteye.com/blog/2322921