hbaseテストjavaコード
10651 ワード
必要なカバンの住所は以下の通りです。http://download.csdn.net/detail/u010310183/8193509
それからコード注視を見てください。すでに表示されています。 自分でテストしてもいいです。
それからコード注視を見てください。すでに表示されています。 自分でテストしてもいいです。
package test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
public class JinTaoTest {
public static Configuration configuration;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.master", "192.168.237.149:60000");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
//configuration.set("hbase.zookeeper.quorum", "192.168.237.149");
}
public static void main(String[] args) throws IOException {
//createTable("wujintao"); //
//insertData("wujintao"); //
//QueryAll("wujintao"); //
//QueryByCondition1("wujintao"); //
//QueryByCondition2("wujintao");
//QueryByCondition3("wujintao");
//deleteRow("wujintao","112233bbbcccc"); // id 112233bbbcccc
// deleteByCondition("wujintao","abcdef");
}
/**
*
* @param tableName
*/
public static void createTable(String tableName) {
System.out.println("start create table ......");
try {
HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
if (hBaseAdmin.tableExists(tableName)) {// , ,
hBaseAdmin.disableTable(tableName);
hBaseAdmin.deleteTable(tableName);
System.out.println(tableName + " is exist,detele....");
}
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
tableDescriptor.addFamily(new HColumnDescriptor("column1"));
tableDescriptor.addFamily(new HColumnDescriptor("column2"));
tableDescriptor.addFamily(new HColumnDescriptor("column3"));
hBaseAdmin.createTable(tableDescriptor);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end create table ......");
}
/**
*
* @param tableName
* @throws IOException
* id , 112233bbbcccc id , column1 , aaa value , mysql
*/
public static void insertData(String tableName) throws IOException {
System.out.println("start insert data ......");
HTable table = new HTable(configuration, tableName);
Put put = new Put("112233bbbcccc".getBytes());// PUT , NEW PUT , ROWKEY, rowkey put
put.add("column1".getBytes(), null, "aaa".getBytes());//
put.add("column2".getBytes(), null, "bbb".getBytes());//
put.add("column3".getBytes(), null, "ccc".getBytes());//
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end insert data ......");
}
public static void dropTable(String tableName) {
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void deleteRow(String tablename, String rowkey) {
try {
HTable table = new HTable(configuration, tablename);
List list = new ArrayList();
Delete d1 = new Delete(rowkey.getBytes());
list.add(d1);
table.delete(list);
System.out.println(" !");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void deleteByCondition(String tablename, String rowkey) {
// API rowkey , API
}
/**
*
* @param tableName
* @throws IOException
*/
public static void QueryAll(String tableName) throws IOException {
// HTablePool pool = new HTablePool(configuration, 1000);
// HTable table = (HTable) pool.getTable(tableName);
HTable table = new HTable(configuration, tableName);
try {
ResultScanner rs = table.getScanner(new Scan());
for (Result r : rs) {
System.out.println(" rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println(" :" + new String(keyValue.getFamily())
+ "==== :" + new String(keyValue.getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* "112233bbbcccc"
* @param tableName
* @throws IOException
*/
public static void QueryByCondition1(String tableName) throws IOException {
// HTablePool pool = new HTablePool(configuration, 1000);
// HTable table = (HTable) pool.getTable(tableName);
HTable table = new HTable(configuration, tableName);
try {
Get scan = new Get("112233bbbcccc".getBytes());// rowkey
Result r = table.get(scan);
System.out.println(" rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println(" :" + new String(keyValue.getFamily())
+ "==== :" + new String(keyValue.getValue()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void QueryByCondition2(String tableName) {
try {
// HTablePool pool = new HTablePool(configuration, 1000);
// HTable table = (HTable) pool.getTable(tableName);
HTable table = new HTable(configuration, tableName);
Filter filter = new SingleColumnValueFilter(Bytes
.toBytes("column1"), null, CompareOp.EQUAL, Bytes
.toBytes("aaa")); // column1 aaa
Scan s = new Scan();
s.setFilter(filter);
ResultScanner rs = table.getScanner(s);
for (Result r : rs) {
System.out.println(" rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println(" :" + new String(keyValue.getFamily())
+ "==== :" + new String(keyValue.getValue()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void QueryByCondition3(String tableName) {
try {
HTablePool pool = new HTablePool(configuration, 1000);
HTable table = (HTable) pool.getTable(tableName);
List<Filter> filters = new ArrayList<Filter>();
Filter filter1 = new SingleColumnValueFilter(Bytes
.toBytes("column1"), null, CompareOp.EQUAL, Bytes
.toBytes("aaa"));
filters.add(filter1);
Filter filter2 = new SingleColumnValueFilter(Bytes
.toBytes("column2"), null, CompareOp.EQUAL, Bytes
.toBytes("bbb"));
filters.add(filter2);
Filter filter3 = new SingleColumnValueFilter(Bytes
.toBytes("column3"), null, CompareOp.EQUAL, Bytes
.toBytes("ccc"));
filters.add(filter3);
FilterList filterList1 = new FilterList(filters);
Scan scan = new Scan();
scan.setFilter(filterList1);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.println(" rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println(" :" + new String(keyValue.getFamily())
+ "==== :" + new String(keyValue.getValue()));
}
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
は最後にブートファイルを設定する必要があります。 run.sh 上の文章でこのブートファイルを紹介しました。 フォーマットは以下の通りです#!/bin/sh
LIBS_DIR=/usr/hadooptest/lib //
for i in $LIBS_DIR/*.jar
do CLASSPATH=$i:"$CLASSPATH"
done
SERVER=/usr/hadooptest // bin
cd $SERVER/bin
java -Xms512m -Xmx1024m -classpath $CLASSPATH test.JinTaoTest //