JAVA API操作hbase1.4.2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | package com.quyf; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * Created by Administrator on 2018/4/8. */ public class HbaseDemo { private static Connection conn; private static Configuration configuration; private static Admin admin; public static void main(String[] args) throws Exception{ //System.out.println("hello world"); } @Before public void setup() throws Exception{ configuration = new Configuration(); configuration.set( "hbase.zookeeper.quorum" , "bg1:2181,bg2:2181,bg3:2181" ); // configuration.addResource(new Path("I:\\gitproject\\hbase_demo\\src\\main\\resources\\hbase-site.xml")); conn = ConnectionFactory.createConnection(configuration); admin = conn.getAdmin(); } /** * 结束之后 关闭对象 */ @After public void End_up() throws Exception { if (conn != null ) conn.close(); } @Test public void create() throws IOException { String[] cols = { "phone" , "addr" , "age" }; createTable( "helloworld" , cols); } /** * @param tableName * @param columns * @throws IOException */ private void createTable(String tableName, String[] columns) throws IOException { TableName tb = TableName.valueOf( tableName ); if ( admin.tableExists( tb )){ System.out.println( "已经存在" ); } else { System.out.println( "not存在" ); HTableDescriptor desc = new HTableDescriptor( tb ); for (String column:columns) { desc.addFamily( new HColumnDescriptor(column)); } admin.createTable(desc); System.out.println( "create " +tableName+ " success" ); } } @Test public void showTable() throws IOException { TableName[] tables = admin.listTableNames(); for (TableName tab:tables){ System.out.println( "表名:" +tab.getNameAsString()); HTableDescriptor desc = admin.getTableDescriptor( tab ); for (HColumnDescriptor column: desc.getColumnFamilies()){ System.out.println(column.getNameAsString()+ "===" +column.toString()); } } } @Test public void getByRowKey() throws IOException { //admin.getTableDescriptor(TableName.valueOf("test")); Table table = conn.getTable(TableName.valueOf( "test" )); Get get = new Get( "0004" .getBytes()); Result rt = table.get(get); System.out.println( rt.toString()); for (Cell cell:rt.rawCells()){ System.out.println( Bytes.toString(CellUtil.cloneRow(cell))+ "\t" + Bytes.toString( CellUtil.cloneFamily(cell))+ "\t" + Bytes.toString(CellUtil.cloneQualifier(cell))+ "\t" + Bytes.toString( CellUtil.cloneValue(cell)) ); } } @Test public void put() throws IOException { Table tab = conn.getTable(TableName.valueOf( "test" )); Put put1 = new Put( "0004" .getBytes()); put1.addColumn( "info" .getBytes(), "age" .getBytes(),Bytes.toBytes( "4" )); //put1.addColumn("info".getBytes(),"username".getBytes(),Bytes.toBytes("hellooww")); Put put2 = new Put( "0005" .getBytes()); put2.addColumn( "info" .getBytes(), "age" .getBytes(),Bytes.toBytes( "45" )); // put2.addColumn("info".getBytes(),"username".getBytes(),Bytes.toBytes("alibaba")); List<Put> list = new ArrayList<Put>(); list.add(put1); list.add(put2); tab.put(list); } @Test public void deletByRowKey() throws IOException { Table table = conn.getTable(TableName.valueOf( "test" )); Delete delete = new Delete(Bytes.toBytes( "0004" )); //如果age列有多个版本的话,这里只删除了最新的一个版本,其他版本的数据还在的, // addColumn是删除某一个列簇里的最新时间戳版本。 // delete.addColumns是删除某个列簇里的所有时间戳版本。 //delete.addColumn("info".getBytes(),"age".getBytes()); //测试下删除不存在的列 // delete.addColumn("info".getBytes(),"hh".getBytes()); //测试下删除不存在的列簇 会报错 column family info22 does not exist in region test delete.addColumn( "info22" .getBytes(), "hh" .getBytes()); table.delete(delete); table.close(); } @Test public void scanTable() throws IOException { Table table = conn.getTable(TableName.valueOf( "test" )); Scan scan = new Scan(); //scan可以加很多条件和范围 scan.withStartRow( "0002" .getBytes()); //scan.withStopRow("0004".getBytes());//不包含末尾行 scan.withStopRow( "0004" .getBytes(), true ); //包含末尾行 ResultScanner rtScan = table.getScanner( scan ); // Iterator itor = rtScan.iterator(); // while(itor.hasNext()){ // System.out.println( itor.next().toString()); // } for (org.apache.hadoop.hbase.client.Result next = rtScan.next();next != null ;next = rtScan.next() ){ System.out.println(next.toString()); } } } |
人生在勤,不索何获,坚持自有收获
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!