org.apache.hadoop.hbase.client.HTableInterface Java Examples

The following examples show how to use org.apache.hadoop.hbase.client.HTableInterface. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ObserverEnabler.java    From Kylin with Apache License 2.0 6 votes vote down vote up
public static ResultScanner scanWithCoprocessorIfBeneficial(CubeSegment segment, Cuboid cuboid, TupleFilter tupleFiler, //
        Collection<TblColRef> groupBy, Collection<RowValueDecoder> rowValueDecoders, StorageContext context, HTableInterface table, Scan scan) throws IOException {

    if (context.isCoprocessorEnabled() == false) {
        return table.getScanner(scan);
    }

    CoprocessorRowType type = CoprocessorRowType.fromCuboid(segment, cuboid);
    CoprocessorFilter filter = CoprocessorFilter.fromFilter(segment, tupleFiler);
    CoprocessorProjector projector = CoprocessorProjector.makeForObserver(segment, cuboid, groupBy);
    ObserverAggregators aggrs = ObserverAggregators.fromValueDecoders(rowValueDecoders);

    if (DEBUG_LOCAL_COPROCESSOR) {
        RegionScanner innerScanner = new RegionScannerAdapter(table.getScanner(scan));
        AggregationScanner aggrScanner = new AggregationScanner(type, filter, projector, aggrs, innerScanner);
        return new ResultScannerAdapter(aggrScanner);
    } else {
        scan.setAttribute(AggregateRegionObserver.COPROCESSOR_ENABLE, new byte[] { 0x01 });
        scan.setAttribute(AggregateRegionObserver.TYPE, CoprocessorRowType.serialize(type));
        scan.setAttribute(AggregateRegionObserver.PROJECTOR, CoprocessorProjector.serialize(projector));
        scan.setAttribute(AggregateRegionObserver.AGGREGATORS, ObserverAggregators.serialize(aggrs));
        scan.setAttribute(AggregateRegionObserver.FILTER, CoprocessorFilter.serialize(filter));
        return table.getScanner(scan);
    }
}
 
Example #2
Source File: PhoenixRuntimeIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static void assertTenantIds(Expression e, HTableInterface htable, Filter filter, String[] tenantIds) throws IOException {
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    Scan scan = new Scan();
    scan.setFilter(filter);
    ResultScanner scanner = htable.getScanner(scan);
    Result result = null;
    ResultTuple tuple = new ResultTuple();
    List<String> actualTenantIds = Lists.newArrayListWithExpectedSize(tenantIds.length);
    List<String> expectedTenantIds = Arrays.asList(tenantIds);
    while ((result = scanner.next()) != null) {
        tuple.setResult(result);
        e.evaluate(tuple, ptr);
        String tenantId = (String)PVarchar.INSTANCE.toObject(ptr);
        actualTenantIds.add(tenantId == null ? "" : tenantId);
    }
    // Need to sort because of salting
    Collections.sort(actualTenantIds);
    assertEquals(expectedTenantIds, actualTenantIds);
}
 
Example #3
Source File: HBaseTransactionPruningPlugin.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(Configuration conf) throws IOException {
  this.conf = conf;
  this.hBaseAdmin = new HBaseAdmin(conf);
  this.connection = HConnectionManager.createConnection(conf);

  final TableName stateTable = TableName.valueOf(conf.get(TxConstants.TransactionPruning.PRUNE_STATE_TABLE,
                                                          TxConstants.TransactionPruning.DEFAULT_PRUNE_STATE_TABLE));
  LOG.info("Initializing plugin with state table {}:{}", stateTable.getNamespaceAsString(),
           stateTable.getNameAsString());
  createPruneTable(stateTable);
  this.dataJanitorState = new DataJanitorState(new DataJanitorState.TableSupplier() {
    @Override
    public HTableInterface get() throws IOException {
      return connection.getTable(stateTable);
    }
  });
}
 
