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 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(); } }
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 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 int run(String[] arg0) throws Exception { int errCode = 0; // TODO: Make command options. // How many servers to fake. final int servers = 1; // How many regions to put on the faked servers. final int regions = 100000; // How many 'keys' in the faked regions. final long namespaceSpan = 50000000; // How long to take to pause after doing a put; make this long if you want to fake a struggling // server. final long multiPause = 0; // Check args make basic sense. if ((namespaceSpan < regions) || (regions < servers)) { throw new IllegalArgumentException( "namespaceSpan=" + namespaceSpan + " must be > regions=" + regions + " which must be > servers=" + servers); } // Set my many servers and many regions faking connection in place. getConf().set("hbase.client.connection.impl", ManyServersManyRegionsConnection.class.getName()); // Use simple kv registry rather than zk getConf().set("hbase.client.registry.impl", SimpleRegistry.class.getName()); // When to report fails. Default is we report the 10th. This means we'll see log everytime // an exception is thrown -- usually RegionTooBusyException when we have more than // hbase.test.multi.too.many requests outstanding at any time. getConf().setInt("hbase.client.start.log.errors.counter", 0); // Ugly but this is only way to pass in configs.into ManyServersManyRegionsConnection class. getConf().setInt("hbase.test.regions", regions); getConf().setLong("hbase.test.namespace.span", namespaceSpan); getConf().setLong("hbase.test.servers", servers); getConf().set("hbase.test.tablename", Bytes.toString(BIG_USER_TABLE)); getConf().setLong("hbase.test.multi.pause.when.done", multiPause); // Let there be ten outstanding requests at a time before we throw RegionBusyException. getConf().setInt("hbase.test.multi.too.many", 10); final int clients = 2; // Have them all share the same connection so they all share the same instance of // ManyServersManyRegionsConnection so I can keep an eye on how many requests by server. final ExecutorService pool = Executors.newCachedThreadPool(Threads.getNamedThreadFactory("p")); // Executors.newFixedThreadPool(servers * 10, Threads.getNamedThreadFactory("p")); // Share a connection so I can keep counts in the 'server' on concurrency. final HConnection sharedConnection = HConnectionManager.createConnection(getConf() /*, pool*/); try { Thread[] ts = new Thread[clients]; for (int j = 0; j < ts.length; j++) { final int id = j; ts[j] = new Thread("" + j) { final Configuration c = getConf(); @Override public void run() { try { cycle(id, c, sharedConnection); } catch (IOException e) { e.printStackTrace(); } } }; ts[j].start(); } for (int j = 0; j < ts.length; j++) { ts[j].join(); } } finally { sharedConnection.close(); } return errCode; }
/** * Create a new ClientSmallScanner for the specified table. An HConnection will be retrieved using * the passed Configuration. Note that the passed {@link Scan} 's start row maybe changed. * * @param conf The {@link Configuration} to use. * @param scan {@link Scan} to use in this scanner * @param tableName The table that we wish to rangeGet * @throws IOException */ public ClientSmallScanner(final Configuration conf, final Scan scan, final TableName tableName) throws IOException { this(conf, scan, tableName, HConnectionManager.getConnection(conf)); }