下面列出了org.apache.hadoop.hbase.client.HConnection#getTable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void fullScan(HConnection conn, boolean[] hits, Stats stats) throws IOException {
HTableInterface table = conn.getTable(TEST_TABLE);
try {
stats.markStart();
Scan scan = new Scan();
scan.addFamily(CF);
ResultScanner scanner = table.getScanner(scan);
int i = 0;
for (Result r : scanner) {
if (hits[i])
stats.consume(r);
dot(i, N_ROWS);
i++;
}
stats.markEnd();
} finally {
IOUtils.closeQuietly(table);
}
}
public CubeSegmentTupleIterator(CubeSegment cubeSeg, Collection<HBaseKeyRange> keyRanges, HConnection conn, Collection<TblColRef> dimensions, TupleFilter filter, Collection<TblColRef> groupBy, Collection<RowValueDecoder> rowValueDecoders, StorageContext context) {
this.cube = cubeSeg.getCubeInstance();
this.cubeSeg = cubeSeg;
this.dimensions = dimensions;
this.filter = filter;
this.groupBy = groupBy;
this.rowValueDecoders = rowValueDecoders;
this.context = context;
this.tableName = cubeSeg.getStorageLocationIdentifier();
this.rowKeyDecoder = new RowKeyDecoder(this.cubeSeg);
this.scanCount = 0;
try {
this.table = conn.getTable(tableName);
} catch (Throwable t) {
throw new StorageException("Error when open connection to table " + tableName, t);
}
this.rangeIterator = keyRanges.iterator();
scanNextRange();
}
@Override
public boolean initializeAdapter() {
// Initialize HBase Table
Configuration conf = null;
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", _quorum);
conf.set("hbase.zookeeper.property.clientPort", _port);
try {
LOGGER.debug("=======Connecting to HBASE===========");
LOGGER.debug("=======ZOOKEEPER = "
+ conf.get("hbase.zookeeper.quorum"));
HConnection connection = HConnectionManager.createConnection(conf);
table = connection.getTable(_tableName);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
LOGGER.debug("=======Unable to Connect to HBASE===========");
e.printStackTrace();
}
return false;
}
@Override
public boolean initializeAdapter() {
// Initialize HBase Table
Configuration conf = null;
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", _quorum);
conf.set("hbase.zookeeper.property.clientPort", _port);
try {
LOGGER.debug("=======Connecting to HBASE===========");
LOGGER.debug("=======ZOOKEEPER = "
+ conf.get("hbase.zookeeper.quorum"));
HConnection connection = HConnectionManager.createConnection(conf);
table = connection.getTable(_tableName);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
LOGGER.debug("=======Unable to Connect to HBASE===========");
e.printStackTrace();
}
return false;
}
@Override
public void init() throws IOException {
super.init();
HBaseTablespace space = (HBaseTablespace) TablespaceManager.get(uri);
HConnection hconn = space.getConnection();
htable = hconn.getTable(columnMapping.getHbaseTableName());
htable.setAutoFlushTo(false);
htable.setWriteBufferSize(5 * 1024 * 1024);
}
public static void populateUserProfile(HConnection connection, UserProfile userProfile) throws Exception {
HTableInterface table = connection.getTable(HBaseTableMetaModel.profileCacheTableName);
try {
Put put = new Put(convertKeyToRowKey(HBaseTableMetaModel.profileCacheTableName, userProfile.userId));
put.add(HBaseTableMetaModel.profileCacheColumnFamily, HBaseTableMetaModel.profileCacheJsonColumn, Bytes.toBytes(userProfile.getJSONObject().toString()));
put.add(HBaseTableMetaModel.profileCacheColumnFamily, HBaseTableMetaModel.profileCacheTsColumn, Bytes.toBytes(System.currentTimeMillis()));
table.put(put);
} finally {
table.close();
}
}
public static void populateValidationRules(HConnection connection, ValidationRules rules) throws Exception {
HTableInterface table = connection.getTable(HBaseTableMetaModel.profileCacheTableName);
try {
Put put = new Put(HBaseTableMetaModel.validationRulesRowKey);
put.add(HBaseTableMetaModel.profileCacheColumnFamily, HBaseTableMetaModel.validationRulesRowKey, Bytes.toBytes(rules.getJSONObject().toString()));
table.put(put);
} finally {
table.close();
}
}
private static void testColumnScan(HConnection conn, List<Pair<Integer, Integer>> colScans) throws IOException {
Stats stats = new Stats("COLUMN_SCAN");
HTableInterface table = conn.getTable(TEST_TABLE);
try {
stats.markStart();
int nLogicCols = colScans.size();
int nLogicRows = colScans.get(0).getSecond() - colScans.get(0).getFirst();
Scan[] scans = new Scan[nLogicCols];
ResultScanner[] scanners = new ResultScanner[nLogicCols];
for (int i = 0; i < nLogicCols; i++) {
scans[i] = new Scan();
scans[i].addFamily(CF);
scanners[i] = table.getScanner(scans[i]);
}
for (int i = 0; i < nLogicRows; i++) {
for (int c = 0; c < nLogicCols; c++) {
Result r = scanners[c].next();
stats.consume(r);
}
dot(i, nLogicRows);
}
stats.markEnd();
} finally {
IOUtils.closeQuietly(table);
}
}
public HBaseClientKVIterator(HConnection hconn, String tableName, byte[] family, byte[] qualifier) throws IOException {
this.family = family;
this.qualifier = qualifier;
this.table = hconn.getTable(tableName);
this.scanner = table.getScanner(family, qualifier);
this.iterator = scanner.iterator();
}
public boolean initializeAdapter() {
Configuration conf = null;
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", _quorum);
conf.set("hbase.zookeeper.property.clientPort", _port);
conf.set("zookeeper.session.timeout", "20");
conf.set("hbase.rpc.timeout", "20");
conf.set("zookeeper.recovery.retry", "1");
conf.set("zookeeper.recovery.retry.intervalmill", "1");
try {
LOG.trace("[OpenSOC] Connecting to HBase");
LOG.trace("[OpenSOC] ZOOKEEPER = "
+ conf.get("hbase.zookeeper.quorum"));
LOG.trace("[OpenSOC] CONNECTING TO HBASE WITH: " + conf);
HConnection connection = HConnectionManager.createConnection(conf);
LOG.trace("[OpenSOC] CONNECTED TO HBASE");
table = connection.getTable(_table_name);
LOG.trace("--------CONNECTED TO TABLE: " + table);
JSONObject tester = enrich("cisco.com");
if (tester.keySet().size() == 0)
throw new IOException(
"Either HBASE is misconfigured or whois table is missing");
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public HTableInterface getHTableInterface(String tableName) {
HConnection con = getHConnection();
try {
return con.getTable(tableName);
} catch (IOException e) {
logger.error("Cannot to get HTableInterface.", e.getCause());
}
return null;
}
@Test
public void testPut() throws IOException {
Configuration configuration = HBaseConfiguration.create();
HConnection connection = HConnectionManager.createConnection(configuration);
HTableInterface table = connection.getTable("page");
// use the table as needed, for a single operation and a single thread
Put put = new Put("2".getBytes());
put.add("content".getBytes(), null, "我吃包子".getBytes());
put.add("title".getBytes(), null, "吃包子".getBytes());
put.add("url".getBytes(), null, "http://www.sina.com.cn".getBytes());
table.put(put);
table.close();
connection.close();
}
@SuppressWarnings("deprecation")
@Override
public HTableInterface getTable(byte[] tableName, HConnection connection, ExecutorService pool) throws IOException {
return connection.getTable(tableName, pool);
}
private static void jumpScan(HConnection conn, boolean[] hits, Stats stats) throws IOException {
final int jumpThreshold = 6; // compensate for Scan() overhead, totally by experience
HTableInterface table = conn.getTable(TEST_TABLE);
try {
stats.markStart();
int i = 0;
while (i < N_ROWS) {
int start, end;
for (start = i; start < N_ROWS; start++) {
if (hits[start])
break;
}
for (end = start + 1; end < N_ROWS; end++) {
boolean isEnd = true;
for (int j = 0; j < jumpThreshold && end + j < N_ROWS; j++)
if (hits[end + j])
isEnd = false;
if (isEnd)
break;
}
if (start < N_ROWS) {
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(start));
scan.setStopRow(Bytes.toBytes(end));
scan.addFamily(CF);
ResultScanner scanner = table.getScanner(scan);
i = start;
for (Result r : scanner) {
stats.consume(r);
dot(i, N_ROWS);
i++;
}
}
i = end;
}
stats.markEnd();
} finally {
IOUtils.closeQuietly(table);
}
}
private static void prepareData(HConnection conn) throws IOException {
HTableInterface table = conn.getTable(TEST_TABLE);
try {
// check how many rows existing
int nRows = 0;
Scan scan = new Scan();
scan.setFilter(new KeyOnlyFilter());
ResultScanner scanner = table.getScanner(scan);
for (Result r : scanner) {
r.getRow(); // nothing to do
nRows++;
}
if (nRows > 0) {
System.out.println(nRows + " existing rows");
if (nRows != N_ROWS)
throw new IOException("Expect " + N_ROWS + " rows but it is not");
return;
}
// insert rows into empty table
System.out.println("Writing " + N_ROWS + " rows to " + TEST_TABLE);
long nBytes = 0;
for (int i = 0; i < N_ROWS; i++) {
byte[] rowkey = Bytes.toBytes(i);
Put put = new Put(rowkey);
byte[] cell = randomBytes();
put.add(CF, QN, cell);
table.put(put);
nBytes += cell.length;
dot(i, N_ROWS);
}
System.out.println();
System.out.println("Written " + N_ROWS + " rows, " + nBytes + " bytes");
} finally {
IOUtils.closeQuietly(table);
}
}
@Test
public static void test() throws IOException {
String hbaseUrl = "hbase"; // use hbase-site.xml on classpath
HConnection conn = null;
HTableInterface table = null;
try {
conn = HBaseConnection.get(hbaseUrl);
table = conn.getTable("KYLIN_II_YTYWP3CQGJ");
ResultScanner scanner = table.getScanner(CF, QN);
StringBuffer sb = new StringBuffer();
while (true) {
Result r = scanner.next();
if (r == null)
break;
Cell[] cells = r.rawCells();
Cell c = cells[0];
k.set(c.getRowArray(), c.getRowOffset(), c.getRowLength());
v.set(c.getValueArray(), c.getValueOffset(), c.getValueLength());
byte[] row = k.copyBytes();
byte[] value = v.copyBytes();
// byte[] row = r.getRow();
// byte[] value = r.getValue(CF, QN);
//
sb.append("row length: " + row.length + "\r\n");
sb.append(BytesUtil.toReadableText(row) + "\r\n");
sb.append("value length: " + value.length + "\r\n");
sb.append(BytesUtil.toReadableText(value) + "\r\n");
}
System.out.println(sb.toString());
FileUtils.writeStringToFile(new File("/Users/honma/Desktop/a3"), sb.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (table != null)
table.close();
if (conn != null)
conn.close();
}
}
public static void main(String[] args) throws IOException {
String metadataUrl = args[0];
String hbaseTable = args[1];
System.out.println("Hello friend.");
Configuration hconf = HadoopUtil.newHBaseConfiguration(metadataUrl);
if (User.isHBaseSecurityEnabled(hconf)) {
try {
System.out.println("--------------Getting kerberos credential for user " + UserGroupInformation.getCurrentUser().getUserName());
TokenUtil.obtainAndCacheToken(hconf, UserGroupInformation.getCurrentUser());
} catch (InterruptedException e) {
System.out.println("--------------Error while getting kerberos credential for user " + UserGroupInformation.getCurrentUser().getUserName());
}
}
Scan scan = new Scan();
int limit = 20;
HConnection conn = null;
HTableInterface table = null;
ResultScanner scanner = null;
try {
conn = HConnectionManager.createConnection(hconf);
table = conn.getTable(hbaseTable);
scanner = table.getScanner(scan);
int count = 0;
for (Result r : scanner) {
byte[] rowkey = r.getRow();
System.out.println(Bytes.toStringBinary(rowkey));
count++;
if (count == limit)
break;
}
} finally {
if (scanner != null) {
scanner.close();
}
if (table != null) {
table.close();
}
if (conn != null) {
conn.close();
}
}
}
public EndpointTupleIterator(IISegment segment, TupleFilter rootFilter, Collection<TblColRef> groupBy, List<FunctionDesc> measures, StorageContext context, HConnection conn) throws Throwable {
String tableName = segment.getStorageLocationIdentifier();
table = conn.getTable(tableName);
factTableName = segment.getIIDesc().getFactTableName();
if (rootFilter == null) {
rootFilter = ConstantTupleFilter.TRUE;
}
if (groupBy == null) {
groupBy = Sets.newHashSet();
}
if (measures == null) {
measures = Lists.newArrayList();
}
//this method will change measures
rewriteMeasureParameters(measures, segment.getColumns());
this.seg = segment;
this.context = context;
this.measures = measures;
this.columns = segment.getColumns();
this.columnNames = getColumnNames(columns);
this.tupleInfo = buildTupleInfo();
this.tableRecordInfo = new TableRecordInfo(this.seg);
this.pushedDownRowType = CoprocessorRowType.fromTableRecordInfo(tableRecordInfo, this.columns);
this.pushedDownFilter = CoprocessorFilter.fromFilter(this.seg, rootFilter);
for (TblColRef column : this.pushedDownFilter.getUnstrictlyFilteredColumns()) {
groupBy.add(column);
}
this.pushedDownProjector = CoprocessorProjector.makeForEndpoint(tableRecordInfo, groupBy);
this.pushedDownAggregators = EndpointAggregators.fromFunctions(tableRecordInfo, measures);
IIProtos.IIRequest endpointRequest = prepareRequest();
regionResponsesIterator = getResults(endpointRequest, table);
if (this.regionResponsesIterator.hasNext()) {
this.tupleIterator = new SingleRegionTupleIterator(this.regionResponsesIterator.next());
} else {
this.tupleIterator = ITupleIterator.EMPTY_TUPLE_ITERATOR;
}
}