Example #4
Source File: MappingTableDataTypeIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testMappingHbaseTableToPhoenixTable() throws Exception {
    final TableName tableName = TableName.valueOf("MTEST");
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    PhoenixConnection conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
    
    HBaseAdmin admin = conn.getQueryServices().getAdmin();
    try {
        // Create table then get the single region for our new table.
        HTableDescriptor descriptor = new HTableDescriptor(tableName);
        HColumnDescriptor columnDescriptor =  new HColumnDescriptor(Bytes.toBytes("cf"));
        descriptor.addFamily(columnDescriptor);
        admin.createTable(descriptor);
        HTableInterface t = conn.getQueryServices().getTable(Bytes.toBytes("MTEST"));
        insertData(tableName.getName(), admin, t);
        t.close();
        try {
            testCreateTableMismatchedType();
            fail();
        } catch (SQLException e) {
            assertEquals(SQLExceptionCode.ILLEGAL_DATA.getErrorCode(),e.getErrorCode());
        }
    } finally {
        admin.close();
    }
}
 
Example #5
Source File: AbstractHBaseClient.java    From jstorm with Apache License 2.0 6 votes vote down vote up
protected KVSerializable getRow(String tableName, Class clazz, byte[] key) {
    HTableInterface table = getHTableInterface(tableName);
    Get get = new Get(key);

    HTableInterface htable;
    try {
        htable = getHTableInterface(tableName);
        KVSerializable kvInst = (KVSerializable) clazz.getConstructors()[0].newInstance();
        Result result = htable.get(get);
        if (result != null) {
            kvInst.fromKV(key, result.getValue(CF, V_DATA));
            return kvInst;
        }
    } catch (Exception ex) {
        logger.error("Scan metric meta error, class:{}", clazz.getSimpleName(), ex);
    } finally {
        closeTable(table);
    }
    return null;
}
 
Example #6
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
/**
 * Delete prune upper bounds for the regions that are not in the given exclude set, and the
 * prune upper bound is less than the given value.
 * After the invalid list is pruned up to deletionPruneUpperBound, we do not need entries for regions that have
 * prune upper bound less than deletionPruneUpperBound. We however limit the deletion to only regions that are
 * no longer in existence (due to deletion, etc.), to avoid update/delete race conditions.
 *
 * @param deletionPruneUpperBound prune upper bound below which regions will be deleted
 * @param excludeRegions set of regions that should not be deleted
 * @throws IOException when not able to delete data in HBase
 */
public void deletePruneUpperBounds(long deletionPruneUpperBound, SortedSet<byte[]> excludeRegions)
  throws IOException {
  try (HTableInterface stateTable = stateTableSupplier.get()) {
    byte[] startRow = makeRegionKey(EMPTY_BYTE_ARRAY);
    Scan scan = new Scan(startRow, REGION_KEY_PREFIX_STOP);
    scan.addColumn(FAMILY, PRUNE_UPPER_BOUND_COL);

    try (ResultScanner scanner = stateTable.getScanner(scan)) {
      Result next;
      while ((next = scanner.next()) != null) {
        byte[] region = getRegionFromKey(next.getRow());
        if (!excludeRegions.contains(region)) {
          byte[] timeBytes = next.getValue(FAMILY, PRUNE_UPPER_BOUND_COL);
          if (timeBytes != null) {
            long pruneUpperBoundRegion = Bytes.toLong(timeBytes);
            if (pruneUpperBoundRegion < deletionPruneUpperBound) {
              stateTable.delete(new Delete(next.getRow()));
            }
          }
        }
      }
    }
  }
}
 
Example #7
Source File: HbaseServiceImpl.java    From searchanalytics-bigdata with MIT License 6 votes vote down vote up
@Override
public void insertEventData(final byte[] body) {
	LOG.debug("Inserting searchclicks table row content event!");
	hbaseTemplate.execute("searchclicks", new TableCallback<Object>() {

		@Override
		public Object doInTable(HTableInterface table) throws Throwable {
			String rowId = UUID.randomUUID().toString();
			Put p = new Put(Bytes.toBytes(rowId));
			LOG.debug("Inserting searchclicks table row id: {}", rowId);
			p.add(HbaseJsonEventSerializer.COLUMFAMILY_CLIENT_BYTES, Bytes.toBytes("eventid"), body);
			table.put(p);
			table.close();
			return null;
		}
	});
	LOG.debug("Inserting searchclicks table row content event done!");
}
 
