private void connectToTable() { if (this.conf == null) { this.conf = HBaseConfiguration.create(); } try { Connection conn = ConnectionFactory.createConnection(conf); super.table = (HTable) conn.getTable(TableName.valueOf(tableName)); } catch (TableNotFoundException tnfe) { LOG.error("The table " + tableName + " not found ", tnfe); throw new RuntimeException("HBase table '" + tableName + "' not found.", tnfe); } catch (IOException ioe) { LOG.error("Exception while creating connection to HBase.", ioe); throw new RuntimeException("Cannot create connection to HBase.", ioe); } }
/** * Instantiate a TableRecordWriter with a BufferedMutator for batch writing. */ public TableRecordWriter(JobConf job) throws IOException { // expecting exactly one path TableName tableName = TableName.valueOf(job.get(OUTPUT_TABLE)); try { this.conn = ConnectionFactory.createConnection(job); this.m_mutator = conn.getBufferedMutator(tableName); } finally { if (this.m_mutator == null) { conn.close(); conn = null; } } }
@Override protected void initialize(JobContext context) throws IOException { // Do we have to worry about mis-matches between the Configuration from setConf and the one // in this context? TableName tableName = TableName.valueOf(conf.get(INPUT_TABLE)); try { initializeTable(ConnectionFactory.createConnection(new Configuration(conf)), tableName); } catch (Exception e) { LOG.error(StringUtils.stringifyException(e)); } }
@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()); } } }
/** * Trivial test to verify that nobody messes with * {@link ConnectionFactory#createConnection(Configuration)} */ @Test public void testCreateConnection() throws Exception { Configuration configuration = TEST_UTIL.getConfiguration(); Connection c1 = ConnectionFactory.createConnection(configuration); Connection c2 = ConnectionFactory.createConnection(configuration); // created from the same configuration, yet they are different assertTrue(c1 != c2); assertTrue(c1.getConfiguration() == c2.getConfiguration()); }
@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); } }
@Override public Object run() throws Exception { Put p = new Put(Bytes.toBytes("a")); p.addColumn(family2, qualifier, Bytes.toBytes("v2")); try (Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(tableName)) { t.put(p); } return null; } };
@Override public Object run() throws Exception { Delete d = new Delete(TEST_ROW); d.addFamily(TEST_FAMILY); try(Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TEST_TABLE)) { t.delete(d); } return null; } };
@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); } }
@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 Object run() throws Exception { Delete d = new Delete(TEST_ROW); d.addFamily(TEST_FAMILY); 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")).thenDelete(d); } return null; } };
@Override public Object run() throws Exception { // force a new RS connection conf.set("testkey", TEST_UTIL.getRandomUUID().toString()); Connection connection = ConnectionFactory.createConnection(conf); Table t = connection.getTable(TEST_TABLE.getTableName()); try { Scan scan = new Scan().addFamily(TEST_FAMILY2); Result result = t.getScanner(scan).next(); if (result != null) { return result.listCells(); } return null; } finally { t.close(); connection.close(); } } }, USER_OTHER);
@Override public Void run() throws Exception { try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); Table t = conn.getTable(TEST_TABLE.getTableName())) { t.get(new Get(TEST_ROW).addFamily(TEST_FAMILY)); } return null; } };
@Before public void before() throws IOException { this.connection = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); this.tableNameCounter = 0; }
@Override public Object run() throws Exception { Connection unmanagedConnection = ConnectionFactory.createConnection(UTIL.getConfiguration()); Admin admin = unmanagedConnection.getAdmin(); try { return Arrays.asList(admin.listNamespaceDescriptors()); } finally { admin.close(); unmanagedConnection.close(); } } };
@Override public Void run() throws Exception { try (Connection connection = ConnectionFactory.createConnection(conf)) { try (Table t = connection.getTable(TEST_TABLE.getTableName())) { Delete d = new Delete(row); d.addColumn(TEST_FAMILY1, q1, 120); t.checkAndMutate(row, TEST_FAMILY1).qualifier(q1).ifEquals(value).thenDelete(d); } } return null; } });
@Before public void setUp() throws Exception { utility = new HBaseTestingUtility(conf); utility.startMiniCluster(2); conn = ConnectionFactory.createConnection(utility.getConfiguration()); admin = conn.getAdmin(); String methodName = testName.getMethodName(); tableName = TableName.valueOf(methodName.substring(0, methodName.length() - 3)); }
private void addHBaseDelegationToken(Configuration conf) throws IOException, MetaException { if (User.isHBaseSecurityEnabled(conf)) { Connection connection = ConnectionFactory.createConnection(hbaseConf); try { User curUser = User.getCurrent(); Job job = new Job(conf); TokenUtil.addTokenForJob(connection, curUser, job); } catch (InterruptedException e) { throw new IOException("Error while obtaining hbase delegation token", e); } finally { if (connection != null) { connection.close(); } } } }
@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)); } } } }
@Test public void testRegionServerObserver() throws IOException { try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration()); Admin admin = connection.getAdmin()) { LOG.info("Rolling WALs"); admin.rollWALWriter(UTIL.getMiniHBaseCluster().getServerHoldingMeta()); } // Find out the MetricRegistry used by the CP using the global registries MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForRSCoprocessor( CustomRegionServerObserver.class.getName()); Optional<MetricRegistry> registry = MetricRegistries.global().get(info); assertTrue(registry.isPresent()); Optional<Metric> metric = registry.get().get("rollWALRequests"); assertTrue(metric.isPresent()); Counter rollWalRequests = (Counter)metric.get(); assertEquals(1, rollWalRequests.getCount()); }