public static void main(String[] args) throws IOException{
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
HTableDescriptor[] tables = admin.listTables();
for(HTableDescriptor s:tables){
System.out.println(s.getNameAsString());
}
if(admin != null)
admin.close();
if(connection!=null)
connection.close();
}
(2) 在终端打印出指定的表的所有记录数据;
public static void main(String[] args) throws IOException{
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
TableName table = TableName.valueOf("student");
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 zoo :" +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("time :"+cell.getTimestamp());
System.out.println();
}
}
}
if(admin != null)
admin.close();
if(connection!=null)
connection.close();
}
(3) 向已经创建好的表添加和删除指定的列族或列;
public static void main(String[] args) throws IOException{
Configuration configuration = new Configuration();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(configuration);
TableName tablename = TableName.valueOf("student");
Table table = connection.getTable(tablename);
//add data
Put put = new Put("2018004".getBytes());
put.addColumn("information".getBytes(), "math".getBytes(), "90".getBytes());
table.put(put);
System.out.println("sucessfully input");
//delete data
Delete delete = new Delete("2018004".getBytes());
delete.addFamily("information".getBytes());
delete.addColumn("information".getBytes(),"math".getBytes());
table.delete(delete);
System.out.println("delete sucessfuly");
table.close();
}
(4) 清空指定的表的所有记录数据;
public static void main(String[] args) throws IOException{
Configuration conf = new Configuration();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
TableName tablename = TableName.valueOf("student");
if(!admin.tableExists(tablename)){
System.out.println("tale is not exists");
}else{
admin.disableTable(tablename);
admin.truncateTable(tablename, false);
System.out.println("all data is delete");
}
if(admin != null)
admin.close();
if(connection != null)
connection.close();
}
(5) 统计表的行数。
public static void main(String[] args) throws IOException{
Configuration conf = new Configuration();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("student"));
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 column is : " + num);
if(connection != null)
connection.close();
}
学号 | 姓名 | 性别 | 年龄 |
---|---|---|---|
2018001 | zhangsan | male | 23 |
2018002 | marry | female | 22 |
2018003 | lisi | male | 24 |
课程号 | 课程名 | 学分 |
---|---|---|
12301 | math | 3.0 |
12002 | computer science | 3.3 |
12003 | english | 3.5 |
学号 | 课程号 | 成绩 |
---|---|---|
2018001 | 123001 | 86 |
2018001 | 123003 | 69 |
2018002 | 123-2 | 77 |
创建hbase格式的表
public static void insertcolumn(Table table ,String row,String columnzoo,String column,String data ) throws IOException{
Put put = new Put(row.getBytes());
put.addColumn(columnzoo.getBytes(),column.getBytes(),data.getBytes());
table.put(put);
System.out.println("insert successfully");
}
public static void addata(Table table) throws IOException{
insertcolumn(table,"2018001","information","name","zhangsan");
insertcolumn(table,"2018001","information","sexy","male");
insertcolumn(table,"2018001","information","years","23");
insertcolumn(table,"2018001","information","Cno","123001");
insertcolumn(table,"2018001","information","Cname","Math");
insertcolumn(table,"2018001","information","Credit","2.0");
insertcolumn(table,"2018001","information","score","86");
insertcolumn(table,"2018001","information","Cno","123003");
insertcolumn(table,"2018001","information","Cname","english");
insertcolumn(table,"2018001","information","Credit","3.0");
insertcolumn(table,"2018001","information","score","69");
insertcolumn(table,"2018002","information","name","marry");
insertcolumn(table,"2018002","information","sexy","female");
insertcolumn(table,"2018002","information","years","22");
insertcolumn(table,"2018002","information","Cno","123002");
insertcolumn(table,"2018002","information","Cname","computer science");
insertcolumn(table,"2018002","information","Credit","5.0");
insertcolumn(table,"2018002","information","score","77");
insertcolumn(table,"2018003","information","name","lisi");
insertcolumn(table,"2018003","information","sexy","male");
insertcolumn(table,"2018003","information","years","24");
}
public static void main(String[] args) throws IOException{
Configuration conf = new Configuration();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(conf);
TableName tablename = TableName.valueOf("student");
Admin admin = connection.getAdmin();
if(!admin.tableExists(tablename)){
HTableDescriptor htabledescriptor = new HTableDescriptor(tablename);
htabledescriptor.addFamily(new HColumnDescriptor("information"));
admin.createTable(htabledescriptor);
System.out.println("table create successfully");
}
Table table = connection.getTable(tablename);
addata(table);
if(admin != null)
admin.close();
if(connection!=null)
connection.close();
}
同时,请编程完成以下指定功能:
(1)createTable(String tableName, String[] fields)
创建表,参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
public static void createTable(String tableName ,String[] fields) throws IOException{
Configuration conf = new Configuration();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
TableName tableNa = TableName.valueOf(tableName);
if(admin.tableExists(tableNa)){
admin.disableTable(tableNa);
admin.deleteTable(tableNa);
System.out.println("old table is deleted");
}
HTableDescriptor htabledescriptor = new HTableDescriptor(tableNa);
for(String s :fields){
htabledescriptor.addFamily(new HColumnDescriptor(s));
}
admin.createTable(htabledescriptor);
System.out.println("new table is create successfully");
if(admin != null)
admin.close();
if(connection != null)
connection.close();
}
(2)addRecord(String tableName, String row, String[] fields, String[] values)
向表tableName、行row(用S_Name表示)和字符串数组files指定的单元格中添加对应的数据values。其中fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,字符串数组fields为{“Score:Math”,”Score;Computer Science”,”Score:English”},数组values存储这三门课的成绩。
public static void addRecord(String tableName,String row,String[] fileds,String[] values) throws IOException{
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf(tableName));
for(int i = 0;i < fileds.length;i++){
Put put = new Put(row.getBytes());
String[] colus = fileds[i].split(":");
put.addColumn(colus[0].getBytes(),colus[1].getBytes(), colus[2].getBytes());
table.put(put);
}
if(connection != null)
connection.close();
}
(3)scanColumn(String tableName, String column)
浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
public static void scanColumn(String tableName,String column) throws IOException{
Configuration conf = new Configuration();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.addFamily(column.getBytes());
ResultScanner scanner = table.getScanner(scan);
for(Result s : scanner){
Cell[] cells = s.rawCells();
for(Cell cell : cells){
System.out.print("row name:"+new String(CellUtil.cloneRow(cell)));
System.out.print("column zoo :" +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("time :"+cell.getTimestamp());
System.out.println();
}
}
if(connection!=null)
connection.close();
}
(4)modifyData(String tableName, String row, String column,String val)
修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。
public static void modifyData(String tableName,String row ,String column,String val) throws IOException{
Configuration conf = new Configuration();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(conf);
long ts = 0;
Put put = new Put(row.getBytes());
Scan scan = new Scan();
Table table = connection.getTable(TableName.valueOf(tableName));
ResultScanner scanner = table.getScanner(scan);
for(Result result : scanner)
for(Cell cell :result.getColumnCells(row.getBytes(), column.getBytes()))
ts = cell.getTimestamp();
put.addColumn(row.getBytes(), column.getBytes(), ts,val.getBytes());
table.put(put);
if(connection != null)
connection.close();
}
}
(5)deleteRow(String tableName, String row)
删除表tableName中row指定的行的记录。
public static void deleteRow(String tableName,String row) throws IOException{
Configuration conf = new Configuration();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(row.getBytes());
table.delete(delete);
if(connection!=null)
connection.close();
}