Example #8
Source File: HBaseResourceStore.java    From Kylin with Apache License 2.0 6 votes vote down vote up
private Path writeLargeCellToHdfs(String resPath, byte[] largeColumn, HTableInterface table) throws IOException {
    Path redirectPath = bigCellHDFSPath(resPath);
    Configuration hconf = HadoopUtil.getCurrentConfiguration();
    FileSystem fileSystem = FileSystem.get(hconf);

    if (fileSystem.exists(redirectPath)) {
        fileSystem.delete(redirectPath, true);
    }

    FSDataOutputStream out = fileSystem.create(redirectPath);

    try {
        out.write(largeColumn);
    } finally {
        IOUtils.closeQuietly(out);
    }

    return redirectPath;
}
 
Example #9
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
/**
 * Return the set of regions saved for the time at or before the given time. This method finds the greatest time
 * that is less than or equal to the given time, and then returns all regions with that exact time, but none that are
 * older than that.
 *
 * @param time timestamp in milliseconds
 * @return set of regions and time at which they were recorded, or null if no regions found
 * @throws IOException when not able to read the data from HBase
 */
@Nullable
public TimeRegions getRegionsOnOrBeforeTime(long time) throws IOException {
  try (HTableInterface stateTable = stateTableSupplier.get()) {
    TimeRegions timeRegions;
    while ((timeRegions = getNextSetOfTimeRegions(stateTable, time)) != null) {
      int count = getRegionCountForTime(stateTable, timeRegions.getTime());
      if (count != -1 && count == timeRegions.getRegions().size()) {
        return timeRegions;
      } else {
        LOG.warn(String.format("Got incorrect count for regions saved at time %s, expected = %s but actual = %s",
                               timeRegions.getTime(), count, timeRegions.getRegions().size()));
        time = timeRegions.getTime() - 1;
      }
    }
    return null;
  }
}
 
Example #10
Source File: TransactionAwareHTableTest.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
private void verifyRows(HTableInterface table, Get get, List<byte[]> expectedValues) throws Exception {
  Result result = table.get(get);
  if (expectedValues == null) {
    assertTrue(result.isEmpty());
  } else {
    assertFalse(result.isEmpty());
    byte[] family = TestBytes.family;
    byte[] col = TestBytes.qualifier;
    if (get.hasFamilies()) {
      family = get.getFamilyMap().keySet().iterator().next();
      col = get.getFamilyMap().get(family).first();
    }
    Iterator<Cell> it = result.getColumnCells(family, col).iterator();
    for (byte[] expectedValue : expectedValues) {
      Assert.assertTrue(it.hasNext());
      assertArrayEquals(expectedValue, CellUtil.cloneValue(it.next()));
    }
  }
}
 
Example #11
Source File: UserService.java    From Kylin with Apache License 2.0 6 votes vote down vote up
@Override
public void updateUser(UserDetails user) {
    HTableInterface htable = null;
    try {
        byte[] userAuthorities = serialize(user.getAuthorities());
        htable = HBaseConnection.get(hbaseUrl).getTable(userTableName);
        Put put = new Put(Bytes.toBytes(user.getUsername()));
        put.add(Bytes.toBytes(USER_AUTHORITY_FAMILY), Bytes.toBytes(USER_AUTHORITY_COLUMN), userAuthorities);

        htable.put(put);
        htable.flushCommits();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(htable);
    }
}
 
Example #12
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a list of {@link RegionPruneInfo} for given regions. Returns all regions if the given regions set is null.
 *
 * @param regions a set of regions
 * @return list of {@link RegionPruneInfo}s.
 * @throws IOException when not able to read the data from HBase
 */
