编程实现以下功能
createTable(String tableName, String[] fields)
创建表,参数tableName为表的名称,字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
addRecord(String tableName, String row, String[] fields, String[] values)
向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer
Science”、“English”三列添加成绩时,字符串数组fields为{“Score:Math”, ”Score:Computer Science”,
”Score:English”},数组values存储这三门课的成绩。
scanColumn(StringtableName, String column)
浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
modifyData(String tableName, String row, String column)
修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。
deleteRow(String tableName,String row)
删除表tableName中row指定的行的记录。
public class hbaseexp {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void listName() throws IOException{
init();
HTableDescriptor[] tables = admin.listTables();
for(HTableDescriptor s:tables){
System.out.println(s.getNameAsString());
}
close();
}
public static void scanAll(String tableName) throws IOException{
init();
TableName table = TableName.valueOf(tableName);
if(!admin.tableExists(table)){
System.out.println("table is not exists");
}else{
Table t = connection.getTable(table);
Scan scan = new Scan();
ResultScanner rs = t.getScanner(scan);
for(Result s :rs){
for(Cell cell:s.rawCells()){
System.out.print("row name:"+new String(CellUtil.cloneRow(cell)));
System.out.print("column family :" +new String(CellUtil.cloneFamily(cell)));
System.out.print("column :"+new String(CellUtil.cloneQualifier(cell)));
System.out.print("value :"+new String(CellUtil.cloneValue(cell)));
System.out.print("timestamp :"+cell.getTimestamp());
System.out.println();
}
}
}
close();
}
public static void dropTable(String tableName) throws IOException{
init();
TableName tablename = TableName.valueOf(tableName);
if(!admin.tableExists(tablename)){
System.out.println("tale is not exists");
}else{
//disable drop
admin.disableTable(tablename);
admin.truncateTable(tablename, false);
System.out.println("all data is delete");
}
close();
}
public static void countRows(String tableName) throws IOException{
//key-value number? row number?
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
int num = 0;
for(Result s = scanner.next(); s!=null;s = scanner.next())
num++;
System.out.println("numbers of elements is : " + num);
close();
}
public static void createTable(String tableName, String[] fields) throws IOException {
init();
TableName tablename = TableName.valueOf(tableName);
if (admin.tableExists(tablename)) {
System.out.println("table is exists!");
admin.disableTable(tablename);
admin.deleteTable(tablename);
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);
for (String str : fields) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
close();
}
public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
for (int i = 0; i != fields.length; i++) {
Put put = new Put(row.getBytes());
String[] cols = fields[i].split(":");
put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
table.put(put);
}
table.close();
close();
}
public static void showCell(Result result) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RowName is :" + new String(CellUtil.cloneRow(cell)) + " ");
System.out.println("column Family is :" + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.println("row Name is :" + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.println("value is :" + new String(CellUtil.cloneValue(cell)) + " ");
System.out.println("Timetamp is :" + cell.getTimestamp() + " ");
}
}
public static void scanColumn(String tableName, String column) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
if(column.indexOf(":")==-1)
scan.addFamily(Bytes.toBytes(column));//column already is a family or its own name
else{
String tFamily=column.substring(0, column.indexOf(":"));
String tQualifier=column.substring(column.indexOf(":")+1, column.length());
scan.addColumn(Bytes.toBytes(tFamily), Bytes.toBytes(tQualifier));
}
ResultScanner scanner = table.getScanner(scan);
for (Result result = scanner.next(); result != null; result = scanner.next()) {
showCell(result);
}
table.close();
close();
}
public static void modifyData(String tableName, String row, String column, String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(row.getBytes());
long ts=System.currentTimeMillis();
put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
table.put(put);
table.close();
close();
}
public static void deleteRow(String tableName, String row) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete=new Delete(row.getBytes());
//delete.addColumn(family, qualifier) delete specific column
table.delete(delete);
table.close();
close();
}
//start a connection
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
//end a connection
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//API use https://www.cnblogs.com/frankdeng/p/9310209.html thanks
String[]ColFamily={"Score","Level"};
String[] fields = {"Score:Math", "Score:Computer", "Score:English","Level:All"};
String[] values = {"90", "80", "60","A"};
try {
createTable("HDFSEXP", ColFamily);
addRecord("HDFSEXP", "row1", fields, values);
scanColumn("HDFSEXP","Level:All");
modifyData("HDFSEXP", "Score", "Math", "100");
deleteRow("HDFSEXP", "row1");
System.out.println("finish");
} catch (IOException e) {
e.printStackTrace();
}
}
}