Hadoop:hbase

Source: Internet
Author: User
Keywords Java http value java http value
Tags access apache api based class client content data

Note: The latest hbase shell has canceled hql support, which means that the INSERT and query SQL statements in the following data are not available. For efficiency reasons, there are now only a few ways to work with data, such as Get,put,scan.

Url:http://hadoop.apache.org/hbase/docs/r0.1.1/api/overview-summary.html
Built on the basis of the HDFS that have been created
1: Modify the hadoop/contrib/hbase/conf/hbase-env.sh
Add Java_home Path


2: Modify Hadoop/contrib/hbase/conf/hbase-site.xml, add the following

Hbase.master


10.0.4.121:11100


the host and port that's HBase master Setupcl at. Hbase.rootdir


Hdfs://10.0.4.121:10100/hbase


the directory shared by region servers. 3: Start HBase


hadoop/contrib/hbase/bin/start-hbase.sh

4: View Http://wiki.apache.org/hadoop/Hbase/HbaseShell, Shell action
4.1 advanced into the shell, hadoop/contrib/hbase/bin/hbase Shell
4.2 Creating Table
Create table offer (Image_big,image_small);
4.2 Insert data, query, delete data
such as:
INSERT INTO Offer (Image_big:,image_small:) VALUES (' ABCDEFG ', ' abc ') Where row = ' Testinsert ';
INSERT into Offer (Image_big:,image_small:) VALUES (' hijklmn ', ' hij ') where row = ' Testinsert ';
INSERT into offer (Image_big:content,image_big:path,image_small:content,image_small:path) VALUES (' ABCDEFG ', ' path_ Big ', ' abc ', ' Path_small ') where row = ' Testinsert ';
INSERT into offer (Image_big:content,image_big:path,image_small:content,image_small:path) VALUES (' Hijklmn ', ' Path_big ', ' hij ', ' Path_small ') where row = ' testinsert ';

Select * from offer where row = ' Testinsert ';
+ ———————— + ————————-+
| Column | Cell |
+ ———————— + ————————-+
| image_big: | hijklmn |
+ ———————— + ————————-+
| image_big:content | hijklmn |
+ ———————— + ————————-+
| image_big:path | path_big |
+ ———————— + ————————-+
| image_small: | hij |
+ ———————— + ————————-+
| image_small:content | hij |
+ ———————— + ————————-+
| image_small:path | path_small |
+ ———————— + ————————-+

Select COUNT (*) from the offer where row = ' Testinsert ';
1 row (s) in set. (0.02 sec) The
can be seen from the above, although we inserted 4 data, but the result is 1,hbase covered with the same data, INSERT2 cover INSERT1,INSERT4 overlay insert2, equivalent to update, We also see from the Shell's introduction that HQL did not provide update
at this point the data results should be as follows:
+ ———-+ ———————— –+ ————————— +
| | Column Image_big | Column Image_small |
| key + ———————— –+ ————————— +
| |: |:content |:p ath |: |:content|:p ATH |
+ ————————————-+ ————————— +
|testinsert|hijklmn|hijklmn |path_big| hij | hij | path_small|
+ ———-+ ———————— –+ ————————— +
Add insert join timestamp what happens?
Delete * from offer where row = ' testinsert ';

INSERT into offer (Image_big:,image_small:) VALUES (' ABCDEFG ', ' abc ') Where row = ' Testinsert ' timestamp ' 1209982310285 ′;
INSERT INTO Offer (Image_big:,image_small:) VALUES (' hijklmn ', ' hij ') where row = ' Testinsert ' timestamp ' 1209982311285′;
INSERT into offer (Image_big:content,image_big:path,image_small:content,image_small:path) VALUES (' ABCDEFG ', ' Path_big ', ' abc ', ' Path_small ') where row = ' Testinsert ' timestamp ' 1209982312285′;
INSERT into offer (Image_big:content,image_big:path,image_small:content,image_small:path) VALUES (' Hijklmn ', ' Path_big ', ' hij ', ' Path_small ') where row = ' Testinsert ' timestamp ' 1209982313285′;

Result either
Select * from the offer where row = ' Testinsert '
or select * from offer where row = ' Testinsert ' timestamp ' 1209982310285′; The
returns only
+ ————————-+ ———————-+
| Column | Cell |
+ ————————-+ ———————-+
| image_big: | hijklmn |
+ ————————-+ ———————-+
| image_big:content | hijklmn |
+ ————————-+ ———————-+
| image_big:path | path_big |
+ ————————-+ ———————-+
| image_small: | hij |
+ ————————-+ ———————-+
| image_small:content | hij |
+ ————————-+ ———————-+
| image_small:path | path_small |
+ ————————-+ ———————-+

