Use of the terminal and the Java API for CRUD operations hbase

First, the  purpose of the experiment

The experiment by using a terminal and a Java API for hbase be CRUD operations, to learn the structure of non-relational databases and how to operate, understand comparative non-relational databases and relational databases, know how to use a relational database Philippine hbase .

Second,  the experiment content

The teacher gives the complete content as possible, how much do you write much

Third,  experimental ideas, structures

In the first achieved under way in hbase Linux command line to build the table, CRUD operations

Then use Elipse using Java programming in hbase construction table, CRUD operations.

Fourth,  the experimental results

HBase create command with create a table as follows:

 

At this point, that creates a "student" table attributes: Sname, Ssex, Sage, Sdept , course. Because HBase table will have a default attribute as a key line without the need to create your own default data after the first operation command to put the table name. After creating the "student" table, you can view basic information "student" table by the describe command. Screenshot execute the following command:

 

Then performs HBase additions change the search operation. When adding data, HBase will automatically add a time stamp data is added, when it needs to change, simply add the data directly, i.e. HBase will generate a new version, thereby completing "to" operation, the old version is still reservations, the system periodically garbage data, leaving only a few of the latest version, the version number of the saved can be specified when creating a table.

 

1. Add Data

HBase add data using the put command, Note: only one line of data is a column of a table, i.e. a cell to add a data, so the direct insertion shell command data efficiency is very low, in practical applications, generally using data programming operation.

 In this case, add a table school student number 95001, named LiYing row of data, the line key to 95001. Because HBase table will have a default attribute as a key line without the need to create your own default data after the first operation command to put the table name.

In this case is the 95001 in the line course column group math column adds a data.

 

2. Delete Data

In the delete data operation with HBase delete command and deleteall, the difference between them: 1 delete for deleting a data, the reverse operation is put; 2 deleteall operable to delete the row of data...

At this point delete the student all the data in the table under the column Ssex 95001 line.

deleteall command

Deleteall 'student', '95001' command to delete the 95001 all the columns in a row, all of the data that is in the row.

 

3. View data

HBase has two commands for viewing data: 1 get command is used to view the table of a data line; 2 scan command is used to view all the data in a table.. .

get command

Returns data 'Student' table '95001' line.

scan command

This will return the 'student' entire data table.

You can also query the underlying historical data

Historical versions of a lookup table requires two steps.

(1). When you create a table, specifying the number of versions saved (assuming that is designated as 5)

. (2) insert the data and then update the data to produce historical versions of data, attention: Here are the insert and update data with the data put command

(3) When a query, the query specified number of historical versions. The default will check out the latest data. (Effective value of 1 to 5)

 

4. Delete table

Delete table has two steps, the first step to let the table is not available, the second step to delete the table.

 

Using Elipse programming for hbase additions and deletions to change search

