The premise is that hbase can be used normally.
maven dependencies:
<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 { /* * This does nothing but avoids an error in the log (winutil.exe not found). And that error does not affect normal hbase operation. */ 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 is placed in the 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, because colCheck did not exist before suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),null, p); System.out.println("checkPut:"+suc);//false , because colCheck existed before 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, because colCheck was 1 before Delete del=new Delete(Bytes.toBytes("rowjava")); del.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"));//Delete the latest version, the colCheck value 2 becomes 1 after deletion. 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, because colCheck 2 was deleted before 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, because colCheck was 2 before del=new Delete(Bytes.toBytes("rowjava")); //Do not add, that is, delete the entire row, all versions of all cells. del.addColumns(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"));//Delete the colCheck cell. All versions are deleted table.delete(del); } finally { if(table!=null)table.close();//Close after logic processing connection.close();//Close when the program is closed, this object can be shared by multiple threads } } }
Attached is the useless winutil.exe to avoid a log prompt:
Could not locate executable null\bin\winutils.exe in the Hadoop binaries.