public List<RegionPruneInfo> getPruneInfoForRegions(@Nullable SortedSet<byte[]> regions) throws IOException {
  List<RegionPruneInfo> regionPruneInfos = new ArrayList<>();
  try (HTableInterface stateTable = stateTableSupplier.get()) {
    byte[] startRow = makeRegionKey(EMPTY_BYTE_ARRAY);
    Scan scan = new Scan(startRow, REGION_KEY_PREFIX_STOP);
    scan.addColumn(FAMILY, PRUNE_UPPER_BOUND_COL);

    try (ResultScanner scanner = stateTable.getScanner(scan)) {
      Result next;
      while ((next = scanner.next()) != null) {
        byte[] region = getRegionFromKey(next.getRow());
        if (regions == null || regions.contains(region)) {
          Cell cell = next.getColumnLatestCell(FAMILY, PRUNE_UPPER_BOUND_COL);
          if (cell != null) {
            byte[] pruneUpperBoundBytes = CellUtil.cloneValue(cell);
            long timestamp = cell.getTimestamp();
            regionPruneInfos.add(new RegionPruneInfo(region, Bytes.toStringBinary(region),
                                                     Bytes.toLong(pruneUpperBoundBytes), timestamp));
          }
        }
      }
    }
  }
  return Collections.unmodifiableList(regionPruneInfos);
}
 
Example #13
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
/**
 * Delete prune upper bounds for the regions that are not in the given exclude set, and the
 * prune upper bound is less than the given value.
 * After the invalid list is pruned up to deletionPruneUpperBound, we do not need entries for regions that have
 * prune upper bound less than deletionPruneUpperBound. We however limit the deletion to only regions that are
 * no longer in existence (due to deletion, etc.), to avoid update/delete race conditions.
 *
 * @param deletionPruneUpperBound prune upper bound below which regions will be deleted
 * @param excludeRegions set of regions that should not be deleted
 * @throws IOException when not able to delete data in HBase
 */
public void deletePruneUpperBounds(long deletionPruneUpperBound, SortedSet<byte[]> excludeRegions)
  throws IOException {
  try (HTableInterface stateTable = stateTableSupplier.get()) {
    byte[] startRow = makeRegionKey(EMPTY_BYTE_ARRAY);
    Scan scan = new Scan(startRow, REGION_KEY_PREFIX_STOP);
    scan.addColumn(FAMILY, PRUNE_UPPER_BOUND_COL);

    try (ResultScanner scanner = stateTable.getScanner(scan)) {
      Result next;
      while ((next = scanner.next()) != null) {
        byte[] region = getRegionFromKey(next.getRow());
        if (!excludeRegions.contains(region)) {
          byte[] timeBytes = next.getValue(FAMILY, PRUNE_UPPER_BOUND_COL);
          if (timeBytes != null) {
            long pruneUpperBoundRegion = Bytes.toLong(timeBytes);
            if (pruneUpperBoundRegion < deletionPruneUpperBound) {
              stateTable.delete(new Delete(next.getRow()));
            }
          }
        }
      }
    }
  }
}
 
Example #14
Source File: HBaseResourceStore.java    From Kylin with Apache License 2.0 6 votes vote down vote up
@Override
protected void putResourceImpl(String resPath, InputStream content, long ts) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtils.copy(content, bout);
    bout.close();

    HTableInterface table = getConnection().getTable(getAllInOneTableName());
    try {
        byte[] row = Bytes.toBytes(resPath);
        Put put = buildPut(resPath, ts, row, bout.toByteArray(), table);

        table.put(put);
        table.flushCommits();
    } finally {
        IOUtils.closeQuietly(table);
    }
}
 
Example #15
Source File: TestParalleIndexWriter.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCorrectlyCleansUpResources() throws Exception{
  ExecutorService exec = Executors.newFixedThreadPool(1);
  FakeTableFactory factory = new FakeTableFactory(
      Collections.<ImmutableBytesPtr, HTableInterface> emptyMap());
  ParallelWriterIndexCommitter writer = new ParallelWriterIndexCommitter();
  Abortable mockAbort = Mockito.mock(Abortable.class);
  Stoppable mockStop = Mockito.mock(Stoppable.class);
  // create a simple writer
  writer.setup(factory, exec, mockAbort, mockStop, 1);
  // stop the writer
  writer.stop(this.test.getTableNameString() + " finished");
  assertTrue("Factory didn't get shutdown after writer#stop!", factory.shutdown);
  assertTrue("ExectorService isn't terminated after writer#stop!", exec.isShutdown());
  Mockito.verifyZeroInteractions(mockAbort, mockStop);
}
 
Example #16
Source File: TestParalleWriterIndexCommitter.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testCorrectlyCleansUpResources() throws Exception{
  ExecutorService exec = Executors.newFixedThreadPool(1);
  FakeTableFactory factory = new FakeTableFactory(
      Collections.<ImmutableBytesPtr, HTableInterface> emptyMap());
  ParallelWriterIndexCommitter writer = new ParallelWriterIndexCommitter(VersionInfo.getVersion());
  Abortable mockAbort = Mockito.mock(Abortable.class);
  Stoppable mockStop = Mockito.mock(Stoppable.class);
  // create a simple writer
  writer.setup(factory, exec, mockAbort, mockStop, 1);
  // stop the writer
  writer.stop(this.test.getTableNameString() + " finished");
  assertTrue("Factory didn't get shutdown after writer#stop!", factory.shutdown);
  assertTrue("ExectorService isn't terminated after writer#stop!", exec.isShutdown());
  Mockito.verifyZeroInteractions(mockAbort, mockStop);
}
 
Example #17
Source File: ConnectionQueryServicesImpl.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public long createSequence(String tenantId, String schemaName, String sequenceName, long startWith, long incrementBy, int cacheSize, long timestamp) 
        throws SQLException {
    SequenceKey sequenceKey = new SequenceKey(tenantId, schemaName, sequenceName);
    Sequence newSequences = new Sequence(sequenceKey);
    Sequence sequence = sequenceMap.putIfAbsent(sequenceKey, newSequences);
    if (sequence == null) {
        sequence = newSequences;
    }
    try {
        sequence.getLock().lock();
        // Now that we have the lock we need, create the sequence
        Append append = sequence.createSequence(startWith, incrementBy, cacheSize, timestamp);
        HTableInterface htable = this.getTable(PhoenixDatabaseMetaData.SEQUENCE_TABLE_NAME_BYTES);
        try {
            Result result = htable.append(append);
            return sequence.createSequence(result);
        } catch (IOException e) {
            throw ServerUtil.parseServerException(e);
        }
    } finally {
        sequence.getLock().unlock();
    }
}
 
Example #18
Source File: TestParalleIndexWriter.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testCorrectlyCleansUpResources() throws Exception{
  ExecutorService exec = Executors.newFixedThreadPool(1);
  FakeTableFactory factory = new FakeTableFactory(
      Collections.<ImmutableBytesPtr, HTableInterface> emptyMap());
  ParallelWriterIndexCommitter writer = new ParallelWriterIndexCommitter(VersionInfo.getVersion());
  Abortable mockAbort = Mockito.mock(Abortable.class);
  Stoppable mockStop = Mockito.mock(Stoppable.class);
  // create a simple writer
  writer.setup(factory, exec, mockAbort, mockStop, 1);
  // stop the writer
  writer.stop(this.test.getTableNameString() + " finished");
  assertTrue("Factory didn't get shutdown after writer#stop!", factory.shutdown);
  assertTrue("ExectorService isn't terminated after writer#stop!", exec.isShutdown());
  Mockito.verifyZeroInteractions(mockAbort, mockStop);
}
 
Example #19
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
/**
 * Return regions that were recorded as empty after the given time.
 *
 * @param time time in milliseconds
 * @param includeRegions If not null, the returned set will be an intersection of the includeRegions set
 *                       and the empty regions after the given time
 */
public SortedSet<byte[]> getEmptyRegionsAfterTime(long time, @Nullable SortedSet<byte[]> includeRegions)
  throws IOException {
  SortedSet<byte[]> emptyRegions = new TreeSet<>(Bytes.BYTES_COMPARATOR);
  try (HTableInterface stateTable = stateTableSupplier.get()) {
    Scan scan = new Scan(makeEmptyRegionTimeKey(Bytes.toBytes(time + 1), EMPTY_BYTE_ARRAY),
                         EMPTY_REGION_TIME_KEY_PREFIX_STOP);
    scan.addColumn(FAMILY, EMPTY_REGION_TIME_COL);

    try (ResultScanner scanner = stateTable.getScanner(scan)) {
      Result next;
      while ((next = scanner.next()) != null) {
        byte[] emptyRegion = getEmptyRegionFromKey(next.getRow());
        if (includeRegions == null || includeRegions.contains(emptyRegion)) {
          emptyRegions.add(emptyRegion);
        }
      }
    }
  }
  return Collections.unmodifiableSortedSet(emptyRegions);
}
 
Example #20
Source File: TransactionAwareHTableTest.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
private void verifyRows(HTableInterface table, Get get, List<byte[]> expectedValues) throws Exception {
  Result result = table.get(get);
  if (expectedValues == null) {
    assertTrue(result.isEmpty());
  } else {
    assertFalse(result.isEmpty());
    byte[] family = TestBytes.family;
    byte[] col = TestBytes.qualifier;
    if (get.hasFamilies()) {
      family = get.getFamilyMap().keySet().iterator().next();
      col = get.getFamilyMap().get(family).first();
    }
    Iterator<Cell> it = result.getColumnCells(family, col).iterator();
    for (byte[] expectedValue : expectedValues) {
      Assert.assertTrue(it.hasNext());
      assertArrayEquals(expectedValue, CellUtil.cloneValue(it.next()));
    }
  }
}
 
Example #21
Source File: TransactionAwareHTableTest.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
private void verifyRows(HTableInterface table, Get get, List<byte[]> expectedValues) throws Exception {
  Result result = table.get(get);
  if (expectedValues == null) {
    assertTrue(result.isEmpty());
  } else {
    assertFalse(result.isEmpty());
    byte[] family = TestBytes.family;
    byte[] col = TestBytes.qualifier;
    if (get.hasFamilies()) {
      family = get.getFamilyMap().keySet().iterator().next();
      col = get.getFamilyMap().get(family).first();
    }
    Iterator<Cell> it = result.getColumnCells(family, col).iterator();
    for (byte[] expectedValue : expectedValues) {
      Assert.assertTrue(it.hasNext());
      assertArrayEquals(expectedValue, CellUtil.cloneValue(it.next()));
    }
  }
}
 
Example #22
Source File: FromCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static ColumnResolver getResolverForCreation(final CreateTableStatement statement, final PhoenixConnection connection)
        throws SQLException {
    TableName baseTable = statement.getBaseTableName();
    if (baseTable == null) {
        return EMPTY_TABLE_RESOLVER;
    }
    NamedTableNode tableNode = NamedTableNode.create(null, baseTable, Collections.<ColumnDef>emptyList());
    // Always use non-tenant-specific connection here
    try {
        SingleTableColumnResolver visitor = new SingleTableColumnResolver(connection, tableNode, true);
        return visitor;
    } catch (TableNotFoundException e) {
        // Used for mapped VIEW, since we won't be able to resolve that.
        // Instead, we create a table with just the dynamic columns.
        // A tenant-specific connection may not create a mapped VIEW.
        if (connection.getTenantId() == null && statement.getTableType() == PTableType.VIEW) {
            ConnectionQueryServices services = connection.getQueryServices();
            byte[] fullTableName = SchemaUtil.getTableNameAsBytes(baseTable.getSchemaName(), baseTable.getTableName());
            HTableInterface htable = null;
            try {
                htable = services.getTable(fullTableName);
            } catch (UnsupportedOperationException ignore) {
                throw e; // For Connectionless
            } finally {
                if (htable != null) Closeables.closeQuietly(htable);
            }
            tableNode = NamedTableNode.create(null, baseTable, statement.getColumnDefs());
            return new SingleTableColumnResolver(connection, tableNode, e.getTimeStamp());
        }
        throw e;
    }
}
 
