package com.hikvision.hbase.vertify.test; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.IOException;
import java.io.InterruptedIOException; /**
*/
public class HbasePutTest {
public static final Logger LOGGER= LoggerFactory.getLogger(HbasePutTest.class);
public final static String HBASE_MASTER = "hbase.master";
public final static String HBASE_ZOOKEEPER_PORT = "hbase.zookeeper.property.clientPort";
public final static String HBASE_ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum";
public static HTable htable;
public static void main(String[] args) {
String hbaseMaster="10.17.139.121:60010";
String zookeeperPort="";
String zookeeperQuorum="10.17.139.121";
String tableName="TestTest";
Configuration config= HBaseConfiguration.create();
config.set(HBASE_MASTER, hbaseMaster);
config.set(HBASE_ZOOKEEPER_PORT, zookeeperPort);
config.set(HBASE_ZOOKEEPER_QUORUM, zookeeperQuorum); try {
htable = new HTable(config, tableName);
} catch (IOException e) {
e.printStackTrace();
} if(null==htable){
LOGGER.error("htable is null");
System.exit(-);
} for(int i=;i<;i++){
String rowkey="";
if(i<) {
rowkey = ""+i;
}else if(i<){
rowkey = ""+i;
}
else if(i<){
rowkey = ""+i;
}
else if(i<){
rowkey = ""+i;
}
else if(i<){
rowkey = ""+i;
}
else if(i<){
rowkey = ""+i;
}
else if(i<){
rowkey = ""+i;
}
else if(i<){
rowkey = ""+i;
}
else if(i<){
rowkey = ""+i;
}
else if(i<){
rowkey = ""+i;
}else if(i<){
rowkey = ""+i;
}
long ts=System.currentTimeMillis();
String family="info";
String column=""+i;
int value=i;
putDataToTable(rowkey,ts,family,column,value);
if(==i%){
LOGGER.info("已经加载数据 {} 条",i+);
} }
LOGGER.info("已经加载数据 {} 条",);
}
/**
* @Description:往表中插入数据
* @param rowkey
* @param ts
* @param family
* @param column
* @param value void:
*/
public static void putDataToTable(String rowkey, long ts, String family, String column, int value) {
Put put = new Put(Bytes.toBytes(rowkey), ts);
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
try {
htable.put(put);
} catch (RetriesExhaustedWithDetailsException e) {
e.printStackTrace();
} catch (InterruptedIOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* @Description:往表中插入数据
* @param rowkey
* @param ts
* @param family
* @param column
* @param value void:
*/
public static void putDataToTable(String rowkey, long ts, String family, String column, String value) {
Put put = new Put(Bytes.toBytes(rowkey), ts);
put.add(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
try {
htable.put(put);
} catch (RetriesExhaustedWithDetailsException e) {
e.printStackTrace();
} catch (InterruptedIOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* @Description:从表中查询数据
* @param rowkey
* @param family
* @param column
* @return Result:
*/
public static Result getDataFromTable(String rowkey, String family, String column) {
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
Result dbresult = null;
try {
dbresult = htable.get(get);
} catch (IOException e) {
e.printStackTrace();
}
return dbresult;
} /**
* @Description:从表中查询数据
* @param rowkey
* @param family
* @param column
* @return Result:
*/
public static Result getDataFromTable(String rowkey, String family, String... column) {
Get get = new Get(Bytes.toBytes(rowkey));
for (String string : column) {
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(string));
}
Result dbresult = null;
try {
dbresult = htable.get(get);
} catch (IOException e) {
e.printStackTrace();
}
return dbresult;
}
}