@Override public Row get() { if (row == null) { if (searchRow != null) { Result r; try { Get get = new Get(HBaseUtils.toBytes(searchRow.getRowKey())); get.setTimeStamp(searchTimestamp); if (columns != null) { for (Column c : columns) { if (c.isRowKeyColumn()) continue; else if (c.getColumnFamilyName() != null) get.addColumn(c.getColumnFamilyNameAsBytes(), c.getNameAsBytes()); else get.addColumn(defaultColumnFamilyName, c.getNameAsBytes()); } } r = secondaryIndex.dataTable.get(get); } catch (IOException e) { throw DbException.convert(e); } if (r != null) { Value[] data = new Value[columns.size()]; Value rowKey = ValueString.get(Bytes.toString(r.getRow())); if (columns != null) { int i = 0; for (Column c : columns) { i = c.getColumnId(); if (c.isRowKeyColumn()) data[i] = rowKey; else data[i] = HBaseUtils.toValue( // r.getValue(c.getColumnFamilyNameAsBytes(), c.getNameAsBytes()), c.getType()); } } row = new HBaseRow(null, rowKey, data, Row.MEMORY_CALCULATE, r); } } } return row; }
public static SessionRemote getMasterSessionRemote(Properties info) { return getSessionRemote(info, HBaseUtils.getMasterURL()); }
public class SessionRemotePool { private static final int corePoolSize = HBaseUtils.getConfiguration() .getInt( HBaseConstants.SESSION_CORE_POOL_SIZE, HBaseConstants.DEFAULT_SESSION_CORE_POOL_SIZE); // key是Master或RegionServer的URL private static final ConcurrentHashMap<String, ConcurrentLinkedQueue<SessionRemote>> // pool = new ConcurrentHashMap<String, ConcurrentLinkedQueue<SessionRemote>>(); private static ConcurrentLinkedQueue<SessionRemote> getQueue(String url) { ConcurrentLinkedQueue<SessionRemote> queue = pool.get(url); if (queue == null) { // 避免多个线程生成不同的ConcurrentLinkedQueue实例 synchronized (SessionRemotePool.class) { queue = pool.get(url); if (queue == null) { queue = new ConcurrentLinkedQueue<SessionRemote>(); pool.put(url, queue); } } } return queue; } public static SessionRemote getMasterSessionRemote(Properties info) { return getSessionRemote(info, HBaseUtils.getMasterURL()); } public static SessionRemote getSessionRemote(Properties info, String url) { SessionRemote sr = getQueue(url).poll(); if (sr == null || sr.isClosed()) { byte[] userPasswordHash = null; byte[] filePasswordHash = null; Properties prop = new Properties(); String key; for (Object o : info.keySet()) { key = o.toString(); if (key.equalsIgnoreCase("_userPasswordHash_")) userPasswordHash = (byte[]) info.get(key); else if (key.equalsIgnoreCase("_filePasswordHash_")) filePasswordHash = (byte[]) info.get(key); else prop.setProperty(key, info.getProperty(key)); } ConnectionInfo ci = new ConnectionInfo(url, prop); ci.setUserPasswordHash(userPasswordHash); ci.setFilePasswordHash(filePasswordHash); sr = (SessionRemote) new SessionRemote(ci).connectEmbeddedOrServer(false); } return sr; } public static void release(SessionRemote sr) { if (sr == null || sr.isClosed()) return; ConcurrentLinkedQueue<SessionRemote> queue = getQueue(sr.getURL()); if (queue.size() > corePoolSize) sr.close(); else queue.offer(sr); } public static CommandRemote getCommandRemote( HBaseSession originalSession, Prepared prepared, // String url, String sql) throws Exception { SessionRemote sessionRemote = originalSession.getSessionRemote(url); if (sessionRemote != null && sessionRemote.isClosed()) sessionRemote = null; boolean isNew = false; if (sessionRemote == null) { isNew = true; sessionRemote = getSessionRemote(originalSession.getOriginalProperties(), url); } if (sessionRemote.getTransaction() == null) sessionRemote.setTransaction(originalSession.getTransaction()); if (isNew) originalSession.addSessionRemote(url, sessionRemote); return getCommandRemote(sessionRemote, sql, prepared.getParameters(), prepared.getFetchSize()); } public static CommandRemote getCommandRemote( SessionRemote sr, String sql, List<Parameter> parameters, int fetchSize) { CommandRemote cr = (CommandRemote) sr.prepareCommand(sql, fetchSize); // 传递最初的参数值到新的CommandRemote if (parameters != null) { ArrayList<? extends ParameterInterface> newParams = cr.getParameters(); for (int i = 0, size = parameters.size(); i < size; i++) { newParams.get(i).setValue(parameters.get(i).getParamValue(), true); } } return cr; } public static void check() { for (ConcurrentLinkedQueue<SessionRemote> queue : pool.values()) for (SessionRemote sr : queue) sr.checkTransfers(); } }