Skip to content

Instantly share code, notes, and snippets.

@mattshma
Last active December 22, 2016 10:27
Show Gist options
  • Select an option

  • Save mattshma/4a6cc0322452f6e3725eec5487153c67 to your computer and use it in GitHub Desktop.

Select an option

Save mattshma/4a6cc0322452f6e3725eec5487153c67 to your computer and use it in GitHub Desktop.
hbase 1.2 api example
// exmaple: https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/package-summary.html#client_example
package com.yz;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HbaseTest implements Runnable {
private Table table;
public HbaseTest(Table table) {
this.table = table;
}
public void run() {
String rowkey = "";
List<Put> puts = new ArrayList<Put>();
for(int i=23; i<100; i++) {
long now = System.currentTimeMillis();
for(int j=0; j<10000; j++) {
rowkey = i + "-" + j;
Put p = new Put(Bytes.toBytes(rowkey));
p.add(Bytes.toBytes("info"), Bytes.toBytes("q"), Bytes.toBytes("v"+rowkey));
puts.add(p);
}
try {
table.put(puts);
} catch (IOException e) {
e.printStackTrace();
}
long re = (System.currentTimeMillis() - now)/1000;
System.out.println("Cost time:" + re);
try{
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "10.6.24.107,10.6.24.111,10.6.24.113");
conf.set("hbase.zookeeper.property.clientPort", "2181");
Connection conn = ConnectionFactory.createConnection(conf);
try {
Table table = conn.getTable(TableName.valueOf("test"));
Thread thread = new Thread(new HbaseTest(table));
thread.start();
thread.join();
// Get g = new Get(Bytes.toBytes("2015-12-21_1001130236_2033310283_17679_16996049666709551606"));
// Result r = table.get(g);
// byte[] value = r.getValue(Bytes.toBytes("info"), Bytes.toBytes(""));
// String valueStr = Bytes.toString(value);
// System.out.println("Get value:" + valueStr);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
conn.close();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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>my</groupId>
<artifactId>hbasep</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0-cdh5.7.0</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment