/** * Get list of ColumnInfos that contain Column Name and its associated PDataType for an import. * The supplied list of columns can be null -- if it is non-null, it represents a user-supplied * list of columns to be imported. * * @param conn Phoenix connection from which metadata will be read * @param tableName Phoenix table name whose columns are to be checked. Can include a schema name * @param columns user-supplied list of import columns, can be null */ public static List<ColumnInfo> generateColumnInfo( Connection conn, String tableName, List<String> columns) throws SQLException { PTable table = PhoenixRuntime.getTable(conn, tableName); List<ColumnInfo> columnInfoList = Lists.newArrayList(); Set<String> unresolvedColumnNames = new TreeSet<String>(); if (columns == null) { // use all columns in the table for (PColumn pColumn : table.getColumns()) { int sqlType = pColumn.getDataType().getResultSetSqlType(); columnInfoList.add(new ColumnInfo(pColumn.toString(), sqlType)); } } else { // Leave "null" as indication to skip b/c it doesn't exist for (int i = 0; i < columns.size(); i++) { String columnName = columns.get(i); try { ColumnInfo columnInfo = PhoenixRuntime.getColumnInfo(table, columnName); columnInfoList.add(columnInfo); } catch (ColumnNotFoundException cnfe) { unresolvedColumnNames.add(columnName.trim()); } catch (AmbiguousColumnException ace) { unresolvedColumnNames.add(columnName.trim()); } } } // if there exists columns that cannot be resolved, error out. if (unresolvedColumnNames.size() > 0) { StringBuilder exceptionMessage = new StringBuilder(); boolean first = true; exceptionMessage.append("Unable to resolve these column names:\n"); for (String col : unresolvedColumnNames) { if (first) first = false; else exceptionMessage.append(","); exceptionMessage.append(col); } exceptionMessage.append("\nAvailable columns with column families:\n"); first = true; for (PColumn pColumn : table.getColumns()) { if (first) first = false; else exceptionMessage.append(","); exceptionMessage.append(pColumn.toString()); } throw new SQLException(exceptionMessage.toString()); } return columnInfoList; }
/** * Constructs a column info for the supplied pColumn * * @param pColumn * @return columnInfo * @throws SQLException if the parameter is null. */ public static ColumnInfo getColumnInfo(PColumn pColumn) throws SQLException { if (pColumn == null) { throw new SQLException("pColumn must not be null."); } int sqlType = pColumn.getDataType().getResultSetSqlType(); ColumnInfo columnInfo = new ColumnInfo(pColumn.toString(), sqlType); return columnInfo; }