Apache Phoenix Core library providing SQL-on-HBase functionality with JDBC connectivity, query compilation, and transaction support
Phoenix provides a comprehensive schema management system with strong typing, metadata caching, and full support for tables, columns, indexes, and other database objects. The metadata system enables efficient query compilation and execution planning.
import org.apache.phoenix.schema.*;
import org.apache.phoenix.schema.types.*;
import org.apache.phoenix.exception.*;
import java.sql.*;Core interface representing Phoenix table metadata with complete schema information.
public interface PTable extends PMetaDataEntity {
// Basic table information
PName getSchemaName()
PName getTableName()
PName getName() // Full qualified name
PTableType getType()
long getTimeStamp()
long getSequenceNumber()
// Column operations
List<PColumn> getColumns()
PColumn getColumnForColumnName(String name) throws ColumnNotFoundException, AmbiguousColumnException
List<PColumn> getPKColumns()
int getBaseColumnCount()
PColumn getColumnForColumnQualifier(byte[] cq)
// Index information
List<PTable> getIndexes()
PIndexState getIndexState()
PName getParentName()
PName getParentTableName()
// Table properties
boolean isTransactional()
boolean isImmutableRows()
Integer getSaltBuckets()
String getDefaultFamilyName()
boolean isWALDisabled()
boolean isMultiTenant()
boolean hasViewModifiedUpdateCacheFreq()
Integer getBucketNum()
// View and inheritance
ViewType getViewType()
String getViewStatement()
List<PName> getPhysicalNames()
// Inner enums
enum ViewType { READ_ONLY, UPDATABLE }
enum IndexType { GLOBAL, LOCAL }
enum LinkType { PHYSICAL_TABLE, PARENT_TABLE, EXCLUDED_COLUMN, CHILD_TABLE }
}Enumeration of Phoenix table types with correct values.
public enum PTableType {
SYSTEM('s'), // System tables (metadata tables)
TABLE('t'), // Regular Phoenix/HBase tables
VIEW('v'), // Phoenix views
INDEX('i'), // Phoenix indexes
PROJECTED('p'), // Projected tables
SUBQUERY('u'); // Subquery tables
public String getSerializedValue()
public static PTableType fromSerializedValue(String serializedValue)
}Enumeration of Phoenix index states with correct values.
public enum PIndexState {
BUILDING("b"), // Index is being built
USABLE("o"), // Index is usable for queries
UNUSABLE("x"), // Index is unusable
ACTIVE("a"), // Index is active and maintained
INACTIVE("i"), // Index is inactive
DISABLE("d"), // Index is disabled
REBUILD("r"), // Index needs rebuild
PENDING_ACTIVE("j"), // Index is pending activation
PENDING_DISABLE("k"), // Index is pending disable
CREATE_DISABLE("c"); // Index created in disabled state
public String getSerializedValue()
public static PIndexState fromSerializedValue(String serializedValue)
}Usage:
PhoenixConnection connection = getPhoenixConnection();
PTable table = connection.getTable("users");
// Basic table info
PName schemaName = table.getSchemaName();
String fullTableName = table.getName().getString();
PTableType tableType = table.getType();
// Column information
List<PColumn> allColumns = table.getColumns();
try {
PColumn nameColumn = table.getColumnForColumnName("name");
System.out.println("Found column: " + nameColumn.getName().getString());
} catch (ColumnNotFoundException e) {
System.out.println("Column not found");
} catch (AmbiguousColumnException e) {
System.out.println("Ambiguous column reference");
}
List<PColumn> pkColumns = table.getPKColumns();
// Index information
List<PTable> indexes = table.getIndexes();
boolean hasIndexes = !indexes.isEmpty();
// Check table type
if (table.getType() == PTableType.INDEX) {
PName parentName = table.getParentName();
System.out.println("Index on table: " + parentName);
}
// Check index state
if (table.getType() == PTableType.INDEX) {
PIndexState state = table.getIndexState();
if (state == PIndexState.ACTIVE) {
System.out.println("Index is active and can be used");
}
}Interface representing Phoenix column metadata with data type and constraint information.
public interface PColumn extends PDatum {
// Basic column information
PName getName()
PName getFamilyName()
int getPosition()
Integer getArraySize()
// Column properties
SortOrder getSortOrder()
boolean isRowTimestamp()
boolean isDynamic()
byte[] getViewConstant()
long getTimestamp()
boolean isDerived()
boolean isExcluded()
byte[] getColumnQualifierBytes()
String getExpressionStr()
}Interface representing Phoenix column family metadata.
public interface PColumnFamily extends PMetaDataEntity {
PName getName()
List<PColumn> getColumns()
PColumn getColumnForColumnName(String name) throws ColumnNotFoundException
int getPosition()
}Usage:
// Column information
PColumn column = table.getColumnForColumnName("email");
PName columnName = column.getName();
PDataType dataType = column.getDataType();
boolean isNullable = column.isNullable();
SortOrder sortOrder = column.getSortOrder();
// Check if column is in primary key
boolean isPK = table.getPKColumns().contains(column);
// Get column family (for multi-family tables)
PName familyName = column.getFamilyName();
if (familyName != null) {
System.out.println("Column family: " + familyName.getString());
}Interface for Phoenix metadata cache with correct method signatures.
public interface PMetaData extends MetaDataMutated, Iterable<PTable> {
// Table retrieval
PTable getTableRef(PTableKey key) throws TableNotFoundException
PSchema getSchema(PTableKey key) throws SchemaNotFoundException
// Table operations
PMetaData addTable(PTable table, long resolvedTimestamp) throws SQLException
PMetaData removeTable(PName tenantId, String schemaName, String tableName, String parentTableName, long tableTimeStamp) throws SQLException
PMetaData addColumn(PName tenantId, String tableName, List<PColumn> columns, long tableTimeStamp,
long tableSeqNum, boolean isImmutableRows, boolean isWALDisabled, boolean isMultitenant,
boolean storeNulls, boolean isTransactional, long updateCacheFrequency, long resolvedTime) throws SQLException
PMetaData removeColumn(PName tenantId, String tableName, List<PColumn> columnsToRemove, long tableTimeStamp,
long tableSeqNum, long resolvedTime) throws SQLException
// Schema operations
PMetaData addSchema(PSchema schema) throws SQLException
PMetaData removeSchema(PSchema schema, long schemaTimeStamp) throws SQLException
// Utility methods
int size()
PMetaData clone()
long getAge(PTableRef ref)
}Class representing Phoenix schema information from the parse package.
public class PSchema implements PMetaDataEntity {
public String getSchemaName()
public long getTimeStamp()
public long getEstimatedSize()
}Base interface for all metadata entities.
public interface PMetaDataEntity {
long getEstimatedSize()
}Usage:
PhoenixConnection connection = getPhoenixConnection();
PMetaData metaData = connection.getMetaDataCache();
// Access table by key
PTableKey tableKey = new PTableKey(connection.getTenantId(), "users");
try {
PTable table = metaData.getTableRef(tableKey).getTable();
System.out.println("Found table: " + table.getName().getString());
} catch (TableNotFoundException e) {
System.out.println("Table not found in cache");
}
// Iterate over all cached tables
for (PTable table : metaData) {
System.out.println("Cached table: " + table.getName().getString());
}
// Check cache size and age
int cacheSize = metaData.size();
System.out.println("Metadata cache contains " + cacheSize + " tables");PhoenixConnection connection = DriverManager.getConnection("jdbc:phoenix:localhost")
.unwrap(PhoenixConnection.class);
// Get table metadata
PTable usersTable = connection.getTable("users");
// Examine table properties
System.out.println("Table: " + usersTable.getName().getString());
System.out.println("Type: " + usersTable.getType());
System.out.println("Columns: " + usersTable.getColumns().size());
System.out.println("PK Columns: " + usersTable.getPKColumns().size());
// Access specific columns
for (PColumn column : usersTable.getColumns()) {
System.out.printf("Column: %s, Type: %s, Nullable: %s%n",
column.getName().getString(),
column.getDataType(),
column.isNullable());
}PTable table = connection.getTable("orders");
List<PColumn> pkColumns = table.getPKColumns();
System.out.println("Primary key columns:");
for (PColumn pkColumn : pkColumns) {
System.out.printf(" %s (%s) - Position: %d, Sort: %s%n",
pkColumn.getName().getString(),
pkColumn.getDataType().toString(),
pkColumn.getPosition(),
pkColumn.getSortOrder());
}PTable baseTable = connection.getTable("users");
List<PTable> indexes = baseTable.getIndexes();
System.out.println("Indexes on " + baseTable.getName().getString() + ":");
for (PTable index : indexes) {
System.out.printf(" Index: %s, State: %s, Type: %s%n",
index.getName().getString(),
index.getIndexState(),
index.getType());
// Show indexed columns
List<PColumn> indexColumns = index.getColumns();
for (PColumn col : indexColumns) {
if (!col.getName().getString().startsWith("0:")) { // Skip row key columns
System.out.println(" Column: " + col.getName().getString());
}
}
}PhoenixConnection connection = getConnection();
PMetaData originalCache = connection.getMetaDataCache();
// Force table metadata refresh
connection.getQueryServices().clearCache();
// Compare cache before and after
PMetaData refreshedCache = connection.getMetaDataCache();
System.out.println("Original cache size: " + originalCache.size());
System.out.println("Refreshed cache size: " + refreshedCache.size());Install with Tessl CLI
npx tessl i tessl/maven-org-apache-phoenix--phoenix-core