Example #23
Source File: HBaseResourceStore.java    From Kylin with Apache License 2.0 5 votes vote down vote up
private Put buildPut(String resPath, long ts, byte[] row, byte[] content, HTableInterface table) throws IOException {
    int kvSizeLimit = this.kylinConfig.getHBaseKeyValueSize();
    if (content.length > kvSizeLimit) {
        writeLargeCellToHdfs(resPath, content, table);
        content = BytesUtil.EMPTY_BYTE_ARRAY;
    }

    Put put = new Put(row);
    put.add(B_FAMILY, B_COLUMN, content);
    put.add(B_FAMILY, B_COLUMN_TS, Bytes.toBytes(ts));

    return put;
}
 
Example #24
Source File: TestUtil.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void clearMetaDataCache(Connection conn) throws Throwable {
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    HTableInterface htable = pconn.getQueryServices().getTable(PhoenixDatabaseMetaData.TYPE_TABLE_NAME_BYTES);
    htable.coprocessorExec(MetaDataProtocol.class, HConstants.EMPTY_START_ROW,
            HConstants.EMPTY_END_ROW, new Batch.Call<MetaDataProtocol, Void>() {
        @Override
        public Void call(MetaDataProtocol instance) throws IOException {
          instance.clearCache();
          return null;
        }
      });
}
 
Example #25
Source File: StatisticsUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static Result readRegionStatistics(HTableInterface statsHTable, byte[] tableNameBytes, ImmutableBytesPtr cf, byte[] regionName, long clientTimeStamp)
        throws IOException {
    byte[] prefix = StatisticsUtil.getRowKey(tableNameBytes, cf, regionName);
    Get get = new Get(prefix);
    get.setTimeRange(MetaDataProtocol.MIN_TABLE_TIMESTAMP, clientTimeStamp);
    get.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.GUIDE_POSTS_WIDTH_BYTES);
    get.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.GUIDE_POSTS_BYTES);
    get.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.GUIDE_POSTS_ROW_COUNT_BYTES);
    return statsHTable.get(get);
}
 
Example #26
Source File: GenericAggregateReader.java    From Eagle with Apache License 2.0 5 votes vote down vote up
@Override
protected void onOpen(HTableInterface tbl, Scan scan) throws IOException {
	this.result = this.aggregateClient.aggregate(
			tbl,
			this.ed,
			scan,
			this.aggregateCondition.getGroupbyFields(),
			this.aggregateCondition.getAggregateFunctionTypes(),
			this.aggregateCondition.getAggregateFields(),
			this.aggregateCondition.isTimeSeries(),
			this.startTime,
			this.endTime,
			this.aggregateCondition.getIntervalMS());
}
 
Example #27
Source File: TestCachingHTableFactory.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCacheCorrectlyExpiresTable() throws Exception {
  // setup the mocks for the tables we will request
  HTableFactory delegate = Mockito.mock(HTableFactory.class);
  ImmutableBytesPtr t1 = new ImmutableBytesPtr(Bytes.toBytes("t1"));
  ImmutableBytesPtr t2 = new ImmutableBytesPtr(Bytes.toBytes("t2"));
  ImmutableBytesPtr t3 = new ImmutableBytesPtr(Bytes.toBytes("t3"));
  HTableInterface table1 = Mockito.mock(HTableInterface.class);
  HTableInterface table2 = Mockito.mock(HTableInterface.class);
  HTableInterface table3 = Mockito.mock(HTableInterface.class);
  Mockito.when(delegate.getTable(t1)).thenReturn(table1);
  Mockito.when(delegate.getTable(t2)).thenReturn(table2);
  Mockito.when(delegate.getTable(t3)).thenReturn(table3);
  
  // setup our factory with a cache size of 2
  CachingHTableFactory factory = new CachingHTableFactory(delegate, 2);
  factory.getTable(t1);
  factory.getTable(t2);
  factory.getTable(t3);
  // get the same table a second time, after it has gone out of cache
  factory.getTable(t1);
  
  Mockito.verify(delegate, Mockito.times(2)).getTable(t1);
  Mockito.verify(delegate, Mockito.times(1)).getTable(t2);
  Mockito.verify(delegate, Mockito.times(1)).getTable(t3);
  Mockito.verify(table1).close();
}
 