Code:

  1 import org.apache.hadoop.conf.Configuration;
  2 import org.apache.hadoop.hbase.*;
  3 import org.apache.hadoop.hbase.client.*;
  4 import java.io.IOException;
  5  
  6 public class ExampleForHbase
  7 {
  8     public static Configuration configuration;
  9     public static Connection connection;
 10     public static Admin admin;
 11  
 12     //主函数中的语句请逐句执行,只需删除其前的//即可,如:执行insertRow时请将其他语句注释
 13     public static void main(String[] args)throws IOException
 14     {
 15         //创建一个表,表名为Score,列族为sname,course
 16         //createTable("Score",new String[]{"sname","course"});
 17  
 18         //在Score表中插入一条数据,其行键为95001,sname为Mary(因为sname列族下没有子列所以第四个参数为空)
 19         //等价命令:put 'Score','95001','sname','Mary'
 20         //insertRow("Score", "95001", "sname", "", "Mary");
 21         //在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
 22         //等价命令:put 'Score','95001','score:Math','88'
 23         //insertRow("Score", "95001", "course", "Math", "88");
 24         //在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
 25         //等价命令:put 'Score','95001','score:English','85'
 26         //insertRow("Score", "95001", "course", "English", "85");
 27  
 28         //1、删除Score表中指定列数据,其行键为95001,列族为course,列为Math
 29         //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码取消注释注释,将删除制定列族的代码注释
 30         //等价命令:delete 'Score','95001','score:Math'
 31         //deleteRow("Score", "95001", "course", "Math");
 32  
 33         //2、删除Score表中指定列族数据,其行键为95001,列族为course(95001的Math和English的值都会被删除)
 34         //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,将删除制定列族的代码取消注释
 35         //等价命令:delete 'Score','95001','score'
 36         //deleteRow("Score", "95001", "course", "");
 37  
 38         //3、删除Score表中指定行数据,其行键为95001
 39         //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,以及将删除制定列族的代码注释
 40         //等价命令:deleteall 'Score','95001'
 41         //deleteRow("Score", "95001", "", "");
 42  
 43         //查询Score表中,行键为95001,列族为course,列为Math的值
 44         //getData("Score", "95001", "course", "Math");
 45         //查询Score表中,行键为95001,列族为sname的值(因为sname列族下没有子列所以第四个参数为空)
 46         //getData("Score", "95001", "sname", "");
 47  
 48         //删除Score表
 49         //deleteTable("Score");
 50     }
 51  
 52     //建立连接
 53     public static void init()
 54     {
 55         configuration  = HBaseConfiguration.create();
 56         configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
 57         try
 58         {
 59             connection = ConnectionFactory.createConnection(configuration);
 60             admin = connection.getAdmin();
 61         }
 62         catch (IOException e)
 63         {
 64             e.printStackTrace();
 65         }
 66     }
 67     //关闭连接
 68     public static void close()
 69     {
 70         try
 71         {
 72             if(admin != null)
 73             {
 74                 admin.close();
 75             }
 76             if(null != connection)
 77             {
 78                 connection.close();
 79             }
 80         }
 81         catch (IOException e)
 82         {
 83             e.printStackTrace();
 84         }
 85     }
 86  
 87     /**
 88      * 建表。HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
 89      * @param myTableName 表名
 90      * @param colFamily 列族名
 91      * @throws IOException
 92      */
 93     public static void createTable(String myTableName,String[] colFamily) throws IOException 
 94     {
 95  
 96         init();
 97         TableName tableName = TableName.valueOf(myTableName);
 98  
 99         if(admin.tableExists(tableName))
100         {
101             System.out.println("talbe is exists!");
102         }
103         else 
104         {
105             HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
106             for(String str:colFamily)
107             {
108                 HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
109                 hTableDescriptor.addFamily(hColumnDescriptor);
110             }
111             admin.createTable(hTableDescriptor);
112             System.out.println("create table success");
113         }
114         close();
115     }
116     /**
117      * 删除指定表
118      * @param tableName 表名
119      * @throws IOException
120      */
121     public static void deleteTable(String tableName) throws IOException 
122     {
123         init();
124         TableName tn = TableName.valueOf(tableName);
125         if (admin.tableExists(tn)) 
126         {
127             admin.disableTable(tn);
128             admin.deleteTable(tn);
129         }
130         close();
131     }
132  
133     /**
134      * 查看已有表
135      * @throws IOException
136      */
137     public static void listTables() throws IOException 
138     {
139         init();
140         HTableDescriptor hTableDescriptors[] = admin.listTables();
141         for(HTableDescriptor hTableDescriptor :hTableDescriptors)
142         {
143             System.out.println(hTableDescriptor.getNameAsString());
144         }
145         close();
146     }
147     /**
148      * 向某一行的某一列插入数据
149      * @param tableName 表名
150      * @param rowKey 行键
151      * @param colFamily 列族名
152      * @param col 列名(如果其列族下没有子列,此参数可为空)
153      * @param val 值
154      * @throws IOException
155      */
156     public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException 
157     {
158         init();
159         Table table = connection.getTable(TableName.valueOf(tableName));
160         Put put = new Put(rowKey.getBytes());
161         put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
162         table.put(put);
163         table.close();
164         close();
165     }
166  
167     /**
168      * 删除数据
169      * @param tableName 表名
170      * @param rowKey 行键
171      * @param colFamily 列族名
172      * @param col 列名
173      * @throws IOException
174      */
175     public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException 
176     {
177         init();
178         Table table = connection.getTable(TableName.valueOf(tableName));
179         Delete delete = new Delete(rowKey.getBytes());
180         //删除指定列族的所有数据
181         //delete.addFamily(colFamily.getBytes());
182         //删除指定列的数据
183         //delete.addColumn(colFamily.getBytes(), col.getBytes());
184  
185         table.delete(delete);
186         table.close();
187         close();
188     }
189     /**
190      * 根据行键rowkey查找数据
191      * @param tableName 表名
192      * @param rowKey 行键
193      * @param colFamily 列族名
194      * @param col 列名
195      * @throws IOException
196      */
197     public static void getData(String tableName,String rowKey,String colFamily,String col)throws  IOException
198     {
199         init();
200         Table table = connection.getTable(TableName.valueOf(tableName));
201         Get get = new Get(rowKey.getBytes());
202         get.addColumn(colFamily.getBytes(),col.getBytes());
203         Result result = table.get(get);
204         showCell(result);
205         table.close();
206         close();
207     }
208     /**
209      * 格式化输出
210      * @param result
211      */
212     public static void showCell(Result result)
213     {
214         Cell[] cells = result.rawCells();
215         for(Cell cell:cells)
216         {
217             System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
218             System.out.println("Timetamp:"+cell.getTimestamp()+" ");
219             System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
220             System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
221             System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
222         }
223     }
224 }

 

建表成功

 

在执行完代码中的所有插入操作以后可以看到表中数据为:

 

 

执行三个不同层次的删除操作后表中已经没有数据了

 

再重新执行一个插入数据的操作

然后执行第一个getData操作

 

再执行第二个getData操作

 最后删除表。

五、 心得体会

做实验的时候要细心,注意具体导入的是哪个地方的jar

 

Guess you like

Origin www.cnblogs.com/ku1274755259/p/11108808.html