member表格里面的数据已经导入
import java.io.IOException;
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.TableName;
import org.apache.hadoop.hbase.client.Append;
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.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class test01 {
static Configuration conf;
static Connection conn;
static HBaseAdmin admin;
public static void init() throws IOException {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","slave0,slave1,slave2");
conn = ConnectionFactory.createConnection(conf);
admin = (HBaseAdmin) conn.getAdmin();
}
public static void creatTable(String tableName, String[] familys) throws IOException {
if(admin.tableExists(tableName)) {
System.out.println( "表已存在");
}else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
for(String s : familys) {
tableDesc.addFamily(new HColumnDescriptor(s));
}
admin.createTable(tableDesc);
System.out.println("表创建成功");
}
}
public static void put(String tableName,String rowkey,String family,String column,String value) throws IOException {
HTable table = new HTable(conf,TableName.valueOf(tableName));
HBaseAdmin admin = new HBaseAdmin(conf);
if(!admin.tableExists(tableName)) {
System.out.println("表不存在");
}else {
table.setAutoFlush(true);
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
System.out.println("数据插入成功");
}
}
public static void get(String tableName,String rowkey,String family,String qualifier) throws IOException {
if(!admin.tableExists(tableName)) {
System.out.println("表不存在");
}else {
HTable table = new HTable(conf,TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier) );
Result result = table.get(get);
System.out.println(result.isEmpty());
KeyValue[] kvs = result.raw();
System.out.println("kvs" + kvs.length);
for (KeyValue kv:kvs) {
System.out.println(Bytes.toString(kv.getRow()));
System.out.println(Bytes.toString(kv.getFamily()));
System.out.println(Bytes.toString(kv.getQualifier()));
System.out.println(Bytes.toString(kv.getValue()));
}
}
}
public static void delete(String tableName,String rowkey,String family,String column, Long timeStampe) throws IOException {
if(!admin.tableExists(tableName)) {
throw new RuntimeException("表不存在");
}
HTable table = new HTable(conf,Bytes.toBytes(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
if(family != null) {
if (column != null) {
delete.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
}
}else {
delete.addFamily(Bytes.toBytes(family));
}
if (timeStampe != null) {
delete.setTimestamp(timeStampe);
}
table.delete(delete);
table.close();
}
public static void append(String tableName,String rowkey,String family,String column, String value) throws IOException {
if(!admin.tableExists(tableName)) {
throw new RuntimeException("表不存在");
}
HTable table = new HTable(conf,Bytes.toBytes(tableName));
Append append = new Append(Bytes.toBytes(rowkey));
append.add(Bytes.toBytes(family),Bytes.toBytes(column), Bytes.toBytes(value));
table.append(append);
table.close();
}
public static void increment(String tableName,String rowkey,String family,String column, long account) throws IOException {
if(!admin.tableExists(tableName)) {
throw new RuntimeException("表不存在");
}
HTable table = new HTable(conf,tableName);
Increment inc = new Increment(Bytes.toBytes(rowkey));
inc.addColumn(Bytes.toBytes(family),Bytes.toBytes(column), account);
table.increment(inc);
table.close();
}
public static void putLong(String tableName,String rowkey,String family,String column,long value) throws IOException {
HTable table = new HTable(conf,TableName.valueOf(tableName));
HBaseAdmin admin = new HBaseAdmin(conf);
if(!admin.tableExists(tableName)) {
System.out.println("表不存在");
}else {
table.setAutoFlush(true);
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
System.out.println("数据插入成功");
}
}
public static void getLong(String tableName,String rowkey,String family,String qualifier) throws IOException {
if(!admin.tableExists(tableName)) {
System.out.println("表不存在");
}else {
HTable table = new HTable(conf,TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier) );
Result result = table.get(get);
System.out.println(result.isEmpty());
KeyValue[] kvs = result.raw();
System.out.println("kvs" + kvs.length);
for (KeyValue kv:kvs) {
System.out.println(Bytes.toString(kv.getRow()));
System.out.println(Bytes.toString(kv.getFamily()));
System.out.println(Bytes.toString(kv.getQualifier()));
System.out.println(Bytes.toLong(kv.getValue()));
}
}
}
public static void main(String[] args) throws IOException {
init();
get("member","row1","project","hbase");
putLong("membet","row1","project","hbase",10L);
getLong("member","row1","project","hbase");
increment("member","row1","project","hbase",8L);
get("member","row1","project","hbase");
get("member","row1","info","id");
append("member","row1","info","id","0");
get("member","row1","info","id");
get("member","row1","project","java");
delete("member","row1","project","java",null);
get("member","row1","project","java");
}
}
这是后面的报错内容:
Exception in thread "main" java.lang.IllegalArgumentException: offset (0) + length (8) exceed the capacity of the array: 2
at org.apache.hadoop.hbase.util.Bytes.explainWrongLengthOrOffset(Bytes.java:632)
at org.apache.hadoop.hbase.util.Bytes.toLong(Bytes.java:606)
at org.apache.hadoop.hbase.util.Bytes.toLong(Bytes.java:579)
at persion.zengye.com.test01.getLong(test01.java:167)
at persion.zengye.com.test01.main(test01.java:224)
根据报错可知数组容量定义小了,
offset (0) + length (8) exceed the capacity of the array: