hbase HbaseUtil 도구 류
package com.utstar.iptvboss.util.hbase;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
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.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;
public class HBaseUtils {
private static Configuration conf;
private static Connection conn;
private static HBaseUtils hBaseUtils;
private static Properties prop;
private static Logger logger = Logger.getLogger(HBaseUtils.class);
public void init() {
conf = HBaseConfiguration.create();
prop = new Properties();
try {
prop.load(HBaseUtils.class.getResourceAsStream("/hbase.properties"));
String clientPort = prop.getProperty("clientPort");
String quorum = prop.getProperty("quorum");
conf.set("hbase.zookeeper.quorum", quorum);
conf.set("hbase.zookeeper.property.clientPort", clientPort);
conn = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
private HBaseUtils() {
}
public static HBaseUtils getInstance() {
if(hBaseUtils == null) {
synchronized (HBaseUtils.class) {
if (hBaseUtils == null) {
hBaseUtils = new HBaseUtils();
hBaseUtils.init();
}
}
}
return hBaseUtils;
}
/**
* by 1022
* @return
*/
public Configuration getConfiguration(){
return this.conf;
}
/**
* by 1022
* @param tableName
* @param row
* @param columnFamily
* @param column
* @param data
* @throws IOException
*/
public static void putData(String tableName, String row, String columnFamily, String column, String data)
throws IOException {
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf(tableName));
try {
Put put = new Put(Bytes.toBytes(row));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
table.put(put);
} finally {
table.close();
conn.close();
}
}
public void testGet() throws IOException {
long begin = System.currentTimeMillis();
HTable table = (HTable) conn.getTable(TableName.valueOf("CFSchema"));
TableName name = table.getName();
System.out.println("name : " + name);
/* Get get = new Get(Bytes.toBytes("91244740570"));
Result result = table.get(get);
String str = Bytes.toString(result.getValue(Bytes.toBytes("mediaCodes"),
Bytes.toBytes("Userid")));
System.out.println(str);
long end = System.currentTimeMillis();
System.out.println("used time : " + (end - begin));*/
table.close();
}
public void createTable() throws IOException {
HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("people"));
HColumnDescriptor htd_info = new HColumnDescriptor("info");
htd.addFamily(htd_info);
htd.addFamily(new HColumnDescriptor("data"));
htd_info.setMaxVersions(3);
admin.createTable(htd);
admin.close();
}
public String getListByUserId(String userid) {
HTable table;
String rs = "";
try {
table = (HTable) conn.getTable(TableName.valueOf("CFSchema"));
logger.debug(" tableName : " + table + " ");
//Get get = new Get(Bytes.toBytes(userid));
//Result result = table.get(get);
Scan scan = new Scan(Bytes.toBytes(userid),Bytes.toBytes(userid));
scan.addColumn("cf".getBytes(), "mediaCodes".getBytes());
ResultScanner scanner = table.getScanner(scan);
Result result = null;
for (Result r : scanner){
result = r;
rs = Bytes.toString(result.getValue(Bytes.toBytes("cf"),
Bytes.toBytes("mediaCodes")));
}
logger.debug(" recmd list : " + rs + " ");
table.close();
} catch (IOException e) {
logger.debug(" catch message : " + e.toString() + " ");
e.printStackTrace();
}
return rs;
}
/**
* @param userid
* @return
* id
*/
public String getListByUserIdForCluster(String userid) {
HTable table;
String rs = "";
String cluster = "";
Scan scan = null;
Result result = null;
ResultScanner scanner = null;
try {
//
table = (HTable) conn.getTable(TableName.valueOf("UserSchema"));
logger.debug(" tableName : " + table + " ");
scan = new Scan(Bytes.toBytes(userid),Bytes.toBytes(userid));
scan.addColumn("cf".getBytes(), "mediaCodes".getBytes());
scanner = table.getScanner(scan);
for (Result r : scanner){
result = r;
cluster = Bytes.toString(result.getValue(Bytes.toBytes("cf"),
Bytes.toBytes("mediaCodes")));
}
table.close();
logger.debug(" cluster : " + cluster + " ");
//
table = (HTable) conn.getTable(TableName.valueOf("KmeansSchema"));
scan = new Scan(Bytes.toBytes(cluster),Bytes.toBytes(cluster));
scan.addColumn("cf".getBytes(), "mediaCodes".getBytes());
scanner = table.getScanner(scan);
for (Result r : scanner){
result = r;
rs = Bytes.toString(result.getValue(Bytes.toBytes("cf"),
Bytes.toBytes("mediaCodes")));
}
logger.debug(" recmd list : " + rs + " ");
table.close();
} catch (IOException e) {
logger.debug(" catch message : " + e.toString() + " ");
e.printStackTrace();
}
return rs;
}
public Map getGYLFirstVersionField() {
String fieldString = prop.getProperty("GYLFirstVersionFields");
String[] fields = fieldString.split(",");
HashMap fieldsMap = new HashMap();
for (int i = 0; i < fields.length; i++) {
fieldsMap.put(i, fields[i]);
}
return fieldsMap;
}
public Map getGYLSecondVersionField() {
String fieldString = prop.getProperty("GYLSecondVersionFields");
String[] fields = fieldString.split(",");
HashMap fieldsMap = new HashMap();
for (int i = 0; i < fields.length; i++) {
fieldsMap.put(i, fields[i]);
}
return fieldsMap;
}
//
public static long rowCount(String tableName) {
long rowCount = 0;
@SuppressWarnings("resource")
AggregationClient aggregationClient = new AggregationClient(conf);
Scan scan = new Scan();
try {
rowCount = aggregationClient.rowCount(TableName.valueOf(tableName),
new LongColumnInterpreter(), scan);
} catch (Throwable e) {
e.printStackTrace();
}
return rowCount;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【 Hbase 】 【 03 】 자바 조작 habsedemo1.HbaseSnapShot 2.HbaseService 3.HbaseServiceImpl 4. pom 파일 jar 패키지 도입...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.