3.2.2.3 类: HBaseUtil
package com.atguigu.dataconsumer.util;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Map;
public class HBaseUtil {
private static Configuration conf;
private static Connection conn;
private static int regions = Integer.parseInt(PropertyUtil.getProperty("hbase.regions"));
private static DecimalFormat formatter = new DecimalFormat("0000");
static {
try {
conf = HBaseConfiguration.create();
conn = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createTable(String tableName, String cf) {
if (isTableExists(tableName)) return;
Admin admin = null;
try {
admin = getAdmin();
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
System.out.println(cf);
desc.addFamily(new HColumnDescriptor(cf));
admin.createTable(desc, getSplitKeys());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static byte[][] getSplitKeys() {
byte[][] splitKeys = new byte[regions][];
for (int i = 0; i < splitKeys.length; i++) {
String key = formatter.format(i) + "|";
splitKeys[i] = Bytes.toBytes(key);
}
return splitKeys;
}
public static void createNS(String NSName) {
if (isNSExists(NSName)) return;
NamespaceDescriptor build = NamespaceDescriptor.create(NSName)
.addConfiguration("creator", "atguigu")
.addConfiguration("create_time", System.currentTimeMillis() + "")
.build();
Admin admin = null;
try {
admin = getAdmin();
admin.createNamespace(build);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Admin getAdmin() throws IOException {
Admin admin = conn.getAdmin();
return admin;
}
public static boolean isNSExists(String nameSpace) {
Admin admin = null;
try {
admin = getAdmin();
NamespaceDescriptor descriptor = admin.getNamespaceDescriptor(nameSpace);
Map<String, String> conf = descriptor.getConfiguration();
return true;
} catch (IOException e) {
return false;
} finally {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static boolean isTableExists(String tableName) {
Admin admin = null;
try {
admin = getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public static Table getTable(String tableName) {
try {
Table table = conn.getTable(TableName.valueOf(tableName));
return table;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static byte[] getRowKey(String value) {
String[] split = value.split(",");
String rowKeyStr = new StringBuffer()
.append(getRowKeyPrefix(split[0], split[1].substring(0, 7)))
.append("_")
.append(split[0])
.append("_")
.append(split[1])
.append("_")
.append(split[2])
.append("_")
.append(split[3])
.append("_")
.append(split[4])
.toString();
return Bytes.toBytes(rowKeyStr);
}
private static String getRowKeyPrefix(String... arms) {
int i = Math.abs(Arrays.toString(arms).hashCode()) % regions;
return formatter.format(i);
}
}