Example #28
Source File: HBaseBackedTransactionLogger.java    From hbase-secondary-index with GNU General Public License v3.0 5 votes vote down vote up
public void forgetTransaction(final long transactionId) {
    Delete delete = new Delete(getRow(transactionId));

    HTableInterface table = getTable();
    try {
        table.delete(delete);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        putTable(table);
    }
}
 
Example #29
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
/**
 * Create a transactional aware instance of the passed HTable, with the option
 * of allowing non-transactional operations.
 * @param hTable underlying HBase table to use
 * @param conflictLevel level of conflict detection to perform (defaults to {@code COLUMN})
 * @param allowNonTransactional if true, additional operations (checkAndPut, increment, checkAndDelete)
 *                              will be available, though non-transactional
 */
public TransactionAwareHTable(HTableInterface hTable, TxConstants.ConflictDetection conflictLevel,
                              boolean allowNonTransactional) {
  super(conflictLevel, allowNonTransactional, 
      hTable.getConfiguration().getBoolean(
          TxConstants.TX_PRE_014_CHANGESET_KEY,
          TxConstants.DEFAULT_TX_PRE_014_CHANGESET_KEY));
  this.hTable = hTable;
}
 
Example #30
Source File: InvalidListPruneTest.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startMiniCluster() throws Exception {
  // Setup the configuration to start HBase cluster with the invalid list pruning enabled
  conf = HBaseConfiguration.create();
  conf.setBoolean(TxConstants.TransactionPruning.PRUNE_ENABLE, true);
  // Flush prune data to table quickly, so that tests don't need have to wait long to see updates
  conf.setLong(TxConstants.TransactionPruning.PRUNE_FLUSH_INTERVAL, 0L);
  AbstractHBaseTableTest.startMiniCluster();

  TransactionStateStorage txStateStorage = new InMemoryTransactionStateStorage();
  TransactionManager txManager = new TransactionManager(conf, txStateStorage, new TxMetricsCollector());
  txManager.startAndWait();

  // Do some transactional data operations
  txDataTable1 = TableName.valueOf("invalidListPruneTestTable1");
  HTable hTable = createTable(txDataTable1.getName(), new byte[][]{family}, false,
                              Collections.singletonList(TestTransactionProcessor.class.getName()));
  try (TransactionAwareHTable txTable = new TransactionAwareHTable(hTable, TxConstants.ConflictDetection.ROW)) {
    TransactionContext txContext = new TransactionContext(new InMemoryTxSystemClient(txManager), txTable);
    txContext.start();
    for (int i = 0; i < MAX_ROWS; ++i) {
      txTable.put(new Put(Bytes.toBytes(i)).add(family, qualifier, Bytes.toBytes(i)));
    }
    txContext.finish();
  }

  testUtil.flush(txDataTable1);
  txManager.stopAndWait();

  pruneStateTable = TableName.valueOf(conf.get(TxConstants.TransactionPruning.PRUNE_STATE_TABLE,
                                               TxConstants.TransactionPruning.DEFAULT_PRUNE_STATE_TABLE));
  connection = HConnectionManager.createConnection(conf);
  dataJanitorState =
    new DataJanitorState(new DataJanitorState.TableSupplier() {
      @Override
      public HTableInterface get() throws IOException {
        return connection.getTable(pruneStateTable);
      }
    });
}