public static void writeTest(String tableStr) { try { Configuration conf = HBaseConfiguration.create(); byte[] tableName = Bytes.toBytes(tableStr); HConnection hConnection = HConnectionManager.createConnection(conf); HTableInterface table = hConnection.getTable(tableName); byte[] family = f0; List<Put> puts = new ArrayList<Put>(); for (int k = 0; k < 10; k++) // 写10行数据 { byte[] rowkey = Bytes.toBytes("rowKey_" + k); Put p = new Put(rowkey); byte[] value_id = Bytes.toBytes("123456"); byte[] value_user = Bytes.toBytes("mengqinghao" + k); p.add(family, qualifier_id, value_id); p.add(family, qualifier_user, value_user); puts.add(p); } table.put(puts); System.out.println("Puts done: " + puts.size()); table.close(); // very important } catch (IOException e) { e.printStackTrace(); } }
/* (non-Javadoc) * @see com.hazelcast.core.MapLoader#loadAllKeys() */ @Override public Set<String> loadAllKeys() { Set<String> keySet = null; if (allowLoadAll) { keySet = new HashSet<String>(); HTableInterface hti = null; try { hti = pool.getTable(tableName); Scan s = new Scan(); s.addColumn(family, qualifier); ResultScanner rs = hti.getScanner(s); Result r = null; while ((r = rs.next()) != null) { String k = new String(r.getRow()); keySet.add(k); } } catch (IOException e) { LOG.error("IOException while loading all keys", e); } finally { if (hti != null) { pool.putTable(hti); } } } return keySet; }
/* * (non-Javadoc) * * @see com.hazelcast.core.MapLoader#load(java.lang.Object) */ @Override public String load(String key) { String retval = null; if (allowLoad) { HTableInterface table = null; try { Get g = new Get(Bytes.toBytes(key)); table = pool.getTable(tableName); Result r = table.get(g); byte[] value = r.getValue(family, qualifier); if (value != null) { if (outputFormatType == StoreFormatType.SMILE) { retval = jsonSmileConverter.convertFromSmile(value); } else { retval = new String(value); } } } catch (IOException e) { LOG.error("Value did not exist for row: " + key, e); } finally { if (pool != null && table != null) { pool.putTable(table); } } } return retval; }
@Override public void mutateMany(Map<String, Map<ByteBuffer, KCVMutation>> mutations, StoreTransaction txh) throws StorageException { final long delTS = System.currentTimeMillis(); final long putTS = delTS + 1; Map<ByteBuffer, Pair<Put, Delete>> commandsPerKey = convertToCommands(mutations, putTS, delTS); List<Row> batch = new ArrayList<Row>(commandsPerKey.size()); // actual batch operation // convert sorted commands into representation required for 'batch' operation for (Pair<Put, Delete> commands : commandsPerKey.values()) { if (commands.getFirst() != null) batch.add(commands.getFirst()); if (commands.getSecond() != null) batch.add(commands.getSecond()); } try { HTableInterface table = null; try { table = connectionPool.getTable(tableName); table.batch(batch); table.flushCommits(); } finally { IOUtils.closeQuietly(table); } } catch (IOException e) { throw new TemporaryStorageException(e); } catch (InterruptedException e) { throw new TemporaryStorageException(e); } waitUntil(putTS); }
/* * (non-Javadoc) * * @see com.hazelcast.core.MapStore#store(java.lang.Object, * java.lang.Object) */ @Override public void store(String key, String value) { HTableInterface table = null; try { table = pool.getTable(tableName); try { byte[] rowId = prefixDate ? IdUtil.bucketizeId(key) : Bytes.toBytes(key); Put p = new Put(rowId); if (outputFormatType == StoreFormatType.SMILE) { p.add(family, qualifier, jsonSmileConverter.convertToSmile(value)); } else { p.add(family, qualifier, Bytes.toBytes(value)); } table.put(p); } catch (NumberFormatException nfe) { LOG.error("Encountered bad key: " + key, nfe); } } catch (IOException e) { LOG.error("Error during put", e); } finally { if (table != null) { pool.putTable(table); } } }
public static void deleteTest(String tableStr) { try { Configuration conf = HBaseConfiguration.create(); byte[] tableName = Bytes.toBytes(tableStr); HConnection hConnection = HConnectionManager.createConnection(conf); HTableInterface table = hConnection.getTable(tableName); byte[] startRow = Bytes.toBytes("rowKey_1"); byte[] stopRow = Bytes.toBytes("rowKey_3"); byte[] family = f0; Scan scan = new Scan(); scan.addFamily(family); scan.setMaxVersions(1); // scan.setStartRow(startRow); // scan.setStopRow(stopRow); ResultScanner scanner = table.getScanner(scan); Result result = scanner.next(); List<Delete> delete = new ArrayList<Delete>(); while (result != null) { Delete del = new Delete(result.getRow()); delete.add(del); result = scanner.next(); } table.delete(delete); System.out.println("delete done"); table.close(); // very important } catch (IOException e) { e.printStackTrace(); } }
public static void readTest(String tableStr, String row) { try { Configuration conf = HBaseConfiguration.create(); byte[] tableName = Bytes.toBytes(tableStr); HConnection hConnection = HConnectionManager.createConnection(conf); HTableInterface table = hConnection.getTable(tableName); byte[] rowkey = Bytes.toBytes(row); Get get = new Get(rowkey); get.addFamily(f0); Result result = table.get(get); NavigableMap<byte[], byte[]> m = result.getFamilyMap(f0); if (m == null || m.isEmpty()) { System.err.println("Empty." + m); return; } for (Map.Entry<byte[], byte[]> entry : m.entrySet()) { String qualifier = Bytes.toString(entry.getKey()); String value = Bytes.toString(entry.getValue()); System.out.println(qualifier + ":" + value); } table.close(); // very important } catch (IOException e) { e.printStackTrace(); } }
public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HTableInterface literaturesTable = new HTable(conf, "pb_literatures"); Scan s = new Scan(); s.addFamily(Bytes.toBytes("info")); ResultScanner rs = literaturesTable.getScanner(s); for (Result r : rs) { byte[] b = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("title")); System.out.println(Bytes.toString(b)); } }
/* * (non-Javadoc) * * @see com.hazelcast.core.MapStore#delete(java.lang.Object) */ @Override public void delete(String key) { if (allowDelete) { HTableInterface table = null; try { Delete d = new Delete(Bytes.toBytes(key)); table = pool.getTable(tableName); table.delete(d); } catch (IOException e) { LOG.error("IOException while deleting key: " + key, e); } finally { if (table != null) { pool.putTable(table); } } } }
@Override public <T extends CoprocessorProtocol, R> void coprocessorExec( Class<T> protocol, byte[] startKey, byte[] endKey, Batch.Call<T, R> callable, Batch.Callback<R> callback) throws IOException, Throwable { table.coprocessorExec(protocol, startKey, endKey, callable, callback); }
public static void scanTest(String tableStr) { try { Configuration conf = HBaseConfiguration.create(); byte[] tableName = Bytes.toBytes(tableStr); HConnection hConnection = HConnectionManager.createConnection(conf); HTableInterface table = hConnection.getTable(tableName); byte[] startRow = Bytes.toBytes("rowKey_0"); byte[] stopRow = Bytes.toBytes("rowKey_6"); byte[] family = f0; Scan scan = new Scan(); scan.addFamily(family); scan.setMaxVersions(1); // scan.setStartRow(startRow); // scan.setStopRow(stopRow); int count = 0; ResultScanner scanner = table.getScanner(scan); Result result = scanner.next(); while (result != null) { String rowKey = Bytes.toString(result.getRow()); NavigableMap<byte[], byte[]> m = result.getFamilyMap(family); if (m == null || m.isEmpty()) { System.err.println("Empty." + m); return; } for (Map.Entry<byte[], byte[]> entry : m.entrySet()) { String qualifier = Bytes.toString(entry.getKey()); String value = Bytes.toString(entry.getValue()); System.out.println(rowKey + ":" + qualifier + ":" + value); } result = scanner.next(); count++; System.out.println("-----------------------------"); } table.close(); // very important System.out.println("count:" + count); } catch (IOException e) { e.printStackTrace(); } }
@Override public <T extends Service, R> void coprocessorService( Class<T> service, byte[] startKey, byte[] endKey, Batch.Call<T, R> callable, Callback<R> callback) throws ServiceException, Throwable { table.coprocessorService(service, startKey, endKey, callable, callback); }
/** * Puts the specified HTable back into the pool. * * <p>If the pool already contains <i>maxSize</i> references to the table, then the table instance * gets closed after flushing buffered edits. * * @param table table */ private void returnTable(HTableInterface table) throws IOException { // this is the old putTable method renamed and made private String tableName = Bytes.toString(table.getTableName()); if (tables.size(tableName) >= maxSize) { // release table instance since we're not reusing it this.tables.remove(tableName, table); this.tableFactory.releaseHTableInterface(table); return; } tables.put(tableName, table); }
/* * (non-Javadoc) * * @see com.hazelcast.core.MapStore#deleteAll(java.util.Collection) */ @Override public void deleteAll(Collection<String> keys) { if (allowDelete) { HTableInterface table = null; try { List<Delete> deletes = new ArrayList<Delete>(); for (String k : keys) { Delete d = new Delete(Bytes.toBytes(k)); deletes.add(d); } table = pool.getTable(tableName); table.delete(deletes); } catch (IOException e) { LOG.error("IOException while deleting values", e); } finally { if (table != null) { pool.putTable(table); } } } }
/** * Code for each 'client' to run. * * @param id * @param c * @param sharedConnection * @throws IOException */ static void cycle(int id, final Configuration c, final HConnection sharedConnection) throws IOException { HTableInterface table = sharedConnection.getTable(BIG_USER_TABLE); table.setAutoFlushTo(false); long namespaceSpan = c.getLong("hbase.test.namespace.span", 1000000); long startTime = System.currentTimeMillis(); final int printInterval = 100000; Random rd = new Random(id); boolean get = c.getBoolean("hbase.test.do.gets", false); try { Stopwatch stopWatch = new Stopwatch(); stopWatch.start(); for (int i = 0; i < namespaceSpan; i++) { byte[] b = format(rd.nextLong()); if (get) { Get g = new Get(b); table.get(g); } else { Put p = new Put(b); p.add(HConstants.CATALOG_FAMILY, b, b); table.put(p); } if (i % printInterval == 0) { LOG.info("Put " + printInterval + "/" + stopWatch.elapsedMillis()); stopWatch.reset(); stopWatch.start(); } } LOG.info( "Finished a cycle putting " + namespaceSpan + " in " + (System.currentTimeMillis() - startTime) + "ms"); } finally { table.close(); } }
/* * (non-Javadoc) * * @see com.hazelcast.core.MapLoader#loadAll(java.util.Collection) */ @Override public Map<String, String> loadAll(Collection<String> keys) { if (allowLoadAll) { List<Get> gets = new ArrayList<Get>(keys.size()); for (String k : keys) { Get g = new Get(Bytes.toBytes(k)); gets.add(g); } HTableInterface table = null; Map<String, String> kvMap = new HashMap<String, String>(); try { table = pool.getTable(tableName); table.get(gets); Result[] result = table.get(gets); for (Result r : result) { byte[] value = r.getValue(family, qualifier); if (value != null) { if (outputFormatType == StoreFormatType.SMILE) { kvMap.put( new String(r.getRow()), jsonSmileConverter.convertFromSmile(r.getValue(family, qualifier))); } else { kvMap.put(new String(r.getRow()), new String(r.getValue(family, qualifier))); } } } } catch (IOException e) { LOG.error("IOException while getting values", e); } finally { if (pool != null && table != null) { pool.putTable(table); } } } return null; }
@Override public <R> void batchCallback( List<? extends Row> actions, Object[] results, Callback<R> callback) throws IOException, InterruptedException { table.batchCallback(actions, results, callback); }
@Override public Result increment(Increment increment) throws IOException { return table.increment(increment); }
@Override public long incrementColumnValue( byte[] row, byte[] family, byte[] qualifier, long amount, boolean writeToWAL) throws IOException { return table.incrementColumnValue(row, family, qualifier, amount, writeToWAL); }
@Override public boolean isAutoFlush() { return table.isAutoFlush(); }
@Override public void flushCommits() throws IOException { table.flushCommits(); }
@Override public void setWriteBufferSize(long writeBufferSize) throws IOException { table.setWriteBufferSize(writeBufferSize); }
@Override public CoprocessorRpcChannel coprocessorService(byte[] row) { return table.coprocessorService(row); }
@Override public void setAutoFlush(boolean autoFlush, boolean clearBufferOnFail) { table.setAutoFlush(autoFlush, clearBufferOnFail); }
@Override public long getWriteBufferSize() { return table.getWriteBufferSize(); }
@Override public void setAutoFlush(boolean autoFlush) { table.setAutoFlush(autoFlush); }
@Override public Result append(Append append) throws IOException { return table.append(append); }
@Override public void mutateRow(RowMutations rm) throws IOException { table.mutateRow(rm); }
@Override public <R> Object[] batchCallback(List<? extends Row> actions, Callback<R> callback) throws IOException, InterruptedException { return table.batchCallback(actions, callback); }
@Override public <T extends Service, R> Map<byte[], R> coprocessorService( Class<T> service, byte[] startKey, byte[] endKey, Batch.Call<T, R> callable) throws ServiceException, Throwable { return table.coprocessorService(service, startKey, endKey, callable); }