@Override public Object run() throws Exception { Put p = new Put(TEST_ROW); p.addColumn(family1, qualifier, Bytes.toBytes("v1")); try (Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(tableName)) { t.put(p); } return null; } };
@BeforeClass public static void setUp() throws Exception { TEST_UTIL.startMiniCluster(3); SPLIT_KEYS = new byte[8][]; for (int i = 111; i < 999; i += 111) { SPLIT_KEYS[i / 111 - 1] = Bytes.toBytes(String.format("%03d", i)); } CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get(); }
@Override public Object run() throws Exception { try (Connection conn = ConnectionFactory.createConnection(conf); Table acl = conn.getTable(AccessControlLists.ACL_TABLE_NAME)) { BlockingRpcChannel service = acl.coprocessorService(TEST_TABLE.getName()); AccessControlService.BlockingInterface protocol = AccessControlService.newBlockingStub(service); AccessControlUtil.getUserPermissions(null, protocol, Bytes.toBytes(namespace1), "dummy"); } return null; } };
@Test public void testRegionObserverSingleRegion() throws IOException { final TableName tableName = TableName.valueOf(name.getMethodName()); try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration()); Admin admin = connection.getAdmin()) { admin.createTable( new HTableDescriptor(tableName) .addFamily(new HColumnDescriptor(foo)) // add the coprocessor for the region .addCoprocessor(CustomRegionObserver.class.getName())); try (Table table = connection.getTable(tableName)) { table.get(new Get(foo)); table.get(new Get(foo)); // 2 gets } } assertPreGetRequestsCounter(CustomRegionObserver.class); }
@Test public void testConnectionRideOverClusterRestart() throws IOException, InterruptedException { Configuration config = new Configuration(TEST_UTIL.getConfiguration()); final TableName tableName = TableName.valueOf(name.getMethodName()); TEST_UTIL.createTable(tableName, new byte[][] {FAM_NAM}).close(); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(tableName); // this will cache the meta location and table's region location table.get(new Get(Bytes.toBytes("foo"))); // restart HBase TEST_UTIL.shutdownMiniHBaseCluster(); TEST_UTIL.restartHBaseCluster(2); // this should be able to discover new locations for meta and table's region table.get(new Get(Bytes.toBytes("foo"))); TEST_UTIL.deleteTable(tableName); table.close(); connection.close(); }
@Test(expected=RetriesExhaustedException.class) public void testSocketClosed() throws IOException, InterruptedException { TableName tableName = TableName.valueOf(name.getMethodName()); UTIL.createTable(tableName, fam1).close(); Configuration conf = new Configuration(UTIL.getConfiguration()); conf.set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY, MyRpcClientImpl.class.getName()); conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf(name.getMethodName())); table.get(new Get(Bytes.toBytes("asd"))); connection.close(); for (Socket socket : MyRpcClientImpl.savedSockets) { assertTrue("Socket + " + socket + " is not closed", socket.isClosed()); } } }
int numRegions = NB_SERVERS * 20; int regionReplication = 10; String tableName = htd.getTableName().getNameAsString() + "2"; htd = HTU.createTableDescriptor(tableName); htd.setRegionReplication(regionReplication); byte[] startKey = Bytes.toBytes("aaa"); byte[] endKey = Bytes.toBytes("zzz"); byte[][] splits = HTU.getRegionSplitStartKeys(startKey, endKey, numRegions); HTU.getAdmin().createTable(htd, startKey, endKey, numRegions); try (Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration()); Table table = connection.getTable(htd.getTableName())) { get.setConsistency(Consistency.TIMELINE); get.setReplicaId(j); table.get(get); // this should not block. Regions should be coming online HTU.deleteTableIfAny(TableName.valueOf(tableName));
@Override public void configure(JobConf job) { try { Connection connection = ConnectionFactory.createConnection(job); Table exampleTable = connection.getTable(TableName.valueOf("exampleDeprecatedTable")); // mandatory initializeTable(connection, exampleTable.getName()); byte[][] inputColumns = new byte [][] { Bytes.toBytes("columnA"), Bytes.toBytes("columnB") }; // mandatory setInputColumns(inputColumns); Filter exampleFilter = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator("aa.*")); // optional setRowFilter(exampleFilter); } catch (IOException exception) { throw new RuntimeException("Failed to configure for job.", exception); } }
@Test public void testPutGetWithDelegationToken() throws Exception { TableName tableName = getTestTableName(); byte[] family = Bytes.toBytes("f"); byte[] qualifier = Bytes.toBytes("q"); byte[] row = Bytes.toBytes("row"); byte[] value = Bytes.toBytes("data"); try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) { Admin admin = conn.getAdmin(); HTableDescriptor tableDescriptor = new HTableDescriptor(new HTableDescriptor(tableName)); tableDescriptor.addFamily(new HColumnDescriptor(family)); admin.createTable(tableDescriptor); try (Table table = conn.getTable(tableName)) { table.put(new Put(row).addColumn(family, qualifier, value)); Result result = table.get(new Get(row)); assertArrayEquals(value, result.getValue(family, qualifier)); } } } }
/** * simple test that just executes parts of the client * API that accept a pre-created Connection instance */ @Test public void testUnmanagedHConnection() throws IOException { final TableName tableName = TableName.valueOf(name.getMethodName()); TEST_UTIL.createTable(tableName, HConstants.CATALOG_FAMILY); Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); Table t = conn.getTable(tableName); Admin admin = conn.getAdmin(); assertTrue(admin.tableExists(tableName)); assertTrue(t.get(new Get(ROW)).isEmpty()); admin.close(); }
public void testRegionReplicaReplication(int regionReplication) throws Exception { // test region replica replication. Create a table with single region, write some data // ensure that data is replicated to the secondary region TableName tableName = TableName.valueOf("testRegionReplicaReplicationWithReplicas_" + regionReplication); HTableDescriptor htd = HTU.createTableDescriptor(tableName.toString()); htd.setRegionReplication(regionReplication); HTU.getAdmin().createTable(htd); TableName tableNameNoReplicas = TableName.valueOf("testRegionReplicaReplicationWithReplicas_NO_REPLICAS"); HTU.deleteTableIfAny(tableNameNoReplicas); HTU.createTable(tableNameNoReplicas, HBaseTestingUtility.fam1); Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration()); Table table = connection.getTable(tableName); Table tableNoReplicas = connection.getTable(tableNameNoReplicas); try { // load some data to the non-replicated table HTU.loadNumericRows(tableNoReplicas, HBaseTestingUtility.fam1, 6000, 7000); // load the data to the table HTU.loadNumericRows(table, HBaseTestingUtility.fam1, 0, 1000); verifyReplication(tableName, regionReplication, 0, 1000); } finally { table.close(); tableNoReplicas.close(); HTU.deleteTableIfAny(tableNameNoReplicas); connection.close(); } }
@Override protected void initialize(JobConf job) throws IOException { Path[] tableNames = FileInputFormat.getInputPaths(job); String colArg = job.get(COLUMN_LIST); String[] colNames = colArg.split(" "); byte [][] m_cols = new byte[colNames.length][]; for (int i = 0; i < m_cols.length; i++) { m_cols[i] = Bytes.toBytes(colNames[i]); } setInputColumns(m_cols); Connection connection = ConnectionFactory.createConnection(job); initializeTable(connection, TableName.valueOf(tableNames[0].getName())); }
@Override public Void run() throws Exception { Scan s = new Scan(); s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL)); try (Connection connection = ConnectionFactory.createConnection(conf); Table t = connection.getTable(table.getName())) { ResultScanner scanner = t.getScanner(s); Result result = scanner.next(); assertTrue(!result.isEmpty()); assertTrue(Bytes.equals(Bytes.toBytes("row2"), result.getRow())); result = scanner.next(); assertNull(result); } return null; } };
@Test public void testWALObserver() throws IOException { // Find out the MetricRegistry used by the CP using the global registries MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForWALCoprocessor( CustomWALObserver.class.getName()); Optional<MetricRegistry> registry = MetricRegistries.global().get(info); assertTrue(registry.isPresent()); Optional<Metric> metric = registry.get().get("walEditsCount"); assertTrue(metric.isPresent()); try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration()); Admin admin = connection.getAdmin()) { admin.createTable( new HTableDescriptor(TableName.valueOf(name.getMethodName())) .addFamily(new HColumnDescriptor("foo"))); Counter rollWalRequests = (Counter)metric.get(); long prevCount = rollWalRequests.getCount(); assertTrue(prevCount > 0); try (Table table = connection.getTable(TableName.valueOf(name.getMethodName()))) { table.put(new Put(foo).addColumn(foo, foo, foo)); } assertEquals(1, rollWalRequests.getCount() - prevCount); } }
@Override public Object run() throws Exception { Put p = new Put(TEST_ROW); p.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(1)); try(Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TEST_TABLE)) { t.checkAndMutate(TEST_ROW, TEST_FAMILY).qualifier(TEST_QUALIFIER) .ifEquals(Bytes.toBytes("test_value")).thenPut(p); } return null; } };
@Test public void testScanRawDeleteFamilyVersion() throws Exception { TableName tableName = TableName.valueOf(name.getMethodName()); TEST_UTIL.createTable(tableName, FAMILY); Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); conf.set(RPC_CODEC_CONF_KEY, ""); conf.set(DEFAULT_CODEC_CLASS, ""); try (Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(tableName)) { Delete delete = new Delete(ROW); delete.addFamilyVersion(FAMILY, 0L); table.delete(delete); Scan scan = new Scan(ROW).setRaw(true); ResultScanner scanner = table.getScanner(scan); int count = 0; while (scanner.next() != null) { count++; } assertEquals(1, count); } finally { TEST_UTIL.deleteTable(tableName); } }
private void verifyRows(TableName tableName) throws IOException { try (Connection conn = ConnectionFactory.createConnection(conf)) { try (Table t = conn.getTable(tableName)) { try (ResultScanner scanner = t.getScanner(new Scan())) { Result result; int rowCount = 0; while ((result = scanner.next()) != null) { byte[] value = result.getValue(TEST_FAMILY, TEST_QUALIFIER); Assert.assertArrayEquals(value, Bytes.toBytes(rowCount++)); } Assert.assertEquals(ROW_COUNT, rowCount); } } } }
@Test public void testMaxKeyValueSize() throws Exception { final TableName tableName = TableName.valueOf(name.getMethodName()); Configuration conf = TEST_UTIL.getConfiguration(); String oldMaxSize = conf.get(ConnectionConfiguration.MAX_KEYVALUE_SIZE_KEY); Table ht = TEST_UTIL.createTable(tableName, FAMILY); byte[] value = new byte[4 * 1024 * 1024]; Put put = new Put(ROW); put.addColumn(FAMILY, QUALIFIER, value); ht.put(put); try { TEST_UTIL.getConfiguration().setInt( ConnectionConfiguration.MAX_KEYVALUE_SIZE_KEY, 2 * 1024 * 1024); // Create new table so we pick up the change in Configuration. try (Connection connection = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) { try (Table t = connection.getTable(TableName.valueOf(FAMILY))) { put = new Put(ROW); put.addColumn(FAMILY, QUALIFIER, value); t.put(put); } } fail("Inserting a too large KeyValue worked, should throw exception"); } catch(Exception e) {} conf.set(ConnectionConfiguration.MAX_KEYVALUE_SIZE_KEY, oldMaxSize); }
public void setUp(long threshold, String TN, DataBlockEncoding encoding) throws Exception { desc = new HTableDescriptor(TableName.valueOf(TN)); hcd = new HColumnDescriptor(family); hcd.setMobEnabled(true); hcd.setMobThreshold(threshold); hcd.setMaxVersions(4); hcd.setDataBlockEncoding(encoding); desc.addFamily(hcd); admin = TEST_UTIL.getAdmin(); admin.createTable(desc); table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()) .getTable(TableName.valueOf(TN)); }
@Override public int runTestFromCommandLine() throws Exception { IntegrationTestingUtility.setUseDistributedCluster(getConf()); int numPresplits = getConf().getInt("loadmapper.numPresplits", 5); // create HTableDescriptor for specified table HTableDescriptor htd = new HTableDescriptor(getTablename()); htd.addFamily(new HColumnDescriptor(TEST_FAMILY)); try (Connection conn = ConnectionFactory.createConnection(getConf()); Admin admin = conn.getAdmin()) { admin.createTable(htd, Bytes.toBytes(0L), Bytes.toBytes(-1L), numPresplits); } doLoad(getConf(), htd); doVerify(getConf(), htd); getTestingUtil(getConf()).deleteTable(getTablename()); return 0; }