小白入门篇| HBase1.0+ java-api介绍
Posted ITPUB技术小栈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小白入门篇| HBase1.0+ java-api介绍相关的知识,希望对你有一定的参考价值。
作者:cjfeii 鸣谢:感谢ChinaUnix技术社区的cjfeii提供稿件 ,如需转载,请注明出处。
本文主要介绍了这样的一些改动,并写了一些常用操作的代码以供参考。
Configuration conf = HBaseConfiguration.create(); conf.addResource("hbase-site.xml"); Connection connect = ConnectionFactory.createConnection(conf);
还有一个要注意的,在操作 table的过程中,表名称不能再用String类型,而需要用TableName类型了:
/* test create table. */ publicstaticvoid createTable(String tableName, String[] family) throws Exception { Admin admin = connect.getAdmin(); TableNametn = TableName.valueOf(tableName); HTableDescriptordesc = newHTableDescriptor(tn); for (inti = 0; i<family.length; i++) { desc.addFamily(newHColumnDescriptor(family[i])); } if (admin.tableExists(tn)) { System.out.println("table Exists!"); System.exit(0); } else { admin.createTable(desc); System.out.println("create table Success!"); } }
/* put data into table. */ public static void addData(String rowKey, String tableName, String[] column1, String[] value1, String[] column2, String[] value2) throws IOException { /* get table. */ TableNametn = TableName.valueOf(tableName); Table table = connect.getTable(tn); /* create put. */ Put put = new Put(Bytes.toBytes(rowKey)); HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies(); for (inti = 0; i<columnFamilies.length; i++) { String f = columnFamilies[i].getNameAsString(); if (f.equals("article")) { for (int j = 0; j < column1.length; j++) { put.addColumn(Bytes.toBytes(f), Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j])); } } if (f.equals("author")) { for (int j = 0; j < column2.length; j++) { put.addColumn(Bytes.toBytes(f), Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j])); } } } /* put data. */ table.put(put); System.out.println("add data Success!"); }
/* get data. */ public static void getResult(String tableName, String rowKey) throws IOException { /* get table. */ Table table = connect.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } }
/* scan table. */ public static void getResultScan(String tableName) throws IOException { Scan scan = new Scan(); ResultScannerrs = null; Table table = connect.getTable(TableName.valueOf(tableName)); try { rs = table.getScanner(scan); for (Result r : rs) { for (Cell cell : r.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell)));
System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } } } finally { rs.close(); } }
/* range scan table. */ public static void getResultScan(String tableName, String start_rowkey, String stop_rowkey) throws IOException { Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(start_rowkey)); scan.setStopRow(Bytes.toBytes(stop_rowkey)); ResultScannerrs = null; Table table = connect.getTable(TableName.valueOf(tableName)); try { rs = table.getScanner(scan); for (Result r : rs) { for (Cell cell : r.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell)));
System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } } } finally { rs.close(); } }
/* get column data. */ public static void getResultByColumn(String tableName, String rowKey, String familyName, String columnName) throws IOException { /* get table. */ Table table = connect.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); // 获取指定列族和列修饰符对应的列 Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp());
} }
/* update. */ public static voidupdateTable(StringtableName, String rowKey, String familyName, StringcolumnName, String value) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes .toBytes(value)); table.put(put); System.out.println("update table Success!"); }
/* get multi-version data. */ public static void getResultByVersion(String tableName, String rowKey, String familyName, String columnName) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); get.setMaxVersions(5); Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("------------------------------------"); System.out.println("timest: " + cell.getSequenceId()); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } }
/* delete column. */ public static voiddeleteColumn(StringtableName, String rowKey, String falilyName, StringcolumnName) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Delete deleteColumn = new Delete(Bytes.toBytes(rowKey)); deleteColumn.addColumns(Bytes.toBytes(falilyName), Bytes.toBytes(columnName)); table.delete(deleteColumn); System.out.println(falilyName + ":" + columnName + "is deleted!"); } /* delete row. */ public static voiddeleteAllColumn(StringtableName, StringrowKey) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Delete deleteAll = new Delete(Bytes.toBytes(rowKey)); table.delete(deleteAll); System.out.println("all columns are deleted!");
}
/* delete table. */ public static void deleteTable(String tableName) throws IOException { Admin admin = connect.getAdmin(); admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); System.out.println(tableName + "is deleted!"); }
http://blog.sina.com.cn/s/blog_66474b1601017fxr.html
http://www.aboutyun.com/thread-7149-1-1.html
大会以"架构创新之路"为主题,共设置两个主场分享时段,24个技术交流专场时段;邀请来自互联网、电子商务、金融、电信、政府、行业协会等20多个领域,150多位技术专家及行业领袖来分享他们的经验;并将吸引4000多名系统运维、架构师、及各种企业的IT决策人士参会,为他们提供最具价值的交流平台。
以上是关于小白入门篇| HBase1.0+ java-api介绍的主要内容,如果未能解决你的问题,请参考以下文章