I was puzzled, such as HBase architecture The introduction is timestamp, the data is backed up by time. But how do you understand this?
http://www.mail-archive.com/core-user@hadoop.apache.org/msg00222.html, the above page does not seem to support the current, but I have inserted here is successful; Another person understand row and timestamp from the data results are all index level, should be outside the data itself, then do not show it is not a problem, but the data seems to be covered?
Delete First
Delete * from the offer where row = ' Testinsert ';
Select again
SELECT * from offer where row = ' Testinsert ';
+ ————————-+ ———————-+
| Column | Cell |
+ ————————-+ ———————-+
| Image_big: | ABCDEFG |
+ ————————-+ ———————-+
| image_big:content | ABCDEFG |
+ ————————-+ ———————-+
| Image_big:path | Path_big |
+ ————————-+ ———————-+
| Image_small: | ABC |
+ ————————-+ ———————-+
| image_small:content | ABC |
+ ————————-+ ———————-+
| Image_small:path | Path_small |
+ ————————-+ ———————-+
This unexpected discovery indicates that the data is backed up, but no historical data has been searched, and the timestamp condition in the select does not seem to work, and each return is the most recent data. Architecture says insert if there is no time condition, the system defaults to the current time.

5 Client Access HBase
such as the last access HDFs, the introduction of the Hbase-site.xml,lib package, the following code

Package com.chua.hadoop.client;

Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.DataInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.Iterator;
Import Java.util.SortedMap;

Import org.apache.commons.httpclient.HttpClient;
Import Org.apache.commons.httpclient.methods.GetMethod;
Import org.apache.hadoop.hbase.HBaseConfiguration;
Import org.apache.hadoop.hbase.HTable;
Import Org.apache.hadoop.io.Text;

/**
* Description of implementation of Class Hbase.java: TODO class Implementation description
* @author Chua 2008-5-4 05:03:33
*/
public class HBase {

/**
* @param args
*/
public static void Main (string] args) throws Exception {
String domain = "www.dlog.cn";
String path_s = "/uploads/m/me/meichua/meichua_100.jpg";
String path_b = "/uploads/m/me/meichua/200804/22094433_tluyw.jpg";
BYTE] data_s = GetData (domain, path_s);
BYTE] Data_b = GetData (Domain,path_b);

hbaseconfiguration config = new hbaseconfiguration ();
htable table = new htable (config, new Text ("offer"));
Createrecore (table, "Chua", "Image_big", Data_b,path_b);
Createrecore (table, "Chua", "Image_small", data_s,path_s);

Get all data for a row, traverse keyset
SortedMap map = Table.getrow (New Text ("Chua"));
if (!map.isempty ()) {
Iterator it = Map.keyset (). iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());
}
}
Get the data for a row of colunmname
BYTE] data = Table.get (new text ("Chua"), New text ("Image_big:content");
SaveAsFile (data, "c:/chua_big.jpg");
}

public static void Createrecore (htable table,string Row, string colunm,byte[] data, string path) throws IOException {
Long lockid = table.startupdate (new Text (row));
Table.put (Lockid, New Text (colunm+ ": Content"), data);
Table.put (Lockid, New Text (colunm+ ":p ath"), path.getbytes ());
Table.commit (lockid);
}

/**
* Read pictures from the internet
* @param domain
* @param path
* @return
*/
public static byte] GetData (String domain,string path) {
BYTE] Dataresource = null;
try {
HttpClient client = new HttpClient ();
Client.gethostconfiguration (). Sethost (domain,80, "http");
GetMethod GetMethod = new GetMethod (path);
int status = Client.executemethod (GetMethod);
if (status = = 200) {
Dataresource = Getmethod.getresponsebody ();
}
Getmethod.releaseconnection ();
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Download error" +e);
}
return dataresource;
}

/**
* Read from local file
* @param path
* @return
*/
public static byte] GetData (String path) {
File File = new file (path);
DataInputStream dis = null;
try {
dis = new DataInputStream (new Bufferedinputstream (new FileInputStream (file));
int length = dis.available ();
BYTE] data = new Byte[length];
Dis.read (data);
return data;
catch (Exception e) {
E.printstacktrace ();
return null;
}
}

/**
* Save to a file
* @param data
* @param path
*/
public static void SaveAsFile (Byte] data,string path) {
if (data!= null) {
try {
Bufferedoutputstream out = new Bufferedoutputstream (new FileOutputStream (path));
for (byte tmp:data) {
Out.write (TMP);
}
Out.close ();
catch (Exception e) {
E.printstacktrace ();
}
}
}
}
Output:
Image_big:content
Image_big:path
Image_small:content
Image_small:path
The above is a client access HBase example, relatively simple

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.