Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

本文介绍了一个使用Java操作HBase数据库的示例程序,包括创建表、插入数据、查询及删除等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、搭建环境

  新建JAVA项目,添加的包有:

   有关Hadoop的hadoop-core-0.20.204.0.jar

   有关Hbase的hbase-0.90.4.jar、hbase-0.90.4-tests.jar以及Hbase资源包中lib目录下的所有jar包

 

 

001import java.io.IOException;
002import java.util.ArrayList;
003import java.util.List;
004  
005import org.apache.hadoop.conf.Configuration;
006import org.apache.hadoop.hbase.HBaseConfiguration;
007import org.apache.hadoop.hbase.HColumnDescriptor;
008import org.apache.hadoop.hbase.HTableDescriptor;
009import org.apache.hadoop.hbase.KeyValue;
010import org.apache.hadoop.hbase.MasterNotRunningException;
011import org.apache.hadoop.hbase.ZooKeeperConnectionException;
012import org.apache.hadoop.hbase.client.Delete;
013import org.apache.hadoop.hbase.client.Get;
014import org.apache.hadoop.hbase.client.HBaseAdmin;
015import org.apache.hadoop.hbase.client.HTable;
016import org.apache.hadoop.hbase.client.HTablePool;
017import org.apache.hadoop.hbase.client.Put;
018import org.apache.hadoop.hbase.client.Result;
019import org.apache.hadoop.hbase.client.ResultScanner;
020import org.apache.hadoop.hbase.client.Scan;
021import org.apache.hadoop.hbase.filter.Filter;
022import org.apache.hadoop.hbase.filter.FilterList;
023import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
024import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
025import org.apache.hadoop.hbase.util.Bytes;
026  
027public class JinTaoTest {
028  
029    public static Configuration configuration;
030    static {
031        configuration = HBaseConfiguration.create();
032        configuration.set("hbase.zookeeper.property.clientPort", "2181");
033        configuration.set("hbase.zookeeper.quorum", "192.168.1.100");
034        configuration.set("hbase.master", "192.168.1.100:600000");
035    }
036  
037    public static void main(String[] args) {
038        // createTable("wujintao");
039        // insertData("wujintao");
040        // QueryAll("wujintao");
041        // QueryByCondition1("wujintao");
042        // QueryByCondition2("wujintao");
043        //QueryByCondition3("wujintao");
044        //deleteRow("wujintao","abcdef");
045        deleteByCondition("wujintao","abcdef");
046    }
047  
048      
049    public static void createTable(String tableName) {
050        System.out.println("start create table ......");
051        try {
052            HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
053            if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
054                hBaseAdmin.disableTable(tableName);
055                hBaseAdmin.deleteTable(tableName);
056                System.out.println(tableName + " is exist,detele....");
057            }
058            HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
059            tableDescriptor.addFamily(new HColumnDescriptor("column1"));
060            tableDescriptor.addFamily(new HColumnDescriptor("column2"));
061            tableDescriptor.addFamily(new HColumnDescriptor("column3"));
062            hBaseAdmin.createTable(tableDescriptor);
063        } catch (MasterNotRunningException e) {
064            e.printStackTrace();
065        } catch (ZooKeeperConnectionException e) {
066            e.printStackTrace();
067        } catch (IOException e) {
068            e.printStackTrace();
069        }
070        System.out.println("end create table ......");
071    }
072  
073      
074    public static void insertData(String tableName) {
075        System.out.println("start insert data ......");
076        HTablePool pool = new HTablePool(configuration, 1000);
077        HTable table = (HTable) pool.getTable(tableName);
078        Put put = new Put("112233bbbcccc".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
079        put.add("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列
080        put.add("column2".getBytes(), null, "bbb".getBytes());// 本行数据的第三列
081        put.add("column3".getBytes(), null, "ccc".getBytes());// 本行数据的第三列
082        try {
083            table.put(put);
084        } catch (IOException e) {
085            e.printStackTrace();
086        }
087        System.out.println("end insert data ......");
088    }
089  
090      
091    public static void dropTable(String tableName) {
092        try {
093            HBaseAdmin admin = new HBaseAdmin(configuration);
094            admin.disableTable(tableName);
095            admin.deleteTable(tableName);
096        } catch (MasterNotRunningException e) {
097            e.printStackTrace();
098        } catch (ZooKeeperConnectionException e) {
099            e.printStackTrace();
100        } catch (IOException e) {
101            e.printStackTrace();
102        }
103  
104    }
105      
106     public static void deleteRow(String tablename, String rowkey)  {
107        try {
108            HTable table = new HTable(configuration, tablename);
109            List list = new ArrayList();
110            Delete d1 = new Delete(rowkey.getBytes());
111            list.add(d1);
112              
113            table.delete(list);
114            System.out.println("删除行成功!");
115              
116        } catch (IOException e) {
117            e.printStackTrace();
118        }
119          
120  
121    }
122  
123       
124     public static void deleteByCondition(String tablename, String rowkey)  {
125            //目前还没有发现有效的API能够实现根据非rowkey的条件删除这个功能能,还有清空表全部数据的API操作
126  
127    }
128  
129  
130      
131    public static void QueryAll(String tableName) {
132        HTablePool pool = new HTablePool(configuration, 1000);
133        HTable table = (HTable) pool.getTable(tableName);
134        try {
135            ResultScanner rs = table.getScanner(new Scan());
136            for (Result r : rs) {
137                System.out.println("获得到rowkey:" + new String(r.getRow()));
138                for (KeyValue keyValue : r.raw()) {
139                    System.out.println("列:" + new String(keyValue.getFamily())
140                            + "====值:" + new String(keyValue.getValue()));
141                }
142            }
143        } catch (IOException e) {
144            e.printStackTrace();
145        }
146    }
147  
148      
149    public static void QueryByCondition1(String tableName) {
150  
151        HTablePool pool = new HTablePool(configuration, 1000);
152        HTable table = (HTable) pool.getTable(tableName);
153        try {
154            Get scan = new Get("abcdef".getBytes());// 根据rowkey查询
155            Result r = table.get(scan);
156            System.out.println("获得到rowkey:" + new String(r.getRow()));
157            for (KeyValue keyValue : r.raw()) {
158                System.out.println("列:" + new String(keyValue.getFamily())
159                        + "====值:" + new String(keyValue.getValue()));
160            }
161        } catch (IOException e) {
162            e.printStackTrace();
163        }
164    }
165  
166      
167    public static void QueryByCondition2(String tableName) {
168  
169        try {
170            HTablePool pool = new HTablePool(configuration, 1000);
171            HTable table = (HTable) pool.getTable(tableName);
172            Filter filter = new SingleColumnValueFilter(Bytes
173                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes
174                    .toBytes("aaa")); // 当列column1的值为aaa时进行查询
175            Scan s = new Scan();
176            s.setFilter(filter);
177            ResultScanner rs = table.getScanner(s);
178            for (Result r : rs) {
179                System.out.println("获得到rowkey:" + new String(r.getRow()));
180                for (KeyValue keyValue : r.raw()) {
181                    System.out.println("列:" + new String(keyValue.getFamily())
182                            + "====值:" + new String(keyValue.getValue()));
183                }
184            }
185        } catch (Exception e) {
186            e.printStackTrace();
187        }
188  
189    }
190  
191      
192    public static void QueryByCondition3(String tableName) {
193  
194        try {
195            HTablePool pool = new HTablePool(configuration, 1000);
196            HTable table = (HTable) pool.getTable(tableName);
197  
198            List<Filter> filters = new ArrayList<Filter>();
199  
200            Filter filter1 = new SingleColumnValueFilter(Bytes
201                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes
202                    .toBytes("aaa"));
203            filters.add(filter1);
204  
205            Filter filter2 = new SingleColumnValueFilter(Bytes
206                    .toBytes("column2"), null, CompareOp.EQUAL, Bytes
207                    .toBytes("bbb"));
208            filters.add(filter2);
209  
210            Filter filter3 = new SingleColumnValueFilter(Bytes
211                    .toBytes("column3"), null, CompareOp.EQUAL, Bytes
212                    .toBytes("ccc"));
213            filters.add(filter3);
214  
215            FilterList filterList1 = new FilterList(filters);
216  
217            Scan scan = new Scan();
218            scan.setFilter(filterList1);
219            ResultScanner rs = table.getScanner(scan);
220            for (Result r : rs) {
221                System.out.println("获得到rowkey:" + new String(r.getRow()));
222                for (KeyValue keyValue : r.raw()) {
223                    System.out.println("列:" + new String(keyValue.getFamily())
224                            + "====值:" + new String(keyValue.getValue()));
225                }
226            }
227            rs.close();
228  
229        } catch (Exception e) {
230            e.printStackTrace();
231        }
232  
233    }
234  
235}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值