package com.cxy.h ;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.h .Cell;
import org.apache.hadoop.h .H Configuration;
import org.apache.hadoop.h .HColumnDe or;
import org.apache.hadoop.h .HTableDe or;
import org.apache.hadoop.h .TableName;
import org.apache.hadoop.h .client.Admin;
import org.apache.hadoop.h .client.Connection;
import org.apache.hadoop.h .client.ConnectionFactory;
import org.apache.hadoop.h .client.Delete;
import org.apache.hadoop.h .client.Get;
import org.apache.hadoop.h .client.Put;
import org.apache.hadoop.h .client.Result;
import org.apache.hadoop.h .client.ResultScanner;
import org.apache.hadoop.h .client.Scan;
import org.apache.hadoop.h .client.Table;
import org.apache.hadoop.h .util.Bytes;
public class H {
static Configuration conf = null;
static Connection conn = null;
static{
conf = H Configuration.create();
conf.set(\"h .zookeeper.quorum\", \"127.0.0.1:2181\");
try {
conn = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Create table
* @throws IOException
*/
public static void createTable(String tableName,String[]family) throws Exception{
Admin admin = conn.getAdmin();
HTableDe or desc = new HTableDe or(TableName.valueOf(tableName));
for(int i =0;i<family.length;i++){
desc.addFamily(new HColumnDe or(family[i]));
}
if(admin.tableExists(TableName.valueOf(tableName))){
System.out.println(\"Table Exists\");
System.exit(0);
}else{
admin.createTable(desc);
System.out.println(\"Create table success!\");
}
}
/**
* Add data
*/
public static void addData(String rowKey,String tableName,String[] column1,String[] value1,String[] column2,String[] value2) throws IOException{
//config ROW_KEY
Put put = new Put(Bytes.toBytes(rowKey));
Table table = conn.getTable(TableName.valueOf(tableName));
HColumnDe or[] columnFamilies = table.getTableDe or().getColumnFamilies();
for(int i=0;i<columnFamilies.length;i++){
//get column_name
String familyName = columnFamilies[i].getNameAsString();
if(familyName.equals(\"article\")){
for(int j=0;j<column1.length;j++){
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));
}
}
if(familyName.equals(\"author\")){
for(int j=0;j<column2.length;j++){
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j]));
}
}
}
table.put(put);
System.out.println(\"Add data success\");
}
/**
* queryBy ROW_KEY
* @throws IOException
*/
public static Result getResult(String tableName,String rowKey) throws IOException{
Get get = new Get(Bytes.toBytes(rowKey));
Table table = conn.getTable(TableName.valueOf(tableName));
Result result = table.get(get);
for(Cell cell:result.listCells()){
System.out.println(\"family:\"+cell.toString());
}
return result;
}
/**
* Scanner table
*/
public static void getResultScann(String tableName) throws IOException{
Scan scan = new Scan();
ResultScanner rs = null;
Table table = conn.getTable(TableName.valueOf(tableName));
rs = table.getScanner(scan);
for(Result r :rs){
for(Cell cell:r.listCells()){
System.out.println(\"row:\"+Bytes.toString(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength()));
System.out.println(\"family:\"+Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getValueLength()));
System.out.println(\"qualifier:\"+Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(),cell.getQualifierLength()));
System.out.println(\"value:\"+Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
System.out.println(\"timestamp:\"+cell.getTimestamp());
System.out.println(\"-------------------------------------\");
}
}
rs.close();
}
/**
* queryBy column
*/
public static void getResultByColumn(String tableName,String rowKey,String familyName,String columnName) throws IOException{
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
Result result = table.get(get);
for(Cell cell:result.listCells()){
System.out.println(\"family:\"+Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getValueLength()));
System.out.println(\"qualifier:\"+Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(),cell.getQualifierLength()));
System.out.println(\"value:\"+Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
System.out.println(\"timestamp:\"+cell.getTimestamp());
System.out.println(\"-------------------------------------\");
}
}
/**
* queryBy cloumn version
*/
public static void getResultByVersion(String tableName,String rowKey,String familyName,String columnName) throws IOException{
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
get.setMaxVersions(5);
Result result = table.get(get);
for(Cell cell:result.listCells()){
System.out.println(\"version:\"+cell.getTimestamp());
}
}
/**
* delete Column
*/
public static void deleteColumn(String tableName,String rowKey,String familyName,String columnName) throws IOException{
Table table = conn.getTable(TableName.valueOf(tableName));
Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
deleteColumn.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
table.delete(deleteColumn);
System.out.println(familyName+\",\"+columnName+\"is deleted\");
}
/**
* delete rowkey
*/
public static void deleteRowKey(String tableName,String rowKey) throws IOException{
Table table = conn.getTable(TableName.valueOf(tableName));
Delete deleteAll = new Delete(Bytes.toBytes(rowKey));
table.delete(deleteAll);
System.out.println(\"delete row\");
}
/**
* delete table
*/
public static void deleteTable(String tableName) throws IOException{
Admin admin = conn.getAdmin();
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println(tableName+\"is deleted\");
}
public static void main(String[] args) throws Exception {
//# export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/usr/lib/h /lib/*
//# hadoop jar h .jar
//Create table
/*String tableName = \"blog\";
String[] family = {\"article\",\"author\"};
createTable(tableName, family);*/
//Add data
/*String[] column1 = {\" \",\"content\",\"tag\"};
String[] value1 = {\"Head Fist H \",\"Hadoop\",\"H \"};
String[] column2 = {\"name\",\"nickname\"};
String[] value2 = {\"nicholas\",\"lee\"};
addData(\"rowkey1\", \"blog\", column1, value1, column2, value2);
addData(\"rowkey2\", \"blog\", column1, value1, column2, value2);
addData(\"rowkey3\", \"blog\", column1, value1, column2, value2);*/
//queryBy ROW_KEY
/*(\"blog\", \"rowkey1\");*/
//Scanner table
/*getResultScann(\"blog\");*/
//queryBy column
/*getResultByColumn(\"blog\", \"rowkey1\", \"article\", \"content\");*/
//queryBy cloumn version
/*getResultByVersion(\"blog\", \"rowkey1\", \"article\", \"content\");*/
//delete Column
/*deleteColumn(\"blog\", \"rowkey1\", \"article\", \"content\");*/
//delete Row
/*deleteRowKey(\"blog\", \"rowkey1\");*/
//delete table
/*deleteTable(\"blog\");*/
}
}
继续阅读与本文标签相同的文章
下一篇 :
你值得收藏:这个免费AI可以神奇拯救低分辨率照片
-
泉州市加快实施“互联网 生产基地 物流”着力构建区域物流枢纽新格局
2026-05-18栏目: 教程
-
微功能号:不需要借助任何辅助工具!微功能号给你美妙的多群直播体验
2026-05-18栏目: 教程
-
加快检测速度以减缓埃博拉疫情:智能手机应用程序改变了对刚果
2026-05-18栏目: 教程
-
微信小程序前景大好,寻找第三方公司进行开发靠谱吗?
2026-05-18栏目: 教程
-
谷歌翻脸无用,华为淡然应对,成功挽回海外市场
2026-05-18栏目: 教程
