//maven 配置
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0</version>
</dependency>
//初始化配置
public static Configuration configuration;
private static final String Table_NAME;
private static final String Family;
private static final String COLUMN;
static{
String configFile = "hbase.properties"; //文件名称
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(configFile);
if (in == null)
throw new RuntimeException("No configuration file!!" + configFile);
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", prop.getProperty("hbase.zookeeper.quorum"));
Table_NAME=prop.getProperty("tablename");
Family=prop.getProperty("family");
COLUMN=prop.getProperty("column");
}
//根据rowkey修改列值
Table table =null;
Connection connection =null;
try {
connection = ConnectionFactory.createConnection(configuration);
table = connection.getTable(TableName.valueOf(Table_NAME));
Put put = new Put(Bytes.toBytes(product_no));
put.addColumn(Bytes.toBytes(Family), Bytes.toBytes(COLUMN),
Bytes.toBytes(product_level));
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
table.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//配置文件
hbase.zookeeper.quorum=k1:2181,k2:2181,k3:2181
tablename=product_info:product_info
family=info
column=product_level