使用java操作hbase(单节点) - SunnyCx
使用java操作hbase(单节点)
1.在运行java代码之前,一定要先启动Hbase,很重要!!
cd /home/cx/itcast/hbase-1.2.6/bin
./start-hbase.sh
2.新建一个java项目,导入相关的jar包,放在lib目录下,并右键build path将它们添加到referenced libraries
3.在项目下新建conf文件夹,将Hbase的配置文件hbase-site.xml复制到该目录,然后选择项目属性,在Libraies->add class
folder,将conf目录选上。(这一步代替了集群情况下,java连接zookeeper的操作)
hbase-site.xml配置如下:
<configuration><property>
<name>hbase.rootdir</name>
<value>file:///home/cx/myhbase1/</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/bigfish/myhbase1/zookeeper</value>
</property>
</configuration>
4.java代码如下:
1 package com.cx;2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.hadoop.conf.Configuration;
8 import org.apache.hadoop.hbase.HBaseConfiguration;
9 import org.apache.hadoop.hbase.HColumnDescriptor;
10 import org.apache.hadoop.hbase.HTableDescriptor;
11 import org.apache.hadoop.hbase.MasterNotRunningException;
12 import org.apache.hadoop.hbase.TableName;
13 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
14 import org.apache.hadoop.hbase.client.*;
15 import org.apache.hadoop.hbase.util.Bytes;
16
17
18
19 public class Hbase1 {
20 static private Configuration conf=HBaseConfiguration.create();;
21 //------------------创建表操作 -------------------------------------
22 public static void createTable(String tableName,String[] cfs) throws Exception {
23 HBaseAdmin admin=new HBaseAdmin(conf);
24 if(admin.tableExists(tableName)) {
25 System.out.println("表已经存在");
26 }else {
27 //指定表名
28 HTableDescriptor htd=new HTableDescriptor(TableName.valueOf(tableName));
29 //添加列族
30 for(int i=0;i<cfs.length;i++) {
31 htd.addFamily(new HColumnDescriptor(cfs[i]));
32 }
33 //创建表
34 admin.createTable(htd);
35 System.out.println("表创建成功!");
36 }
37 admin.close();
38 }
39 //----------------------一次插入一条数据----------------------------------
40 public static void put
41 (String tableName,String row,String columnFamily,String column,String data) throws Exception{
42 //得到一个表对象
43 HTable table =new HTable(conf, tableName);
44 //得到一个Put对象
45 //将字符串转换为字符数组
46 Put put=new Put(Bytes.toBytes(row));
47 put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
48 //在表中放入put对象
49 table.put(put);
50 System.out.println("插入数据成功!");
51 table.close();
52 }
53 // //----------------------一次插入海量数据(1oow)--------------------------
54 // public static void putAll(String tableName) throws Exception{
55 // HTable table =new HTable(conf, tableName);
56 // //得到list对象//得到list对象
57 //
58 // List<Put> puts=new ArrayList<Put>(10000);
59 // for(int i=1;i<=1000000;i++) {
60 // Put put =new Put(Bytes.toBytes("rk"+i));
61 // put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(""+i));
62 // puts.add(put);
63 // //每隔1w条放一次
64 // if(i%10000==0){
65 // table.put(puts);
66 // puts=new ArrayList<Put>(10000);//相当于清空
67 // }
68 // }
69 // table.put(puts);
70 // table.close();
71 // }
72 //------------------查询一个---------------------------
73 public static void get(String tableName,String row) throws IOException{
74 HTable table =new HTable(conf, tableName);
75 Get get =new Get(Bytes.toBytes(row));
76 //传get对象
77 //返回result对象
78 Result result=table.get(get);
79 String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
80 System.out.println("get="+r);
81 table.close();
82 }
83 //---------------------查询多个-------------------------------
84 public static void scan(String tablename) throws Exception{
85 HTable table = new HTable(conf, tablename);
86 Scan scan = new Scan();
87 ResultScanner rs = table.getScanner(scan);
88 for(Result result:rs){
89 String r=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("name")));
90 System.out.println("Scan:"+r);
91 }
92 table.close();
93 }
94 //----------------------更新(put将老版本覆盖,查询最新的)-----------------
95
96
97 //-------------------------删除--------------------------------------
98 public static void del(String tableName) throws IOException{
99 HTable table =new HTable(conf, tableName);
100 //创建delete对象
101 Delete delete = new Delete(Bytes.toBytes("rk0003"));
102 table.delete(delete);
103 System.out.println("删除成功!");
104 table.close();
105 }
106
107 public static void main(String args[]) throws Exception
108 {
109
110 String tableName="people";
111 String columnFamilys[]={"info","data"};
112 Hbase1.createTable(tableName, columnFamilys);
113 Hbase1.put(tableName,"rk0001" , "info", "name", "cx");
114 Hbase1.put(tableName,"rk0002" , "info", "name", "zhangsan");
115 Hbase1.put(tableName,"rk0001" , "info", "age", "20");
116 Hbase1.put(tableName,"rk0001" , "info", "sex", "male");
117 Hbase1.put(tableName,"rk0001" , "info", "height", "1.75");
118 Hbase1.put(tableName,"rk0002" , "info", "age", "22");
119 Hbase1.put(tableName,"rk0001" , "data", "sal", "10000");
120 Hbase1.put(tableName,"rk0003" , "info", "age", "25");
121 // Hbase1.putAll(tableName);
122 Hbase1.get(tableName,"rk0001");
123 Hbase1.scan(tableName);
124 Hbase1.del(tableName);
125 }
126
127 }
5.运行无错误
6.在命令行中查询表是否建立成功
以上是 使用java操作hbase(单节点) - SunnyCx 的全部内容, 来源链接: utcz.com/z/391352.html