获取Hbase HBaseConfiguration对象方法
// 获取配置
public static HBaseConfiguration getConfiguration() {
Configuration HBASE_CONFIG = new Configuration();
if (conf == null) {
HBASE_CONFIG.setInt("hbase.rpc.timeout", 20000);
HBASE_CONFIG.setInt("hbase.client.operation.timeout", 30000);
HBASE_CONFIG.setInt("hbase.client.scanner.timeout.period", 20000);
HBASE_CONFIG.set("hbase.zookeeper.quorum","10.2.4.39,10.2.4.40,10.2.4.41");
HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
conf = new HBaseConfiguration(HBASE_CONFIG); // 读取配置文件
}
return conf;
}
构建Hbase Connection
Connection connection = ConnectionFactory.createConnection(getConfiguration());
批量入库方法
public static void batchInsertTest_two(String value) throws IOException {
Connection connection = ConnectionFactory.createConnection(getConfiguration());
try {
// 连接表 获取表对象
BufferedMutator table = connection.getBufferedMutator(TableName.valueOf("t_event"));
try {
int count =1;
List<Mutation> mutations = new ArrayList<Mutation>();
for (int i = 0; i < 1; ++i) {
Random ran=new Random();
int j=ran.nextInt(100000);
String string = "000";
String temp = String.valueOf(j);
Put p = new Put(temp.getBytes());
p.add("bas".getBytes(), "id".getBytes(), value.getBytes());
mutations.add(p);
if (mutations.size() % 10000 == 0) {
table.mutate(mutations);
mutations.clear();
}
}
// 如果不flush 在后面get可能是看不见的
table.flush();
} finally {
if (table != null)
table.close();
}
} finally {
connection.close();
}
}