开发者

hbase - how to change tables structure without drop tables

开发者 https://www.devze.com 2023-04-02 03:15 出处:网络
i use grails-1.3.2 and gorm-hbase-0.2.4 plugin. Sometimes i need to change tables structure(add new tables or columns).

i use grails-1.3.2 and gorm-hbase-0.2.4 plugin.

Sometimes i need to change tables structure(add new tables or columns). I have created Car table:

class Car{
    static belongsTo = [user:User]

    String color
    String model
    //.....

    static constraints = {
    }
}

but when i want to c开发者_StackOverflow中文版reate car object:

def create = {
        Car car = new Car()
        car.properties = params
        car.save(flush: true)        
 }

I got the following exception:

ERROR gorm.SavePersistentMethod  - APP_CAR
org.apache.hadoop.hbase.TableNotFoundException: APP_CAR

After i run application with create-drop, everithing starts work good.. but i can not after every changes delete all data, i thought plugin have to do all updates

so, i am looking some way after changind tables structure continue to run application without drop tables..

If anybody know solution please help.


Grails will NOT do automatic updates to your tables, what if it drops a column in production automatically? Maybe that is not what you wanted.

There is a database migration plugin to do this and here is an excellent link that explains it. Note that you need to use grails prod instead of using the ones directly in the link, otherwise it will run in development mode only. The link does not show prod in its commands.

The official links are here and the spring source blog about this is here.


database migration plugin will not be works, because it works only with hibernate. You need to do some changes in plugin source. HBasePluginSupport.grovy

static doWithApplicationContext = {ApplicationContext applicationContext ->
    LOG.debug("Closure HBasePluginSupport.doWithApplicationContext{} invoked with arg $applicationContext")

    assert !PluginManagerHolder.getPluginManager().hasGrailsPlugin("hibernate"),"hibernate plug-in conflicts with gorm-hbase plug-in"

    // Read data source configuration, setting defaults as required
    def dataSource = application.config.dataSource
    // TODO write tests for this <--- Even maybe figure out if this is ever invoked
    if (!dataSource) dataSource = new HBaseDefaults()

    def dbCreate = dataSource?.dbCreate
    if (!dbCreate) dbCreate = "create-drop"
    LOG.debug("Data Source configured with dbCreate set to $dbCreate")

    // TODO Complete dbCreate related processing
    if (dbCreate?.toUpperCase()?.equals("CREATE-DROP")) {
        def createIndexedTables = dataSource?.indexed
        LOG.debug ("Flag createIndexedTables set to $createIndexedTables")
        def tableManager = HBaseLookupUtils.getBean("hbase.table.manager")

        tableManager.createSequenceTable()
        tableManager.createReferenceTable()

        application.domainClasses.each {domainClass ->
            LOG.debug("Adding table for Domain Class $domainClass")
            tableManager.createDomainTable(domainClass, createIndexedTables)
        }

        LOG.debug("List of all store found :")
        tableManager.getTableNames().each {tn ->
            LOG.debug("- $tn")
        }
    } else if (dbCreate?.toUpperCase()?.equals("UPDATE")) {
        def createIndexedTables = dataSource?.indexed
        def tableManager = HBaseLookupUtils.getBean("hbase.table.manager")
        def existingTables = tableManager.getTableNames();

        application.domainClasses.each {domainClass ->
            LOG.debug("Domain Class $domainClass")
            def tableDesc = new HTableDescriptor(HBaseNameUtils.getDomainTableName(domainClass))
            if (!existingTables.contains(tableDesc.getNameAsString())) {
                tableManager.createDomainTable(domainClass, createIndexedTables)
                LOG.debug("Adding table for Domain Class $domainClass")
            }
        }
    }

    application.domainClasses.each {domainClass ->
        LOG.debug("Adding dbms related methods to Domain Class $domainClass")
        def domainClassManager = new HBaseDomainClassManager()
        domainClassManager.createQueryMethods(domainClass)
        domainClassManager.createPersistenceMethods(domainClass)
        domainClassManager.addLazyLoadingSupport(domainClass)
        domainClassManager.addDynamicFinders(domainClass)
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号